From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with simple Sine Wave peak detection

Hello everybody,

anyone already faced issues with the Peak Detector VI supplied with LabVIEW.

 

Attached a simple VI I'm using to stress Peak Detector.vi with a simulated noisy signal.

 

If I put very low noise the peak detection is just fine, no problem at all.

 

But after I add a reasonable noise level, even though I increase/adjust the width parameter, it will count an extra peak or valleys once in a while without apparent explanation.

 

See below it found two peaks at the 6th peak (near X=1080), why is that? Apparently it does its own noise filtering, how can I fix that?

11Peaks.png11Peaks@1080.png

PeakDetectionSimulator.png

0 Kudos
Message 1 of 4
(2,596 Views)

Look at the data you show for the double peak.  What would you call the "peak"?  As you've probably realized, "noise" makes an otherwise-smooth signal get lots of local maxima and minima ("peaks" and "valleys"), and it becomes something of a challenge to remove them and deal with the underlying signal.  Furthermore, if the ideal signal is a sinusoid and you want to detect the peak, the worst place to look for it is ... near the peak!  Why?  Because that's where it is the flattest (or, as the mathematicians would say, where its derivative is near zero).

 

There are some heuristic things you can do.  One is if you "know" the number of peaks and valleys you expect, you can widen the width until the number of peaks and valleys is the "right number".  Another is you can look at the spacing of peaks and valleys, and if you have two peaks very close to each other, take their average.  Of course, you need to look at your data and see what makes sense.

 

Bob Schor

0 Kudos
Message 2 of 4
(2,592 Views)

Well the VI seems to work pretty well at low-ish noise, it does find a single peak around where a human would believe the actual peak of the noise-free sine wave would be.

 

I would believe the purpose of the threshold value is to make sure it reports a single peak from the data that is above that threshold, and a single valley for the data below the threshold.

 

Here I'm simulating the noised signal so I know how many cycles I have. In the real life it won't be the case, I want to rely on something to get the actual number of peaks/valleys. The VI seems to do it pretty well at a certain limit. But the behavior is weird to me, it doesnt make sense that it reports two peaks that are so close from each other.

 

You're correct I could implement my own code and re-invent the wheel that LabVIEW already did, they do it almost perfectly but I realize it may be quite tricky.

0 Kudos
Message 3 of 4
(2,563 Views)

@JICR wrote:

You're correct I could implement my own code and re-invent the wheel that LabVIEW already did, they do it almost perfectly but I realize it may be quite tricky.


What you need is a "local phase-less smoother", something like "replace each point with the average of the 2n+1 points centered around this point" (i.e. a 3-point, 5-point, 29-point average centered at the point-in-question).  The Peak Detector routine is probably doing something like this inside.  You could easily code up such a "pre-peak" routine and see if you like/believe it (compare the "peak" of the filtered wave with the data from the unfiltered wave, see if you like it).

 

Bob Schor

0 Kudos
Message 4 of 4
(2,544 Views)