MATLAB: Don’t know how to find the subtraction between two peaks (not manualy)

loopMATLABmatrixpeakssinc

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

Best Answer

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)