fft basics
hi every one,
i would like to share some of the uses of fft for spectrum analysis.
Hope it will be useful for those who are novice to MATLAB programming.
DFT Notes:
DFT produces a discrete frequency domain representation.Also, DFT is only defined
in the region between 0 and Fs.
(as we know, One period extends from f = 0 to Fs, where Fs is the sampling frequency.)
When the region between 0 and Fs is examined, it can be seen that there is even
symmetry around the center point, 0:5Fs, the Nyquist frequency.
This symmetry adds redundant information.(the data between 0:5fs and fs is a mirror
image of the data between 0 and 0:5Fs.)
Matlab's FFT function is an effective tool for computing the discrete Fourier transform
of a signal. The typical syntax for computing the FFT of a signal is fft(x,N) where x is the
signal, x[n], you wish to transform, and N is the number of points in the FFT.
N must be at least as large as the number of samples in x[n].
Example:1
To demonstrate the effect of changing the value of N,
sythesize a cosine with 30 samples at 10 samples per period.
n = [0];
x = cos(2*pi*n/10);
Define 3 different values for N.
The abs function in MATLAB finds the magnitude of the transform,
N1 = 64;
N2 = 128;
N3 = 256;
X1 = abs(fft(x,N1));
X2 = abs(fft(x,N2));
X3 = abs(fft(x,N3));
to plot the magnitude of the transform we need to scale the freq.
The frequency scale begins at 0 and extends to N-1 for an N-point FFT.
We then normalize the scale so that it extends from 0 to [ 1-(1/N) ] .
F1 = [0 : N1 - 1]/N1;
F2 = [0 : N2 - 1]/N2;
F3 = [0 : N3 - 1]/N3;
subplot(3,1,1)
plot(F1,X1,'-x'),title('N = 64'),axis([0 1 0 20])
subplot(3,1,2)
plot(F2,X2,'-x'),title('N = 128'),axis([0 1 0 20])
subplot(3,1,3)
plot(F3,X3,'-x'),title('N = 256'),axis([0 1 0 20])
Inference:
upon examining the plot, one can see each of the transforms adheres to
the same shape, differing only in the number of samples used
to approximate that shape. In the plot one can see
the magnitude of 2 sincs with the center of the first sinc function at 0.1Fs and the
second at 0.9Fs. (sincethe data between 0:5fs and fs is a mirror image of the data
between 0 and 0:5Fs, here in this code we normalize the freq axisi.e., x-axis to 1.)
Example 2:
In the the last example the length of x[n] was limited to 3 periods in length.
Now, let's choose a large value for N (for a transform with many points), and vary
the number of repetitions of the fundamental period.
n = [0];
x1 = cos(2*pi*n/10); % 3 periods
x2 = [x1 x1]; % 6 periods
x3 = [x1 x1 x1]; % 9 periods
N = 2048; %no. of fft points
% fft magnitude depends on fft points
X1 = abs(fft(x1,N));
X2 = abs(fft(x2,N));
X3 = abs(fft(x3,N));
F = [0:N-1]/N;
subplot(3,1,1)
plot(F,X1),title('3 periods'),axis([0 1 0 50])
subplot(3,1,2)
plot(F,X2),title('6 periods'),axis([0 1 0 50])
subplot(3,1,3)
plot(F,X3),title('9 periods'),axis([0 1 0 50])
Inference:
The first plot, the transform of 3 periods of a cosine,
looks like the magnitude of 2 sincs with the center
of the first sinc at 0:1fs and the second at 0:9fs.
The second plot also has a sinc-like appearance,
but its frequency is higher and it has a larger
magnitude at 0:1fs and 0:9fs. Similarly, the third plot
has a larger sinc frequency and magnitude.
As x[n] is extended to an large number of periods,
the sincs will begin to look more and more like
impulses.
why sinc function looks more like impulse when x[n]
is extended to an large no. of periods?
Reason:
When Matlab computes the FFT, it automatically fills the
spaces from n = 30 to n = 2047 with zeros. This is like taking a
sinusoid and mulitipying it with a rectangular box of length 30.
A multiplication of a box and a sinusoid in the time domain should
result in the convolution of a sinc with impulses in the frequency domain.
Furthermore, increasing the width of the box in the time domain
should increase the frequency of the sinc in the frequency domain.
Spectrum Analysis with the FFT and Matlab
The FFT does not directly give you the spectrum of a signal.
the FFT can vary dramatically depending on the number of points (N) of the FFT,
and the number of periods of the signal that are represented.
The FFT contains information between 0 and fs, however, we know
that the sampling frequency must be at least twice the highest frequency component.
Therefore, the signal's spectrum should be entirly below fs/2 , the Nyquist frequency.
a real signal should have a transform magnitude that is symmetrical for for positive
and negative frequencies. So instead of having a spectrum that goes from 0 to fs,
it would be more appropriate to show the spectrum from -fs/2 to fs/2 .
This can be accomplished by using Matlab's fftshift function
as the following code demonstrates.
n = [0]; % 150 samples
x1 = cos(2*pi*n/10); % 15 periods i.e.,each period of 10 samples
N = 2048;
X = abs(fft(x1,N));
X = fftshift(X);
F = [-N/2:N/2-1]/N; % so that freq range is between -0.5fs to 0.5fs
plot(F,X),
xlabel('frequency / f s')
happy learning.
Added after 7 minutes:
Iam a beginner yet.
All are welcome if one like to drop some useful info regarding fft basics, spectrum analysis, zero padding, autocorrelation, PSD etc., in MATLAB.
At present i concentrate on fft basics and plots of PSD Vs freq
(freq analysis) for a while and then to zero padding.
Can any one specify popular site or pdf regarding fft
Thanks in advance.