How to find the maximum of peaks ?

7 次查看(过去 30 天)
I want to find the first maximum 4 peaks in loop.My code finds all the peaks,how can I find only 4 and plot them ?
A= load ('ZT2.mat');
figure
for i= 1:1:300
s3= abs(fft( A.A1(:,i)));
subplot (3,2,1)
plot (s3((1:end/2)));
subplot (3,2,2)
[pkSA{i},locSA{i}]=findpeaks(s3);
plot ( (locSA{i}),(pkSA{i}));
title('Peaks ZT1');
drawnow
pause(0.1)
end

采纳的回答

Yazan
Yazan 2021-8-18
Below, I am proposing an implementation of what I understood to be your task. You have to experiment with the findpeaks multiple options to find a setting good for your data.
clc, clear
load ('ZT2.mat');
figure
fftData = abs(fft(A1, [], 2));
N = size(fftData, 2);
fftData = [fftData(:, 1:N/2), fftData(:, 1)];
freq = linspace(0, 0.5, N/2+1);
locSA = cell(1, size(A1, 1));
pkSA = cell(1, size(A1, 1));
Np = 4;
sortP = 'descend';
minPeakDis = 1;
for i=1:size(fftData,1)
[pkSA{i}, locSA{i}] = findpeaks(fftData(i,:), 'NPeaks', 4, 'SortStr', sortP, ...
'MinPeakDistance', minPeakDis);
subplot (1, 2, 1)
plot(freq, fftData(i,:)); xlabel('Normalized frequency')
ylabel('Amplitude'), title('Positive spectrum')
hold on, plot(freq(locSA{i}), pkSA{i}, 's', 'MarkerSize', 5, 'MarkerEdgeColor', 'g',...
'MarkerFaceColor', 'g'); grid minor, hold off
subplot (1, 2, 2)
stem(freq(locSA{i}), pkSA{i}, 'Marker', 's', 'MarkerSize', 7, 'MarkerEdgeColor', 'g',...
'MarkerFaceColor', 'g'); xlabel('Normalized frequency')
ylabel('Amplitude'), title('Spectral peaks locations'), grid minor
drawnow
pause(1)
end
  1 个评论
Ramesh Bala
Ramesh Bala 2021-8-19
This is perfect .Thanks Yazan
this the key that I was expecting especially 'SortStr', sortP, ...
'MinPeakDistance', minPeakDis

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by