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

FFT Code

The document contains 10 tasks that involve computing the discrete Fourier transform (DFT) and fast Fourier transform (FFT) of different sequences, such as step sequences, exponential sequences, and impulse sequences. Plots of the magnitude and phase responses of the DFT/FFT are generated for each task. Execution times for computing the FFT of randomly generated sequences of different lengths are also measured and plotted.

Uploaded by

asma tariq
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

FFT Code

The document contains 10 tasks that involve computing the discrete Fourier transform (DFT) and fast Fourier transform (FFT) of different sequences, such as step sequences, exponential sequences, and impulse sequences. Plots of the magnitude and phase responses of the DFT/FFT are generated for each task. Execution times for computing the FFT of randomly generated sequences of different lengths are also measured and plotted.

Uploaded by

asma tariq
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

%% Task 01

clc

clear all

close all

n = 0:6;

x = [4 6 2 1 7 4 8];

a = fft(x);

mag = abs(a);

angle = angle(a);

subplot(2,1,1);

stem(mag);

grid on

title('Magnitude Response');

subplot(2,1,2);

stem(angle);

grid on

title('phase Response');

%% Task 02

clc

clear all

close all

x=[4 6 2 1 7 4 8];

N=7

L=length(x);

x_n=[x,zeros(1,N-L)];

for i=1:N

for j=1:N

temp=-2*pi*(i-1)*(j-1)/N;

DFT_mat(i,j)=exp(complex(0,temp));
end

end

X_k=DFT_mat*x_n';

disp('N point DFT is X[k] = ');

disp(X_k);

mag=abs(X_k);

phase=angle(X_k)*180/pi;

subplot(2,1,1);

stem(mag);

xlabel('frequency index k');ylabel('Magnitude of X[k]');

axis([0 N+1 -2 max(mag)+2]);

subplot(2,1,2);

stem(phase);

xlabel('frequency index k');

ylabel('Phase of X[k]');

axis([0 N+1 -180 180]);

%% Task 03

clc

clear all

close all

%Step Sequence

s=4;

t=-s:1:s;

y=[zeros(1,s) ones(1,1) ones(1,s)];

subplot(3,1,1);

stem(t,y);

grid

title ('Step Sequence');

xlabel ('time -->');


ylabel ('--> Amplitude');

xn=y;

N=10

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))');

%% Task 04

clc;

clear all;

close all;

%exponential sequence

n=7

t=0:1:n;

a=-0.9

y=exp(a*t);

y=[9 8 7 6 5 4 3 2]

subplot(3,1,1);
stem(t,y);

grid;

title('exponential response');

xlabel('time');

ylabel('amplitude');

disp(y);

xn=y;

N=8

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))');

%% Task 05

clc

clear all

close all

n = 0:7;

x = [1 1 0 0 0 0 0 0];
a = fft(x);

mag = abs(a);

angle = angle(a);

subplot(2,1,1);

stem(mag);

grid on

title('Magnitude Response');

subplot(2,1,2);

stem(angle);

grid on

title('phase Response');

%% Task 06

clc

clear all

close all

n = 0:7;

x = [1 0 1 0 1 0 1 0];

a = fft(x);

mag = abs(a);

angle = angle(a);

subplot(2,1,1);

stem(mag);

grid on

title('Magnitude Response');

subplot(2,1,2);

stem(angle);

grid on

title('phase Response');

%% Task 07
clc

clear all

close all

n = 0:5;

x = [1 1+j -2j 1+2j -j j];

a = fft(x);

mag = abs(a);

angle = angle(a);

subplot(2,1,1);

stem(mag);

grid on

title('Magnitude Response');

subplot(2,1,2);

stem(angle);

grid on

title('phase Response');

%% Task 08

clc

clear all

close all

n = 0:5;

x = [1 0 1 -2j 1+2j -7j];

a = fft(x);

mag = abs(a);

angle = angle(a);

subplot(2,1,1);

stem(mag);

grid on

title('Magnitude Response');
subplot(2,1,2);

stem(angle);

grid on

title('phase Response');

%% Task 09

clc

clear all

close all

Nmax = 2048;

fft_time=zeros(1,Nmax);

for n=1:1:Nmax

x=rand(1,n);

t=clock;

fft(x);

fft_time(n)=etime(clock,t);

end

n=[1:1:Nmax];

plot(n,fft_time,':')

xlabel('N');

ylabel('Time in Sec');

title('FFT Execution Times');

%% Task 10

clc

clear all

close all

t=-2:1:2;

y=[zeros(1,2) 1 zeros(1,2)];

subplot (3,1,1);

stem(t,y);
title('impulse sequence');

grid;

xlabel ('time -->');

ylabel ('--> Amplitude');

xn=y;

N=10

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)|');

title('magnitude response');

subplot(3,1,3);

stem(k,angxk);

disp(xk);

grid;

xlabel('k');

ylabel('arg(x(k))');

title('angle response');

You might also like