Finding the main frequency after fft

10 ビュー (過去 30 日間)
Pawel Burzynski
Pawel Burzynski 2011 年 9 月 17 日
Hi,
I've got these blocks in simulink:
Sine Wave -> Buffer -> Matrix sum -> Matlab function.
I have to find the frequency of the input, so I wrote some code in Matlab function (by the http://www.mathworks.com/help/techdoc/ref/fft.html) :
{
function [freq, amp] = fcn(u)
%#codegen
Fs=2000;
T=1/Fs;
L=length(u);
t = (0:L-1)*T;
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(u,NFFT);
f = Fs/2*linspace(0,1,NFFT/2+1);
amp = abs(max(Y));
}
It's working properly, I'm getting the maximum value, but I can't get from code frequency of maxium value :/ how Can I get it? Thank You for response.
Best regards, Pawel

採用された回答

Wayne King
Wayne King 2011 年 9 月 17 日
Hi, You have to find the corresponding index in a frequency vector. For example:
Fs = 1e3;
t = 0:.001:1-0.001;
x = cos(2*pi*100*t)+randn(size(t));
xdft = fft(x);
[~,index] = max(abs(xdft(1:length(x)/2+1)));
freq = 0:(Fs/length(x)):Fs/2;
% freq(index)
fprintf('Maximum occurs at %2.3f Hz\n',freq(index))
Wayne

その他の回答 (1 件)

Pawel Burzynski
Pawel Burzynski 2011 年 9 月 17 日
It's excatly what I was looking for :) I had problems with those way of notation. I'm getting some mismatch, but I can handle this.
Thank You Wayne very much! :)
  3 件のコメント
Pawel Burzynski
Pawel Burzynski 2011 年 9 月 23 日
I've got the same problem, mainly i can't get the real frequency of the input event if it is pure sinusoidal source... There is always +/- 15% error. Can someone can help me?
Best regards
Wayne King
Wayne King 2011 年 9 月 23 日
Can you please provide code? It is hard to say the source of the error without a coded example.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpectral Measurements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by