don't know how to find the subtraction between two peaks (not manualy)

8 views (last 30 days)
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
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...

Sign in to comment.

Accepted Answer

Image Analyst
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
Kobi
Kobi on 23 Nov 2013
thank you for you'r help so far
this is ok for the first 3 plots because there are only two peaks but in the 4rth there is a problem and when i try to run this inside a loop i get this error:
"In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in finel_finel_kobi_try (line 89) difference_between_two_peaks(ind) = diff(locs);"
because there is a vector bigger then (1,2) so the diff output to me not a scalar but a vector.
[row,col] = size(R);
for ind=[1:col];
[pks,locs] = findpeaks(R(:,ind)); %find the peaks of the auto corolation
[value_of_max_peak,index_inside_the_frame_of_the_max_peak] = max(pks);
locs(index_inside_the_frame_of_the_max_peak)=[]; % to remove the main peak index
[value_of_max_peak,index_inside_the_frame_of_the_max_peak] = max(pks);
pks(index_inside_the_frame_of_the_max_peak)=[];
difference_between_two_peaks(ind) = diff(locs);
end
because i can't publish the rest of the code here i sent it to you in a private message
Image Analyst
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.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!