MATLAB Answers

How to find the maximum of peaks ?

조회 수: 3(최근 30일)
Ramesh Bala
Ramesh Bala 2021년 8월 18일
댓글: Ramesh Bala 2021년 8월 19일
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');
for i= 1:1:300
s3= abs(fft( A.A1(:,i)));
subplot (3,2,1)
plot (s3((1:end/2)));
subplot (3,2,2)
plot ( (locSA{i}),(pkSA{i}));
title('Peaks ZT1');

채택된 답변

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');
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
  댓글 수: 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개)

Community Treasure Hunt

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

Start Hunting!

Translated by