378 views (last 30 days)

hello .

i tried to find a code for ploting fft,and i found this code which plot single-sided amplitude spectrum according to it's title,is it the same as fft?what is different between single side amplitude spectrum and fft? and is this code ok?

i really appreciate it if you help me.

i have a signal with 135 data point.

Fs = 50; % Sampling frequency T = 1/Fs; % Sample time L = 135; % Length of signal

NFFT = 2^(nextpow2(L)-1);

x=zeros(NFFT,1); x(1:NFFT,1) = rawdata(1:NFFT,1);

YY = fft(x,NFFT)/L; ff = Fs/2*linspace(0,1,NFFT/2+1);

% Plot single-sided amplitude spectrum. plot(ff,2*abs(YY(1:NFFT/2+1))) title('Single-Sided Amplitude Spectrum of tp') xlabel('Frequency (Hz)') ylabel('|Y(f)|')

Adam
on 15 Dec 2014

Edited: Adam
on 15 Dec 2014

That code uses the fft so yes, it is the same thing.

The fft is a transform which gives you a complex result spanning negative and positive frequencies ( -nyquist to +nyquist ). The single-side spectrum throws away the negative frequencies which are often not required for things like plotting spectrum to obtain a single-side spectrum.

So the fft is the method used to transform from the time domain to the frequency domain, but its result requires some manipulation afterwards to obtain what is often the desired result, a single-sided power spectrum.

The code:

ff = Fs/2*linspace(0,1,NFFT/2+1);

is taking the positive half of the spectrum (NFFT/2 +1 gives this, including 0 and nyquist, hence the +1) and mapping it onto your real frequencies from 'normalised frequency'.

Adam
on 16 Dec 2014

I don't know what the signal in your original question is, but it is presumably sampled at 50Hz sample rate, i.e. every 20ms.

If you don't know your sample rate than you cannot get information out of the fft in Hz, all you can really do is either guess at the sample frequency (seems rather pointless) or use normalised frequency - i.e. fs = 1 where your frequency values will be between 0 and 0.5 for the positive have of your spectrum.

Xu LinaM
on 1 Mar 2017

Adam
on 1 Mar 2017

Charanraj
on 9 Jul 2015

Opportunities for recent engineering grads.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/166697-plot-single-sided-amplitude-spectrum#comment_403797

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/166697-plot-single-sided-amplitude-spectrum#comment_403797

Sign in to comment.