MATLAB Answers

Single sided spectrum through FFT algorithm

63 views (last 30 days)
I was trying to perform spectral analyis of speech signal. But can anyone please explain the meaning of those three lines which I have highlighted in Bold font? I got this code from somewhere online.
I have also attached the screenshots of the output graph and other output details ffor your reference. Kindly go through them.
Please reply asap.
Thank you.
[data,fs] = audioread('BabyElephantWalk60.wav');
l = length(data);
NFFT = 2^nextpow2(l);
f = fs/2*linspace(0,1,NFFT/2+1);
xf = abs(fft(data, NFFT));
subplot(2,1,1);
plot(data);
title('Input Speech Signal');
subplot(2,1,2);
plot(f, xf(1:NFFT/2+1));
title('Single Sided Spectrum of the Speech Signal');

  0 Comments

Sign in to comment.

Accepted Answer

Devineni Aslesha
Devineni Aslesha on 21 Jul 2020
Hi Aishwarya,
Here is the explanation for the lines highlighted in bold.
1. f = fs/2*linspace(0,1,NFFT/2+1);
2. xf = abs(fft(data, NFFT)); Here, abs(fft(data, NFFT)); should be abs(fft(data, NFFT))/l; as the fft result is generally divided by the signal length in order to scale it to the same total power as the time-domain signal.
fft is a method used to transform from the time domain to the frequency domain and gives a complex result. Since, spectrun is the distribution of the amplitudes and phases of each frequency component against frequency, we are using abs function to get the amplitude of each frequency component.
3. plot(f, xf(1:NFFT/2+1)); Here, xf(1:NFFT/2+1) should be 2*xf(1:NFFT/2+1).
To compensate analytically for the notion that no negative frequencies exist, the symmetrical negative frequency components are truncated using xf(1:NFFT/2+1) and the positive frequency components are multiplied by two instead.

  3 Comments

Aishwarya Govekar
Aishwarya Govekar on 21 Jul 2020
First of all, I would like to thank you for giving such a detailed and a clear explaination. But still, I have few doubts...
f = fs/2*linspace(0,1,NFFT/2+1);
I have a doubt in the above line... why is the linspace range between 0 to 1. Does it mean that some normalisation is done? I'm sorry, I am average with respect to this concept. Please guide me.
plot(f, xf(1:NFFT/2+1)); Here, xf(1:NFFT/2+1) should be 2*xf(1:NFFT/2+1).
Here, 2*xf(1:NFFT/2+1), I didn't get when you said that the positive frequencies are multiplied by 2. I don't think we should multiply it by two. We want to just eliminate negative side of spectrum right? so xf(1:NFFT/2+1) --> this much will give our desired result I think...
Devineni Aslesha
Devineni Aslesha on 22 Jul 2020
Hi Aishwarya,
f = fs/2*linspace(0,1,NFFT/2+1);
For the above line, the answer is Yes. If you go through the link provided in the answer, the explanation is "The positive half of the spectrum (NFFT/2 +1 gives this, including 0 and nyquist, hence the +1) is mapped onto your real frequencies from the 'normalised frequency'.
plot(f, xf(1:NFFT/2+1)); Here, xf(1:NFFT/2+1) should be 2*xf(1:NFFT/2+1).
The positive frequencies are multiplied by 2 since Matlab FFT returns double-sided, half the input energy is in each the positive and negative frequencies. Here is a similar question that might help you to understand more.
Aishwarya Govekar
Aishwarya Govekar on 22 Jul 2020
Oh okay... Thanks a lot for such a clear explaination!

Sign in to comment.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!