How to find the maximum of peaks ?

3 views (last 30 days)
Ramesh Bala
Ramesh Bala on 18 Aug 2021
Commented: Ramesh Bala on 19 Aug 2021
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

Accepted Answer

Yazan
Yazan on 18 Aug 2021
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 Comment
Ramesh Bala
Ramesh Bala on 19 Aug 2021
This is perfect .Thanks Yazan
this the key that I was expecting especially 'SortStr', sortP, ...
'MinPeakDistance', minPeakDis

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!