0% found this document useful (0 votes)
20 views8 pages

DSP Exp4 2001ee24

The document outlines a DSP lab experiment focused on Discrete Fourier Transform (DFT) and Fast Fourier Transform (FFT) using MATLAB and C programming. It includes aims to compute DFT and IDFT using definitions and matrix representation, as well as implementing FFT and IFFT algorithms without built-in functions. The document also provides MATLAB and C code for each aim, along with instructions for verifying results using Code Composer Studio and the TMS 320C6713 DSK Kit.

Uploaded by

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

DSP Exp4 2001ee24

The document outlines a DSP lab experiment focused on Discrete Fourier Transform (DFT) and Fast Fourier Transform (FFT) using MATLAB and C programming. It includes aims to compute DFT and IDFT using definitions and matrix representation, as well as implementing FFT and IFFT algorithms without built-in functions. The document also provides MATLAB and C code for each aim, along with instructions for verifying results using Code Composer Studio and the TMS 320C6713 DSK Kit.

Uploaded by

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

EE321

DSP LAB
EXPERIMENT 04

Aim: Discrete Fourier Transform and Fast Fourier Transform

Name: Kanishk Giri

Roll No: 2001EE24


Aim 1: Find the DFT and IDFT of the given sequence using definitions. Say, x[n] = {0, 1, 2,
3}
MATLAB Code: -
clc;
clear all;
close all;
N = input('enter the value of N = ')
x_n = [0 1 2 3];
L = length(x_n);
x_n = [x_n zeros(1, N-L)];
X_k = zeros(1, N);
% code for DFT
for k = 0: N-1
for n = 0: N-1
X_k(k+1) += x_n(n+1)*exp(-1i*2*pi*k*n/N);
end
end
% code for IDFT
inv_Xk = zeros(1,N);
for n = 0: N-1
for k = 0: N-1
inv_Xk(n+1)+=(X_k(k+1)/N)*exp(1i*2*pi*k*n/N);
end
end
subplot(3,1,1)
stem(abs(x_n)); title('input sequence');
subplot(3,1,2)
stem(abs(X_k)); title('DFT of input sequence');
subplot(3,1,3)
stem(abs(inv_Xk)); title('IDFT of input sequence');
Aim 2: Repeat Aim 1 using compact matrix representation.
MATLAB Code: -
clc
close all
clear all

x = [0,1,2,3];

N = length(x);

% Complex matrix creation


mat = zeros(N);

for a=1:N
for j=1:N
power = (2*i*pi*(a-1)*(j-1))/N;
mat(a, j) = exp(power);
end
end

% DFT answer
Xd = x*mat;
disp(Xd);

% IDFT answer
matInv = inv(mat);
IXd = Xd*matInv;
disp(IXd);

Aim 3: Consider Aim 1 and Aim 2 and write "C" program to verify using Code Composer
Studio (CCS-3.1) IDE and the DSP TMS 320C6713 DSK Kit.
CCS Code: -

#include<math.h>

float arr[4] = {0,1,2,3};


float realPart[4] = {0,0,0,0};
float imgPart[4] ={0,0,0,0};
int main(void){
int i=0,j=0,n=4;
float N=4;
for(i=0;i<n;i++){
realPart[i]=0;
imgPart[i]=0;
for(j=0;j<n;j++){
float ang=(2*3.14159265359*j*i);
ang/=N;
realPart[i]+=arr[j]*cos(ang);
imgPart[i]-=arr[j]*cos((1.57 - ang));
}
}
return 0;
}

Real Part Plot

Imaginary Part Plot

Aim 4: To find N point FFT and IFFT of the given sequence (Decimation in time and
decimation in frequency algorithms) without using the MATLAB inbuilt functions. Plot and
check your results with the inbuilt functions.
MATLAB Code for FFT: -
clc

close all
clear all

x =[0,1,2,3];
n = length(x);
xa= [0:n-1];
[ll,aa] = bitrevorder(xa);
xx = zeros(1, n);
for ii=1:n
xx(ii) = x(aa(ii));
end
Xdf =xx;
dum = zeros(1, n);
Wn = exp(-i*2*pi/n);
stage = log(n)/log(2);
size = 2;
for e= 1: stage
steps = n/size;
jj = 1;
for b = 1: steps
for i = 1: size
power = Wn.^((i-1)*steps);
p = size/2;
ind = jj-1;
ind = rem(ind, p);
ind = ind+1;
ind = ind + (b-1)*size;
dum(jj) = Xdf(ind) + power*Xdf(ind + p);
jj = jj +1;

end
end
for i = 1:n
Xdf(i) = dum(i);
end
size = size*2;
end
disp(Xdf);
% Comparing ans
fft(x)

MATLAB Code for IFFT: -

clc

close all;
clear all;

x = [6.0000 + 0.0000i, -2.0000 + 2.0000i, -2.0000 - 0.0000i, -2.0000 -


2.0000i];
n = length(x);
xa= [0:n-1];
[ll,aa] = bitrevorder(xa);
xx = zeros(1, n);

for ii=1:n
xx(ii) = x(aa(ii));
end

Xdf =xx;
dum = zeros(1, n);
Wn = exp(i*2*pi/n);
stage = log(n)/log(2);
size = 2;

for e= 1: stage
steps = n/size;
jj = 1;
for b = 1: steps
for i = 1: size
power = Wn.^((i-1)*steps);
p = size/2;
ind = jj-1;
ind = rem(ind, p);
ind +=1;
ind +=(b-1)*size;
dum(jj) = Xdf(ind) + power*Xdf(ind + p);
jj +=1;
end
end
for i = 1: n
Xdf(i) = dum(i);
end
size = size*2;
end
for i=1: n
Xdf(i) = Xdf(i)/n;
end
disp(Xdf);
% Comparing ans
fft(x)

Aim 5: Consider Aim 4 and write "C" program to verify using Code Composer Studio (CCS-
3.1) IDE and the DSP TMS 320C6713 DSK Kit.

CCS Code for FFT: -


#include <stdio.h>
#include <math.h>

float xReal[4] = {0,2, 1,3};


float xImg[4] = {0,0, 0,0};
float pi = 3.14159265359;

int main(){
int n = sizeof(xReal)/sizeof(xReal[0]);
float dumr[n], dumi[n];
float XdReal[n], XdImg[n];
int i=0;

for(i=0; i<n; ++i){


dumr[i] = 0;
dumi[i] = 0;
XdReal[i] = xReal[i];
XdImg[i] = xImg[i];
}
int stage = log(n)/log(2);
int size = 2;
for(int e=1; e<=stage; ++e){
int steps = n/size;
int jj = 1;
for(int b = 1; b<=steps; ++b){
for(int i=1; i<=size; ++i){
float pow = (i-1)*steps;
float ang = 2*pi*pow/n;
float Wnr = cos(ang);
float Wni = sin(ang);
int p = size/2;
int ind = jj-1;
ind = ind%p+1+(b-1)*size;
dumr[jj-1] = XdReal[ind-1] + Wnr*XdReal[ind + p-1] -
Wni*XdImg[ind+p-1];
dumi[jj-1] = XdImg[ind-1] - Wnr*XdImg[ind + p-1] -
Wni*XdReal[ind+p-1];
jj +=1;
}
}
for(int i=0; i<n; ++i){
XdReal[i] = dumr[i];
XdImg[i] = dumi[i];
}
size = size*2;
}
for(int i=0; i<n; ++i){
printf("%f + j*%f, ", XdReal[i], XdImg[i]);
}
printf("\n");
return 0;
}

CCS Code for IFFT: -


#include <stdio.h>
#include <math.h>

float xReal[4] = {6,-2,-2,-2};


float xImg[4] = {0,0,2,-2};
float pi = 3.14159265359;

int main(){
int n = sizeof(xReal)/sizeof(xReal[0]);
float dumr[n], dumi[n];
float XdReal[n], XdImg[n];
int i=0;

for(i=0; i<n; ++i){


dumr[i] = 0;
dumi[i] = 0;
XdReal[i] = xReal[i];
XdImg[i] = xImg[i];
}
int stage = log(n)/log(2);
int size = 2;
for(int e=1; e<=stage; ++e){
int steps = n/size;
int jj = 1;
for(int b = 1; b<=steps; ++b){
for(int i=1; i<=size; ++i){
float pow=(i-1)*steps;
float ang = 2*pi*pow/n;
float Wnr = cos(ang);
float Wni = sin(ang);
int p = size/2;
int ind = jj-1;
ind = ind%p+1(b-1)*size;
dumr[jj-1] = XdReal[ind-1] + Wnr*XdReal[ind + p-1] -
Wni*XdImg[ind+p-1];
dumi[jj-1] = XdImg[ind-1] + Wnr*XdImg[ind + p-1] +
Wni*XdReal[ind+p-1];
jj +=1;
}
}
for(int i=0; i<n; ++i){
XdReal[i] = dumr[i];
XdImg[i] = dumi[i];
}
size = size*2;
}
for(int i=0; i<n; ++i){
XdReal[i]/=n, XdImg[i]/=n;
printf("%f + j*%f, ", XdReal[i], XdImg[i]);
}
printf("\n");
return 0;
}

You might also like