MATLAB: Plotting peaks and troughs of a large oscillating simulation data

MATLABplottingsignal processing

Hi there,
I'm trying the create something that looks like this
Based on my data (the top subplot) I've managed to get the peaks (bottom subplot) using the following code
time_tot = d(:,1);
displacement= d(:,2);
[Maxima,MaxIdx] = findpeaks(displacement);
time_max = time_tot([MaxIdx])
i=i+1 ;figure(i)
hold on
subplot(2,1,1)
plot(time_tot,displacement)
subplot(2,1,2)
plot(time_max,Maxima)
hold off
However, I'm not sure how to get the troughs. If anyone could help with this problem, then I would be greatly appreciative.
Best regards,
Lloyd

Best Answer

One option, if you are using findpeaks, is to negate it and then plot the negative of the peaks.
Example
t = linspace(0, 250*pi, 10000); % Create Data

s = sin(t) .* cos(t/50); % Create Data
figure
plot(t, s)
[pospks,posidx] = findpeaks(s); % Positive Peaks
[negpks,negidx] = findpeaks(-s); % Negative Peaks
figure
plot(t(posidx), pospks)
hold on
plot(t(negidx), -negpks)
hold off