don't know how to find the subtraction between two peaks (not manualy)
8 views (last 30 days)
Show older comments
hi, i just made this program that gives me a plot with something like a sinc function. i need to find the difference between the index of the two peaks (not the one peak in the middle)

, manualy i can do this by subtracting the indexes but i want to make a program that will take plots different then this and do the same thing the peaks are the values from matrix R
eventually i till make a for loop and change the number 2 (as shown in the code) and do the same calculation for the rest of the matrix
[pks,locs] = findpeaks(R(:,2))
as you can see from the attached figure that the function changed
1 Comment
dpb
on 23 Nov 2013
How do you know which peaks are to be compared is the first problem to specify. Once you know that, then an algorithm to find them and after that the rest is easy.
But, specific response relies the answer to the first above...
Accepted Answer
Image Analyst
on 23 Nov 2013
Don't you just use diff(locs)? Here's a full blown demo:
% Create sample data.
t = linspace(-50, 50, 300);
y = sin(t)./t;
% Plot it.
plot(t, y, 'LineWidth', 3);
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Find the peaks.
[peakValues, peakIndexes] = findpeaks(y);
% Plot peaks.
hold on;
plot(t(peakIndexes), peakValues, 'rd', 'MarkerSize', 25, 'LineWidth', 2);
% Find the largest (highest) peak.
[maxPeakValue, maxPeakIndex] = max(peakValues)
% Kobi didn't want the central tallest peak considered, so get rid of that one.
peakValues(maxPeakIndex) = [];
peakIndexes(maxPeakIndex) = [];
% Plot all except the tallest.
plot(t(peakIndexes), peakValues, 'g*', 'MarkerSize', 25, 'LineWidth', 2);
% Find separation between all the remaining peaks.
indexSeparations = diff(peakIndexes)
2 Comments
Image Analyst
on 23 Nov 2013
I don't take private messages or email. The diff output WILL be a vector if you have more than 2 peaks. You can take the histogram of it or the average if you want that - just be aware that the gap in the middle is twice as big because you told me to ignore the central peak. If you want to store the various peak spacings, you can use a cell array for difference_between_two_peaks because the number will vary each time.
difference_between_two_peaks{ind} = diff(locs);
Now, sometimes a cell (element) of difference_between_two_peaks might have 5 values and sometimes it might have 3 values, or whatever it happens to be.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!