# What is diff. b/w fft and pspectrum command in Matlab?

3 views (last 30 days)
Awais on 28 May 2023
What is difference between fft and pspectrum command in Matlab? What is the difference between output of both commands when applied to time domain? As both give frequency domain plot.

Star Strider on 28 May 2023
If you only want to plot the magnitude spectrum, use the fft function.
The differences between the various functions are significant, as well as the information they provide.
The fft function produces a two-sided (uncentred) Fourier magnitude transform without windowing it. The result has the units of the original signal amplitude. It does not independently calculate (or return) an associated frequency vector.
The pspectrum function computes the adaptive-windowed power spectrum (amplitude²) and also calculates and outputs an associated one-sided frequency vector. The details are presented in Spectrum Computation, so I will let you read that discussion on your own.
The pspectrum function also differs from the spectrogram function in the way it calculates and presents it results. The pspectrum function with the 'spectrogram' option calculates the power spectrum as a function of time, while spectrogram (and periodogram) calculate the power spectral density as a function of time. The power spectrum and power spectral density are not the same, and the units are not the same. (For this reason, I generally prefer pspectrum for what I do, however that is simply a personal choice, based on the frequency analyses that I need to do.)
I encourage you to explore the documentation to understand the differences between the various functions.

Sulaymon Eshkabilov on 28 May 2023
Awais on 28 May 2023
I am a begginer user of MATLAB and did not got the point discussed there? I just want to know that if i want to plot frequency domain from a time domain audio signal then what command from these two should i use?

Sulaymon Eshkabilov on 29 May 2023
Note that pspectrum() has an advatange that it can get twim and frequency diomain analysis in one graph that migh have of a good value for data analysis. Here is one example on fft() and pspectrum(). You can choose which one to use in your exercise (s):
t = linspace(0, 1, 1000); % Time signal
fs = 1/t(2); % Sampling frequency
L = length(t); % Length of the time signal
% Signal with some noise is created (if you have data, that should be used):
x = 2*sin(2*pi*t*50)+1.5*cos(2*pi*t*120)+1.5*cos(2*pi*t*250)+1.25*cos(2*pi*t*375)+0.05*randn(size(t));
% FFT() of the time domain signal x(t) is computed:
X_fft = fft(x);
% Absolute value (Amplitude) of the computed FFT is computed:
P1 = abs(X_fft(1:L/2+1)/L);
% Frequency of the signal is computed:
f = fs*(0:L/2)/L;
figure()
plot(f,P1, 'r', 'LineWidth',2)
xlabel('frequency, [Hz]')
ylabel('|X|')
grid on
title('One-sided Amplitude spectrum')
% PSPECTRUM() is computed of x(t) signal
[p,f,t] = pspectrum(x,fs,'spectrogram');
figure('Name', 'Time-Freq Analysis')
waterfall(f,t,p')
xlabel('Frequency (Hz)')
ylabel('Time (seconds)')
wtf = gca;
wtf.XDir = 'reverse';
view([30 45])

### Categories

Find more on Spectral Measurements in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!