Peak Search Algorithm with sliding window and peak excursion
-
using System;
namespace ConsoleApplication10 {
class Program {
static void Main( string\[\] args ) { double\[\] trace = new double\[\] { 47.92370605, 44, 49, 46.31715393, 44.98129654, 46.17653275, 78, 73, 82, 63, 69, 41.72979736, 41.13000488, 41.49586487, 37, 40, 30, 40.79894257, 40.65593719, 41.37298584, 39.13498688, 55, 51, 60, 39.3440094, 38.51491547, 42, 35.12589264 }; double low = trace\[ 0 \]; double previous = trace\[ 0 \]; double peak = trace\[ 0 \]; for ( int i = 1; i < trace.Length; i++ ) { double current = trace\[ i \]; if ( current > previous ) { peak = current; } else { if ( current < previous ) { if ( ( peak - low >= 6.0 ) && ( peak - current >= 6.0 ) ) { Console.WriteLine( $"Peak: {peak} @{i}" ); low = peak = current; } } } previous = current; } // end for. Console.ReadKey(); }
} // end class.
}"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
Hi Gerry, This is a smashing bit of code and works great on the sample data provided. However, if you change p10 value 63 to 64, we lose peak 11 value 69 as expected but we also lose peak 24 at level 60. Many thanks for the great code :) it gives me food for thought :-) Nigel
-
Krellon wrote:
If I could embed an image into my comment I would show you.
A good thing you can't; I asked not only because I'm bad at math, but also because I'm a bit simple. The PC is too, so once you can describe your problems in those kind of steps it becomes easy to translate it to code. So I opened Calc, generated a chart, and.. you're looking for those three tops?
Krellon wrote:
Your essentially looping through an array of points looking for the biggest and smallest peaks and waiting for the delta either side them to go >= 6dB. When it does you grab the biggest peak between the -6dB points. Rinse and repeat.
That sounds like pseudo-code, so let me pseudo away (without compiler)
double lastValue = 44;
List peaks = new List();
for (int i = 0; i < trace.Length; i++)
{
double currentValue = trace[i];// waiting for the delta (so, diff between this and last value?)
double delta = currentValue - lastValue;// either side them to go >= 6dB. (both sides = do for + and -)
if (Math.Abs(delta) > 6)
{
// When it does you grab the biggest peak between the -6dB points.
DoMagic();
}// now the current value becomes our "last read value" before looping again
lastValue = currentValue;
} // Rinse and repeat.There's a DoMagic in there, because that's a complex sentence there. From which range would you want the maximum? Which two points would the -6dB points be? Do you want to use a fixed window size? If you can describe that in simple terms, you can complete the code :)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
Hi Eddy, That's pretty much it. Two things to note though are, inside so magic we check the peak is a positive peak and not a trough with 6dB sides. and lastvalue would become the index of the point >= 6db down on the right side of the peaks slope. What this means is that the window could grow dynamically. By setting a physical window width we can limit the max search window +/- either sode of the located peaks. Thanks again for your time Br Nigel
-
Hi Gerry, This is a smashing bit of code and works great on the sample data provided. However, if you change p10 value 63 to 64, we lose peak 11 value 69 as expected but we also lose peak 24 at level 60. Many thanks for the great code :) it gives me food for thought :-) Nigel
-
Hi Eddy, That's pretty much it. Two things to note though are, inside so magic we check the peak is a positive peak and not a trough with 6dB sides. and lastvalue would become the index of the point >= 6db down on the right side of the peaks slope. What this means is that the window could grow dynamically. By setting a physical window width we can limit the max search window +/- either sode of the located peaks. Thanks again for your time Br Nigel
At any point, did you realize that you are talking in math and that I did not understand a word?
Krellon wrote:
What this means is that the window could grow dynamically. By setting a physical window width we can limit the max search window +/- either sode of the located peaks.
For that, you want recursion. This can be(come) as simple or as complicated as you like :)
Krellon wrote:
Thanks again for your time
You're welcome, ofc. If you want to pay it back, simply answer someone elses question in return :)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
One works with the samples provided. Your "requirements" keep changing.
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal