Plot power spectrum from FFT

6 views (last 30 days)
Yu Zhi
Yu Zhi on 14 Apr 2022
Commented: VBBV on 14 Apr 2022
Hi,
I want to plot power spectrum using FFT. I use the below code but I didn't get the spectrum and instead I got the below graph.
Please advise where my code is wrong. Thanks
x = xlsread('testcode.xlsx'); fs = 100; x = x';
y = fft(x);
n = length(x); % number of samples
f = (0:n-1)*(fs/n); % frequency range
power = abs(y).^2/n; % power of the DFT
plot(f,power)
xlabel('Frequency')
ylabel('Power')

Answers (1)

VBBV
VBBV on 14 Apr 2022
power = abs(y).^2/(fs*n); % power of the DFT
plot(f,power)
Divide with fs sampling frequency and plot it
  1 Comment
VBBV
VBBV on 14 Apr 2022
nfft = 2^nextpow2(length(x));
y = fft(x,nfft);
power = abs(y).^2/(fs*n); % power of the DFT
plot(f,power)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by