필터 지우기
필터 지우기

How to find corresponding frequencies after viewing amplitude in descending order?

조회 수: 2 (최근 30일)
Good day. Below I have a piece of code written.
%% Peak detection Part
% In this part, we have defined a threshold value. So, all peaks above
% threshold value will be detected.
temp_fft = data_filt_ft;
% Normalize the FFT
temp_fft = temp_fft/max(temp_fft);
% threshold
th = 0.02; % i.e. 1% of maximum amplitude
temp_fft(temp_fft < th) = 0; %you can skip this step by commenting this.
% Plot FFT after thresholding
figure; plot(fr, temp_fft);
xlabel('Frequency(Hz)'); ylabel('Amplitude');
grid on;
% find peaks of the remaining data
[peak_ft, peak_loc] = findpeaks(temp_fft);
% frequency corresponding to peak
[peak_fr] = fr(peak_loc);
%Sort amplitude values into descending order
DescendAmp = sort(peak_ft,'descend');
% this fuction returns the fourier transform of the signal
function [ft, f] = fr_t(x, Fs)
L = length(x);
% NFFT = 2^nextpow2(L); % Next power of 2 from length of y
NFFT = L;
X = fft(x,NFFT)/NFFT;
f = Fs/2*linspace(0,1,floor(NFFT/2+1));
ft = abs(X(1:floor(NFFT/2+1)));
% ft = 20*log10(ft);
% plot(f,ft);
end
I am able to sort the amplitudes into descending order but I am unable to view the corresponding frequency values in that order. How can I view the corresponding frequency values in that order?

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 2월 26일
hello
I modifed a bit your code and tested it with a two tone signal
see the line with peak_fr_sorted_values
clc
%% dummy data
Fs = 1e3;
samples = 1e4;
t = (0:samples-1)'*1/Fs;
signal = 0.7*cos(2*pi*50*t)+0.4*cos(2*pi*100*t)+0.1*randn(samples,1);
[data_filt_ft, fr] = fr_t(signal, Fs);
%% Peak detection Part
% In this part, we have defined a threshold value. So, all peaks above
% threshold value will be detected.
temp_fft = data_filt_ft;
% Normalize the FFT
temp_fft = temp_fft/max(temp_fft);
% threshold
th = 0.02; % i.e. 1% of maximum amplitude
temp_fft(temp_fft < th) = 0; %you can skip this step by commenting this.
% Plot FFT after thresholding
figure; plot(fr, temp_fft);
xlabel('Frequency(Hz)'); ylabel('Amplitude');
grid on;
% find peaks of the remaining data
[peak_ft, peak_loc] = findpeaks(temp_fft);
%Sort amplitude values into ascending order
[peak_ft_sorted_values,peak_ft_sorted_index] = sort(peak_ft,'ascend');
% frequency corresponding to peak
[peak_fr] = fr(peak_loc);
% and now sorted according to amplitude (above)
peak_fr_sorted_values = peak_fr(peak_ft_sorted_index)
% this fuction returns the fourier transform of the signal
function [ft, f] = fr_t(x, Fs)
L = length(x);
% NFFT = 2^nextpow2(L); % Next power of 2 from length of y
NFFT = L;
X = fft(x,NFFT)/NFFT;
f = Fs/2*linspace(0,1,floor(NFFT/2+1));
ft = abs(X(1:floor(NFFT/2+1)));
% ft = 20*log10(ft);
% plot(f,ft);
end
  댓글 수: 3

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

dpb
dpb 2021년 2월 26일
[DescendAmp,idx] = sort(peak_ft,'descend');
disp([peak_fr(idx) peak_ft])

sugga singh
sugga singh 2021년 2월 28일
thanks guys. was looking for it.

카테고리

Help CenterFile Exchange에서 Parametric Spectral Estimation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by