LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Peak detection- problems finding x values of location

Solved!
Go to solution

Hello, I'm a a learning programmer and I have a problem you might be able to help with. I have an array of values, a frequency (x) and a voltage (y) that I need to process. I need to find the local maxima and minima. I am using the waveform peak detect vi to do so. I need to find the frequency at which there is a voltage spike, and so what I am doing is the following:

1. run peak detection

2. take the locations of the peaks and multiply by the step size

3. add to the starting frequency to find the frequency at which the spike occured

 

i am adding the product of the peak location and step size because it is my inderstanding that the waveform peak detection VI will not take my x values, but will do all of the peak detection relative to the point, for example, if I have an array that contains y points as 1,2,-2,5,6,7,8,9 then it would return a peak somewhere between points 2 and 3. 

 

The problem is that the frequency it tells me the peak/valley is at does not match the graph of the array. Any ideas on why ths is happening or how i could fix the problem? Many thanks.

0 Kudos
Message 1 of 14
(4,501 Views)

I saw that you posted to another, somewhat related thread.

 

Please post your code with some typical data saved as default so we can look at it.

 

If you look at the detailed help for the peak detector VI you will see that it fits a quadratic curve to the data and finds the peak or valley of that fitted curve. If your peak shape is not very much like a quadratic curve, the location may be off due to the poor fit.  A sawtooth waveform for example will have peaks quite some distance for where the Peak Detector VI thinks they are. Larger values for width make the errors worse in such a case.

 

The peak detector VI is generally quite good for smooth, symmetric peaks. For other types of waveforms you may need to create your own algorithm.

 

Lynn

0 Kudos
Message 2 of 14
(4,485 Views)

I don't think I posted another thread, I posted the question on another, much older, thread, but I don't remember making an entire new thread other than this one. Yeah I can post my code. It's really strange because I was looking through the program that my program is supposed to be replacing and the general method for finding the peak locations is the same as mine, except for the old program, as poorly as it is written, is more accurate than mine. I've had someone else take a look at the code with me and it makes absolutely no sense why the old program is more accurate than mine. 

 

Here's the code for my program; it's the entire program (sorry I didn't make separate VIs) but the part I am having problems with is the second frame of the sequenced structure. I'm sorry I can't attach data as defaults to it, i don't have access to the FMR equipment right now. I can (and will), however, attach a text file of a sample experiment and a generic image of what i am looking for.

 

The first colum of the data is the frequency that our sample is being exposed to. The second column is the measured voltage at that frequency. I am looking for the frequencies at which the voltage is either highest or lowest, hence the peak detection. I am multiplying my data by -1 to flip the graph because the scientists I am working with are used to working with it in that orientation (again, the last program was extremely poorly coded). Please let me know if you have any questions or need me to save the file in an older form (I am using 2011) Thanks a ton

Download All
0 Kudos
Message 3 of 14
(4,465 Views)

I was referring to your posting in that old thread.

 

Without at least the OrthogonalSmooth.vi I cannot look at what the program is doing.  You can zip the subVIs and attach a single file.

 

Some general comments:

1. Use only one event structure in a program. Please read the detailed help file for the event structure, in particular, the caveats and recommendations for using events section. The way you have it set up now will be a problem if someone presses the Save Data or Write CW buttons before pressing Run.

2. You should avoid having code which may take mre than a few milliseconds to execute inside the event structure. Writing to and reading from instruments inside the event strucuture is not a good idea as it may make the program very unresponsive to user inputs. Look at the Producer/Consumer templates.

3. As the note on your diagram indicates you should move from sequence structures to a state machine architecture using a case structure.

4. Avoid local variables. In this case they do not cause any particular problems but get out of the habit of using them.  Wires are always better.

5. Single iteration while loops make no sense in this application. You could eliminate them and the program would work (or not) exactly the same.

6. Avoid hiding wires behind other objects. It makes the flow of the program difficult to follow.

 

Lynn

0 Kudos
Message 4 of 14
(4,444 Views)

I have heard that some people are having problems attaching files to the Forums. If it does not work, trying a different browser may help.

 

It is better to keep the thread going on the Forum rather than through PMs. This way other people can contribute or learn from what has been posted. Also the link you included in the PM goes to your account and I cannot access it without creating an account of my own ad then getting permission from you to access yours.

 

Lynn

0 Kudos
Message 5 of 14
(4,406 Views)

This post is just a test, it seems I cannot post anything no matter which browser I use

0 Kudos
Message 6 of 14
(4,379 Views)

Here's just the orthogonal smooth, it seems that if I attach one file at a time it works okay

0 Kudos
Message 7 of 14
(4,371 Views)

The OrthogonalSmoothPointByPoint.vi is a subVI of OrthogonalSmooth.vi.  That is likely where the problem is.

 

I just looked at the data you posted earlier (test data.txt). When I plot it on an XY graph I get this:

 

test data.png

 

What results do you expect for this data? I am not asking what your code produce, but what would you get if you analyzed it manually.  For example the highest peak appears to be at (9.60, 12.72). What other peaks and valleys do you expect? There is a small inflection point at (9.465, 5.85). Is that something you need to detect? What about the little "wrinkles" below 9.2?  Is this data from the V vs Freq. graph or from a different part of your program?

 

Lynn

0 Kudos
Message 8 of 14
(4,352 Views)

I need to detect the highest peaks, for example, the one around 9.38 and the one at 9.6. When I run an experiment I might see the peak at, for example, 9.42 but the software will see it at 9.4. This is a problem wheni am trying to write a very specific frequency to the generator, as the signal I will get with 9.4 is not as strong/noise-free as the one I would get with 9.42

 

I don't think the orthogonal fit is the problem because we have two versions of the program; my version and the unstable version that was written by some other guy 6 years ago. His version uses the orthogonal smoothing as well, and would display a peak at, for example (this is actual data from a test), 9.427 and when I ran the same test on my program i got 9.409 GHz. I had my mentor take a look through all of the code with me and we do not have the slightest idea why his program is more accurate than mine. It seems that as illogical and sometimes plain silly his program is, the one are where it's better than mine is the accuracy.


Edit: Yes the data is from one of my test runs

0 Kudos
Message 9 of 14
(4,347 Views)

I know how NMR spectrometers work, but I am not as familiar with the physics/chemistry of the samples and how that relates to the spectrum you get.  The three highest peaks in the data you posted all look quite symmetrical, although the datapoints do not necessarily hit the highest point of the peak.  I ran the peak detector on that data and found that the data is not sensitive to the peak width over the range of 3 to 10 points.  This is consistent with the visual appearance of the data.

 

I suspect that the difference between your program and the old one has to do with the way the data is collected rather than the analysis.

 

Is the sweep speed the same for both programs? I know that other types of swept systems can be quite sensitive to sweep rates. Your program takes a different amount of time per iteration, depending on the number of samples per average. If the frequency sweep is not synchronized with your sampling, you may be sweeping at an unknown and variable rate.

 

Lynn

0 Kudos
Message 10 of 14
(4,335 Views)