Reduce the number of peaks from findpeaks command?

9 views (last 30 days)
My code does a few things: it averages data, smooths it, finds the peaks in the data, and then calculates the damping ratio of the data.
I have wayyyy too many peaks. I only want/need about 5 peaks--circled in blue. Right now, my plot looks like this:
How can I do this?
Thanks in advance!
clear
close
clc
load data
v_data = data;
v_mean = mean(v_data,2)'; %getting mean
v_std = std(v_data, 0, 2)'; %getting standard deviation
v_smooth = smooth(v_mean); %smoothing out noisy data
t = v_data(:,1); %picking off a time vector
ind_1 = 1:length(t);
t = t(ind_1);
v = v_smooth(ind_1);
ind_2 = 1:length(t);
[ pks, locs] = findpeaks(v(ind_2));
n_peaks = numel(locs);
%---Calculate Logarithmic Decrement and damping ratio
log_dec = zeros(length(n_peaks));
for nn = 1:n_peaks-1
log_dec(nn)= log(pks(nn)/pks(nn+1)); % computed with n = 1 periods apart
end
mean_dec = mean(log_dec); %average decrement
%---damping ratio---
damp_ratio = 1/sqrt(1+((2*pi/(mean_dec))^2))
pf = polyfit(t(locs), log(pks), 3);
curve_fit = exp(polyval(pf,t));
figure()
axes
plot(t, v_data(:,2), 'Displayname', 'Data 1'); hold on;
plot(t, v_data(:,3), 'Displayname', 'Data 2');
plot(t, v_data(:,4), 'Displayname', 'Data 3');
plot(t, v_data(:,5), 'Displayname', 'Data 4');
plot(t, v_data(:,6), 'Displayname', 'Data 5');
plot(t(locs), pks, 'rd', 'markersize', 10, 'displayname', 'Peaks'); hold on;
plot(t, v_smooth, 'k', 'linewidth', 1.5, 'Displayname', 'Smoothed Average Data');
plot( t, curve_fit,'g-.','LineWidth', 1.5, 'Displayname', 'Peak Envelope');
grid minor
title('Data: 1 - 5');
legend('location', 'ne');
xlabel('Time (s)'); ylabel('Velocity (m/s)');

Accepted Answer

Star Strider
Star Strider on 25 Mar 2024
Edited: Star Strider on 25 Mar 2024
The findpeaks function has a number of name-value pair arguments that you can use. The two I use most often are 'MinPeakProminence' and 'MinPeakDistance'. The first selects the peaks based on their relation to the signal levels around them, and the second does not return peaks within a specified distance of identified peaks. One or botth of those (and possibly others) should do what you want.
load('data.mat')
% whos
v_data = data;
v_mean = mean(v_data,2)'; %getting mean
v_std = std(v_data, 0, 2)'; %getting standard deviation
v_smooth = smooth(v_mean); %smoothing out noisy data
t = v_data(:,1); %picking off a time vector
ind_1 = 1:length(t);
t = t(ind_1);
v = v_smooth(ind_1);
ind_2 = 1:length(t);
[ pks, locs] = findpeaks(v(ind_2), 'MinPeakProminence',7.5, 'MinPeakDistance',280);
n_peaks = numel(locs);
%---Calculate Logarithmic Decrement and damping ratio
log_dec = zeros(length(n_peaks));
for nn = 1:n_peaks-1
log_dec(nn)= log(pks(nn)/pks(nn+1)); % computed with n = 1 periods apart
end
mean_dec = mean(log_dec); %average decrement
%---damping ratio---
damp_ratio = 1/sqrt(1+((2*pi/(mean_dec))^2))
damp_ratio = 0.0624 + 0.1839i
pf = polyfit(t(locs), log(pks), 3);
curve_fit = exp(polyval(pf,t));
figure()
axes
plot(t, v_data(:,2), 'Displayname', 'Data 1'); hold on;
plot(t, v_data(:,3), 'Displayname', 'Data 2');
plot(t, v_data(:,4), 'Displayname', 'Data 3');
plot(t, v_data(:,5), 'Displayname', 'Data 4');
plot(t, v_data(:,6), 'Displayname', 'Data 5');
plot(t(locs), pks, 'rd', 'markersize', 10, 'displayname', 'Peaks'); hold on;
plot(t, v_smooth, 'k', 'linewidth', 1.5, 'Displayname', 'Smoothed Average Data');
plot( t, curve_fit,'g-.','LineWidth', 1.5, 'Displayname', 'Peak Envelope');
Warning: Imaginary parts of complex X and/or Y arguments ignored.
grid minor
title('Data: 1 - 5');
legend('location', 'ne');
xlabel('Time (s)'); ylabel('Velocity (m/s)');
I do not understand your data, and apparently the smoothing steps interact with the returned peaks. This is an improvement, however you will need to continue experimenting yourself to get the results you want.
Meanwhile see: How to filter noise from time-frequency data and find natural frequency of a cantilever? for one approach to fitting this sort of problem.
.

More Answers (0)

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!