MATLAB: How to find indices of nearest zero values to a peak in the data

signal analysis

Hi there,
I am doing some signal analysis, and I have a signal plot with lots of useful peaks that I need to integrate. Between the peaks is lots of noise. To avoid the noise, I need to know the nearest zeros to each peak (one on each side of the peak), and the index at which those points happen. Realistically, while the data fluctuates about zero, there are no points that are precisely zero, so I need the points at which the curves first cross zero on each side of each peak. As in, the xdata value for when ydata is nearest to zero, and xdata is that corresponding value on either side of each peak. Then, I need the index of those two xdata values so I can integrate.
I am having lots of trouble with this logic. My method so far has been to use "fudge factors" and do it manually but this analysis will need to happen frequently over many months, so I would like to automate.

Best Answer

This should get you started:
x = linspace(0, 25); % Create Data

y = sinc(x-10); % Create Data
[pks, locs] = findpeaks(y); % Peak Values & Locations
idxmax = locs(pks == max(pks)); % Highest Peak
zroidx = find(diff(y <= 0)); % Approximate Indices Of Zeros
[~,nearzros] = mink(abs(idxmax - zroidx),2); % Indirect Indices Of Two Closest To Peak
zros = zroidx(nearzros); % Actual Indices Of Two Closest To Peak
figure
plot(x, y)
hold on
plot(x(locs), pks, '^r') % Plot Peaks
plot(x(zros), y(zros), 'og') % Plot Approximate Zeros
hold off
grid
You will need to adapt this to your data. Make any necessary changes to get the result you want.