Reduce the number of peaks from findpeaks command?
Ältere Kommentare anzeigen
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)');
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Descriptive Statistics finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
