I want a FFT plot of a .mat file at 8000Hz (in 3 seconds) to find the dominant frequencies but I'm not getting an peaks. Whats wrong with the codes?Thank you very much for any help!

2 ビュー (過去 30 日間)
if true
% code
end
Here is the code I have but getting no peaks:
Fs = 8000; % Sampling frequency.
t = 0:1/Fs:3-1/Fs;
x = cos(2*pi*1000*t)+rand(size(t));
N = length(x);
xdft = fft(x);
xdft = xdft(1:N/2+1);
psdx = (1/(Fs*N)) * abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
freq = 0:Fs/length(x):Fs/2;
plot(freq,10*log10(psdx)) grid on title('Periodogram Using FFT') xlabel('Frequency (Hz)') ylabel('Power/Frequency (dB/Hz)')
  2 件のコメント
Star Strider
Star Strider 2018 年 4 月 1 日
When I run your code, I see the expected peak at 1 kHz.
Kristy Kwok
Kristy Kwok 2018 年 4 月 1 日
Hi Star, Thank you for your reply. I wasnt sure if my coding was correct. The peak at 1000Hz is because I put 1000 in this line: x = cos(2*pi*1000*t)+rand(size(t)); I have tried to put 800 and the peak will be at 800Hz. I am not sure how to confirm if the code is correct.

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

採用された回答

Star Strider
Star Strider 2018 年 4 月 1 日
My pleasure.
Your code is correct if it produces the result you expect. You have calculated the ‘freq’ frequency vector correctly. I slightly revised your code to plot three different frequencies, at 800, 1000, and 1200 Hz. You can see all of them clearly in the plot (and you can add as many frequencies to the ‘Fvct’ vector as you want):
Fs = 8000; % Sampling frequency.
t = 0:1/Fs:3-1/Fs;
Fvct = [800 1000 1200];
x = cos(2*pi*Fvct(:)*t);
x = sum(x) + rand(size(t));
N = length(x);
xdft = fft(x);
xdft = xdft(1:N/2+1);
psdx = (1/(Fs*N)) * abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
freq = 0:Fs/length(x):Fs/2;
plot(freq,10*log10(psdx))
grid on
title('Periodogram Using FFT')
xlabel('Frequency (Hz)')
ylabel('Power/Frequency (dB/Hz)')
A slightly different implementation is given in the R2015a documentation on the fft (link) function.
  10 件のコメント
Kristy Kwok
Kristy Kwok 2018 年 4 月 1 日
I cannot thank you enough Star! Thank you very much for all your help. Have a great week ahead! :D
Star Strider
Star Strider 2018 年 4 月 1 日
As always, my pleasure!
I hope you do as well!

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by