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

21 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Star Strider
Star Strider el 9 de Oct. de 2020
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.
  2 comentarios
Blenndrman
Blenndrman el 9 de Oct. de 2020
Thanks, it definitely works and does what I'd like. I do need to figure out how to adapt it, but that will be my challenge!
Star Strider
Star Strider el 9 de Oct. de 2020
As always, my pleasure!
The important part is likely picking the appropriate peaks. One way I have found that generally works well is to use the MinPeakProminence name-value pair. After that, loop through the identified peak indices, and use each one for ‘idxmax’ in each iteration. The rest of the code should then work automatically to identify the zeros.
One potential problem might be if there is an identified peak near either end of the vector, and no zero-crossing on one side of the peak. That solution would be to use the first or last index of the vector (as appropriate) as the identified zero.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by