Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Peak Search Algorithm with sliding window and peak excursion

Peak Search Algorithm with sliding window and peak excursion

Scheduled Pinned Locked Moved C#
csharpcssalgorithmsdata-structuresdebugging
25 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    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

    K Offline
    K Offline
    Krellon
    wrote on last edited by
    #21

    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

    L 1 Reply Last reply
    0
    • L Lost User

      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[^]

      K Offline
      K Offline
      Krellon
      wrote on last edited by
      #22

      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

      L 1 Reply Last reply
      0
      • K Krellon

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #23

        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

        L 1 Reply Last reply
        0
        • K Krellon

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #24

          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[^]

          1 Reply Last reply
          0
          • L Lost User

            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

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #25

            It's not as much as a question, but more a set of specs. My interest in the question is gone suddenly :)

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups