Plot power spectrum from FFT
17 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/963765/image.jpeg)
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')
0 Commenti
Risposte (1)
Samhitha
il 13 Feb 2025 alle 4:03
Hello,
To plot the power spectrum correctly, you need to make sure you are plotting the correct half of the FFT result.
For real-valued signals, the FFT output is symmetric. This means you only need the first half of the FFT result to analyze the positive frequencies.
To get the correct power spectrum, you should take the absolute value of the FFT, square it, and then scale it by the number of samples.
Here's how you can fix your code:
x = xlsread('testcode.xlsx');
fs = 100;
x = x';
n = length(x); % number of samples
y = fft(x); % compute the FFT
f = (0:n/2-1)*(fs/n); % frequency range for half the spectrum
power = abs(y(1:n/2)).^2/n; % power of the DFT for half the spectrum
plot(f, power)
xlabel('Frequency')
ylabel('Power')
For more details, please refer to the following MathWorks Documentation:
Hope this heps!
0 Commenti
Vedere anche
Categorie
Scopri di più su Spectral Measurements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!