DSP Matlab Lab Session-2
DSP Matlab Lab Session-2
Table of Contents
There are built-in functions available for computation of DFT and IDFT. These functions make use of FFT
algorithms, which are computationally highly efficient compared to direct computation of DFT and IDFT.
Functions described below are part of the MATLAB Signal Processing Toolbox. The FFT function in Matlab is
an algorithm published in 1965 by J.W.Cooley and J.W.Tuckey for efficiently calculating the DFT. [Ref.1]
[Ref.1] James W. Cooley and John W. Tukey, An Algorithm for the Machine Calculation of Complex Fourier
Series, Mathematics of Computation Vol. 19, No. 90, pp. 297-301, April 1965.
fft(X,N) is the N-point fft, padded with zeros if X has less than N points and truncated if it has more.
In MATLAB,
• For length N input vector x, the DFT is a length N vector X, with elements
n=1
k=1
1
ifft(X) is the inverse discrete Fourier transform of X.
x = [1 2 3 4];
y1 = fft(x)
y1 = 1×4 complex
10.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i
y2 = fft(x,8)
y2 = 1×8 complex
10.0000 + 0.0000i -0.4142 - 7.2426i -2.0000 + 2.0000i 2.4142 - 1.2426i
z1 = ifft (y1)
z1 = 1×4
1 2 3 4
z2 = ifft (y2)
z2 = 1×8
1.0000 2.0000 3.0000 4.0000 0.0000 -0.0000 0 -0.0000
dftmtx(N) is the N-by-N complex matrix of values around the unit-circle whose inner product with a column
vector of length N yields the discrete Fourier transform of the vector. If X is a column vector of length N, then
dftmtx(N)*X yields the same result as FFT(X); however, FFT(X) is more efficient.
D = dftmtx(4)
D = 4×4 complex
1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i 1.0000 + 0.0000i
1.0000 + 0.0000i 0.0000 - 1.0000i -1.0000 + 0.0000i 0.0000 + 1.0000i
1.0000 + 0.0000i -1.0000 + 0.0000i 1.0000 + 0.0000i -1.0000 + 0.0000i
1.0000 + 0.0000i 0.0000 + 1.0000i -1.0000 + 0.0000i 0.0000 - 1.0000i
which returns D = [1 1 1 1
1 -i -1 i
1 -1 1 -1
2
1 i -1 -i]
For illustration, we will take the example input sequence having length =4. Hence,
And
Then,
Xk = 1×4 complex
10.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 - 0.0000i -2.0000 - 2.0000i
xn = 1×4 complex
1.0000 + 0.0000i 2.0000 + 0.0000i 3.0000 - 0.0000i 4.0000 - 0.0000i
Xk_mat = x *dftmtx(4)
3
10.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i
xn_mat = Xk_mat*conj(dftmtx(N))/N
xn_mat = 1×4
1 2 3 4
Convolution
conv - Convolution and polynomial multiplication.
x = [2 1 2 1];
h = [1 2 3 4];
y = conv(x,h)
y = 1×7
2 5 10 16 12 11 4
C = cconv(A, B, N) circularly convolves vectors A and B. The resulting vector is length N. If omitted, N
defaults to LENGTH(A)+LENGTH(B)-1.
x = [2 1 2 1];
h = [1 2 3 4];
y = cconv(x,h,4)
y = 1×4
14 16 14 16
Let x[n] be the sequence of length l1, and h[n] be the sequence of length l2, then the linear convolution of the
two is,
For the linear convolution using DFT, define two new sequences,
and
4
Then, the result of circular convolving the sequences and produces same result as that of linear
convolution. For example, the linear convolution of the vectors x and h of the previous example can be
obtained as follows:
y_lc = cconv(x,h,7)
y_lc = 1×7
2.0000 5.0000 10.0000 16.0000 12.0000 11.0000 4.0000
Or simply using
y_lc = cconv(x,h)
y_lc = 1×7
2.0000 5.0000 10.0000 16.0000 12.0000 11.0000 4.0000
The result of circular convolution and linear convolution can be obtained using DFT as well. For achieving
linear convolution of the two sequences, use the steps provided in the Figure below.
Xk = fft(x,7);
Hk = fft(h,7);
Product_XH = Xk.*Hk;
Lin_conv = ifft(Product_XH,7)
Lin_conv = 1×7
2.0000 5.0000 10.0000 16.0000 12.0000 11.0000 4.0000
Xk = fft(x);
Hk = fft(h);
5
Product_XH = Xk.*Hk;
circular_conv = ifft(Product_XH)
circular_conv = 1×4
14 16 14 16
Let’s assume that the x[n] is the time domain cosine signal of frequency fm = 10 Hz that is sampled at a
frequency fs = 32*fm.
6
fs=32*fm;%sampling frequency with oversampling factor=32
t=0:1/fs:2-1/fs;%2 seconds duration
x=cos(2*pi*fm*t + pi/4);%time domain signal (real number)
plot(t,x); %plot the signal
title('x[n]=cos(2 \pi 10 t + \pi/4)'); xlabel('t=nT_s'); ylabel('x[n]');
Note: The FFT length should be sufficient to cover the entire length of the input signal. If N is less than the
length of the input signal, the input signal will be truncated when computing the FFT. In our case, the cosine
wave is of 2 seconds duration and it will have 640 points (a 10Hz frequency wave sampled at 32 times
oversampling factor will have 2*32*10 = 640 samples in 2 seconds of the record). Since our input signal is
periodic, we can safely use N = 256 point FFT.
Due to Matlab’s index starting at 1, the DC component of the FFT decomposition is present at index 1.
ans = 3.9053e-15
7
Note that the index for the raw FFT are integers from 1 --> N. We need to process it to convert these integers
to frequencies. That is where the sampling frequency counts. Each point or bin in the FFT output array is
spaced by the frequency resolution , that is calculated as,
where, fs is the sampling frequency and N is the FFT size that is considered. Thus, for our example, each
point in the array is spaced by the frequency resolution
The 10Hz cosine signal will register a spike at the 8th sample (10/1.25=8) - located at index 9.
ans = 1×3
0.0000 128.0000 0.0000
Therefore, from the frequency resolution, the entire frequency axis can be computed as
df = 1.2500
Now, plot the absolute value of the FFT against frequencies - the resulting plot is shown below. (Note that
scaling factor of 1/N can be used.)
8
In the above figure, we see that the frequency axis starts with DC, followed by positive frequency terms
which is in turn followed by the negative frequency terms. To introduce proper order in the x-axis, one can
use FFTshift function Matlab, which arranges the frequencies in order: negative frequencies --> DC -->
positive frequencies.
For even N, the original order returned by FFT is as follows (note: here, all indices corresponds to Matlab’s
index)
FFTshift shifts the DC component to the center of the spectrum. This process is illustrated as follows:
9
Figure 2 - Use of fftshift function
Following code snippet performs the shifting using both the manual method and using the Matlab’s in-built
command (fftshift).
10
Phase spectrum
The phase of the spectral components are computed as
One can use the MATLAB function angle for obtaining the phase value. angle(H) returns the phase angles,
in radians, of a matrix with complex elements. Using this function, it can be noted that the phase angles are
calculated and shown for every X values even when they are close to zero.
angle_x = angle(X2);
figure;
stem(f,angle_x,'r')
title('Phase angle plot'); xlabel('frequencies (f)'); ylabel('Phase angle');
11
Fixing this requires a tolerance value. If the magnitude is smaller than the tolerance, then we assume a zero
phase. This tolerance can be added as follows:
tolerance = 0.00001;
X3 = ceil(abs(X2) -tolerance);
X4 = round (X3 ./(X3+1)); %(X4 is the vector of 0s and 1s)
Angle_p = angle(X2).*X4;
figure;
stem(f,Angle_p,'r')
title('Phase angle plot'); xlabel('frequencies (f)'); ylabel('Phase angle');
12
% in terms of degrees
Angle_deg = Angle_p*180/pi;
figure;
stem(f,Angle_deg,'r')
title('Phase angle plot'); xlabel('frequencies (f)'); ylabel('Phase angle');
13
Usually function atan2 is used with unwrapping of phase values between , as follows:
14
Reconstructing the time domain signal from the frequency domain samples
Reconstruction of the time domain signal from the frequency domain sample is pretty straightforward. The
reconstructed signal, shown below, has preserved the same initial phase shift and the frequency of the
original signal. Note: The length of the reconstructed signal is only 256 sample long (0.8 seconds duration),
this is because the size of FFT is considered as N = 256. Since the signal is periodic it is not a concern. For
more complicated signals, appropriate FFT length (better to use a value that is larger than the length of the
signal) need to be used.
15
Verifying the total power in frequency domain
Here, the total power is verified by applying Discrete Fourier Transform (DFT) on the sinusoidal sequence.
The sinusoidal sequence is represented in frequency domain using Matlab’s FFT function. The
power associated with each frequency point is computed as
16
17