0% found this document useful (0 votes)
6 views

LAB 10 (1)

Uploaded by

Be Educate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

LAB 10 (1)

Uploaded by

Be Educate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Implementation of FFT and IFFT on

MATLAB Lab 10

Name: Konain Mazhar Reg#CPEN-211101045

Instructor: Engr. Sir Aqeel Subject: DSP

Signature:______________ Date:

Introduction:
The FFT is a very efficient algorithm technique based on the DFT but with fewer computations
required. The FFT is one of the most used operations in DSP to provide a frequency spectrum
analysis. Two different procedures are used to compute an FFT: the decimation-in-frequency and
the decimation-in-time. Several variants of the FFT have been used, such as the Wino grad
transform, the discrete cosine transforms (DCT), and the discrete Hartley transform. Transform
methods such as the DCT have become increasingly popular in recent years, especially for real-
time systems. They provide a large compression ratio.

DEVELOPMENT OF THE FFT ALGORITHM WITH RADIX-2:


The FFT reduces considerably the computational requirements of the DFT. The DFT of a
discrete-time signal x(nT) is

(1)

where the sampling period T is implied in x(n) and N is the frame length. The constants W are
referred to as twiddle constants or factors, which represent the phase, or

(2)
and are a function of the length N. Equation (1) can be written for k = 0, 1, . . . , N - 1, as

(3)
This represents a matrix of N x N terms, since X(k) needs to be calculated for N values for k.
Since (3) is an equation in terms of a complex exponential, for each specific k there are (N - 1)
complex additions and N complex multiplications. This results in a total of (N2- N) complex
additions and N2 complex multiplications. Hence, the computational requirements of the DFT
can be very intensive, especially for large values of N. FFT reduce computational complexity
from N2 to N log N.

The FFT algorithm takes advantage of the periodicity and symmetry of the twiddle constants to
reduce the computational requirements of the FFT. From the periodicity of W,

(4)
and from the symmetry of W,

(5)
Figure below illustrates the properties of the twiddle constants W for N = 8. For example, let k =
2, and note that from (4), W10 = W2, and from (5), W6 = -W2
For a radix-2 (base 2), the FFT decomposes an N-point DFT into two (N/2)-point or smaller
DFTs. Each (N/2)-point DFT is further decomposed into two (N/4)-point DFTs, and so on. The
last decomposition consists of (N/2) two-point DFTs. The

Smallest transform is determined by the radix of the FFT. For a radix-2 FFT,N must be a power
or base of 2, and the smallest transform or the last decomposition is the two-point DFT. For a
radix-4, the last decomposition is a four-point DFT.
DECIMATION-IN-FREQUENCY FFT ALGORITHM WITH RADIX-2

Let a time-domain input sequence x(n) be separated into two halves:

(6)

(7)
Taking the DFT of each set of the sequence in (6) and (7) gives us

(8)
Let n = n + N/2 in the second summation of (8); X(k) becomes

(9)
where WkN/2 is taken out of the second summation because it is not a function of n. Using

in (9), X(k) becomes

(10)
Because (-1)k = 1 for even k and -1 for odd k, (10) can be separated for even and odd k, or
1. For even k

(11)
2. For odd k
(12)
Substituting k = 2k for even k, and k = 2k + 1 for odd k, (11) and (12) can be written for k = 0, 1,
...

(N/2) - 1 as

(13)

(14)
Because the twiddle constant W is a function of the length N, it can be represented as WN. Then
W2N
can be written as WN/2. Let

(15)

(16)
Equations (13) and (14) can be written more clearly as two (N/2)-point DFTs, or

(17)

Impulse response of FFT:


Program :
%To compute the FFT of the impulse sequence and plot magnitude and phase
response
clc;
clear all;
close all;
%impulse sequence
t=-2:1:2;
y=[zeros(1,2) 1 zeros(1,2)];
subplot (3,1,1);
stem(t,y);
grid;
input('y=');
disp(y);
title ('Impulse Response');
xlabel ('time -->');
ylabel ('--> Amplitude');
xn=y;
N=input('enter the length of the FFT sequence: ');
xk=fft(xn,N);
magxk=abs(xk);
angxk=angle(xk);
k=0:N-1;
subplot(3,1,2);
stem(k,magxk) ;
grid;

Output:
y=[1 2 3 4 5]
0 0 1 0 0

enter the length of the FFT sequence: 5


Columns 1 through 4

1.0000 + 0.0000i -0.8090 - 0.5878i 0.3090 + 0.9511i 0.3090 - 0.9511i

Column 5

-0.8090 + 0.5878i

Plot:
Impulse Response
1
--> Amplitude

0.5

0
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
time -->
1
|x(k)|

0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4
k
5
arg(x(k))

-5
0 0.5 1 1.5 2 2.5 3 3.5 4
k

Step sequence of FFT:


Program :
%To compute the FFT of the step sequence and plot magnitude and phase response
clc;
clear all;
close all;
%Step Sequence
s=input ('enter the length of step sequence');
t=-s:1:s;
y=[zeros(1,s) ones(1,1) ones(1,s)];
subplot(3,1,1);
stem(t,y);
grid
input('y=');
disp(y);
title ('Step Sequence');
xlabel ('time -->');
ylabel ('--> Amplitude');
xn=y;
N=input('enter the length of the FFT sequence: ');
xk=fft(xn,N);
magxk=abs(xk);
angxk=angle(xk);
k=0:N-1;
subplot(3,1,2);
stem(k,magxk);
grid
xlabel('k');
ylabel('|x(k)|');
subplot(3,1,3);
stem(k,angxk);
disp(xk);
grid
xlabel('k');
ylabel('arg(x(k))');

Output:
enter the length of step sequence5
y=[1 2 3 4 5]
0 0 0 0 0 1 1 1 1 1 1

enter the length of the FFT sequence: 10


Columns 1 through 4
5.0000 + 0.0000i -1.0000 + 3.0777i 0.0000 + 0.0000i -1.0000 + 0.7265i
Columns 5 through 8
0.0000 + 0.0000i -1.0000 + 0.0000i 0.0000 + 0.0000i -1.0000 - 0.7265i
Columns 9 through 10
0.0000 + 0.0000i -1.0000 - 3.0777i

Plot:
Step Sequence
1
--> Amplitude

0.5

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
time -->
6

4
|x(k)|

0
0 1 2 3 4 5 6 7 8 9
k
5
arg(x(k))

-5
0 1 2 3 4 5 6 7 8 9
k

Ramp sequence of FFT:


Program:
%To compute the FFT of the Ramp sequence and plot magnitude and phase response
clc;
clear all;
close all;
%Ramp Sequence
s=input ('enter the length of Ramp sequence: ');
t=0:s;
y=t
subplot(3,1,1);
stem(t,y);
grid
input('y=');
disp(y);
title ('ramp Sequence');
xlabel ('time -->');
ylabel ('--> Amplitude');
xn=y;
N=input('enter the legth of the FFT sequence: ');
xk=fft(xn,N);
magxk=abs(xk);
angxk=angle(xk);
k=0:N-1;
subplot(3,1,2);
stem(k,magxk);
grid
xlabel('k');
ylabel('|x(k)|');
subplot(3,1,3);
stem(k,angxk);
disp(xk);
grid
xlabel('k');
ylabel('arg(x(k))');

Output:
enter the length of Ramp sequence: 5

y=

0 1 2 3 4 5

y=10
0 1 2 3 4 5

enter the legth of the FFT sequence: 10


Columns 1 through 8

15.0000 + 0.0000i -7.7361 - 7.6942i 2.5000 + 3.4410i -3.2639 - 1.8164i 2.5000 + 0.8123i -3.0000 + 0.0000i
2.5000 - 0.8123i -3.2639 + 1.8164i

Columns 9 through 10

2.5000 - 3.4410i -7.7361 + 7.6942i

Plot:
ramp Sequence
6
--> Amplitude 4

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time -->
15

10
|x(k)|

0
0 1 2 3 4 5 6 7 8 9
k
5
arg(x(k))

-5
0 1 2 3 4 5 6 7 8 9
k

Exponential sequence of FFT:


Program:
%To compute the FFT of the Exponential sequence and plot magnitude and phase
response
clc;
clear all;
close all;
%exponential sequence
n=input('enter the length of exponential sequence: ');
t=0:1:n;
a=input('enter "a" value: ');
y=exp(a*t);
input('y=')
disp(y);
subplot(3,1,1);
stem(t,y);
grid;
title('exponential response');
xlabel('time');
ylabel('amplitude');
disp(y);
xn=y;
N=input('enter the length of the FFT sequence: ');
xk=fft(xn,N);
magxk=abs(xk);
angxk=angle(xk);
k=0:N-1;
subplot(3,1,2);
stem(k,magxk);
grid;
xlabel('k');
ylabel('|x(k)|');
subplot(3,1,3);
stem(k,angxk);
grid;
disp(xk);
xlabel('k');
ylabel('arg(x(k))');

Output:
enter the length of exponential sequence: 4
enter "a" value: 1
y=[1 2 3 4 5]

ans =

1 2 3 4 5

1.0000 2.7183 7.3891 20.0855 54.5982

1.0000 2.7183 7.3891 20.0855 54.5982

enter the length of the FFT sequence: 10


Columns 1 through 4

85.7910 + 0.0000i -44.8951 -59.8196i -3.5157 +56.8035i 27.3034 -38.3620i

Columns 5 through 8

-36.8799 +18.4192i 40.1834 + 0.0000i -36.8799 -18.4192i 27.3034 +38.3620i

Columns 9 through 10

-3.5157 -56.8035i -44.8951 +59.8196i

Plot:
exponential response
60
amplitude
40

20

0
0 0.5 1 1.5 2 2.5 3 3.5 4
time
100
|x(k)|

50

0
0 1 2 3 4 5 6 7 8 9
k
5
arg(x(k))

-5
0 1 2 3 4 5 6 7 8 9
k

Implementation of IFFT:
Program:
clear all
F0 = 4;
A = 1;
Fs = 100;
Ts=1/Fs;
t=0:Ts:1-Ts;
x=A*sin(2*pi*F0*t);
subplot(311)
plot(t,x)
grid on
xlabel('t')
ylabel('x(t)')
title('original signal of frequency 4HZ')

X_IFFT=ifft(x)
magX_IFFT=abs(X_IFFT)
subplot(312)
plot(magX_IFFT)
xlabel('k')
ylabel('|X(k)|')
title('mag of IFFT')
grid on

magX_IFFTshift = fftshift(magX_IFFT)
N = length(X_IFFT)
if(mod(N-2,x)==0)
k=-N/2:(N/2)-1;
else
k=-(N-1)/2:(N-1)/2;
end;
subplot(313)
plot(k,magX_IFFTshift);
xlabel('k')
ylabel('|shiftedX[k]|')
title('mag of shifted IFFT')
grid on

Output:
original signal of frequency 4HZ
1
x(t)

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
t
mag of IFFT
1
|X(k)|

0.5

0
0 10 20 30 40 50 60 70 80 90 100
k
mag of shifted IFFT
1
|shiftedX[k]|

0.5

0
-50 -40 -30 -20 -10 0 10 20 30 40 50
k

Class Activity
Q1. To compute the IFFT of the Exponential sequence and plot magnitude and phase response?

Q2. To compute the step sequences of IFFT?

You might also like