Professional Documents
Culture Documents
EE321
Digital Signal Processing Laboratory
Experiment 4
Date: 28-02-2024
Group no. 14
Aim 1: Find the DFT and IDFT of the given sequence using definitions. Say, x[n] = {0, 1, 2,
3}
x_n = [0 1 2 3];
length = length(x_n);
x_k = zeros (1, length);
tw = exp((-1j*2*pi)/length);
for k = 0:1:(length-1)
for n = 0:1:(length-1)
var = x_n(n+1)*(tw^(n*k));
x_k(k+1) = x_k(k+1) + var;
end
end
disp(x_n);
disp(x_k);
%Displayed Output:
Input: 0 1 2 3
DFT: 6.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 - 0.0000i -2.0000 - 2.0000i
clear
x_k = [6.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 - 0.0000i -2.0000 - 2.0000i];
length = length(x_k);
x_n = zeros (1, length);
tw = exp((1j*2*pi)/length);
for n = 0:1:(length-1)
for k = 0:1:(length-1)
var = x_k(k+1)*(tw^(n*k));
x_n(n+1) = x_n(n+1) +var;
end
end
x_n = x_n/length;
disp(x_k);
disp(x_n);
%Displayed Output:
Input: 6.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i
Input:
0
1
2
3
DFT:
6.0000 + 0.0000i
-2.0000 + 2.0000i
-2.0000 - 0.0000i
-2.0000 - 2.0000i
%%
%AIM 2: USNIG MATRIX METHOD: IDFT
clear
x_k = [6.0000+0.0000i; -2.0000+2.0000i; -2.0000-0.0000i; -2.0000-2.0000i];
length = length(x_k);
D_N = zeros(length);
tw = exp((1j*2*pi)/length);
for n = 0:1:(length-1)
for k = 0:1:(length-1)
D_N(k+1,n+1) = tw^(n*k);
end
end
x_n = (D_N * x_k)/length;
disp(x_k);
disp(x_n);
Input:
6.0000 + 0.0000i
-2.0000 + 2.0000i
-2.0000 + 0.0000i
-2.0000 - 2.0000i
IDFT:
0.0000 + 0.0000i
1.0000 + 0.0000i
2.0000 - 0.0000i
3.0000 - 0.0000i
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.
%dft
#include<stdio.h>
#include<math.h>
float x_n[4] = {0,1,2,3};
float x_k_real[4]= {0,0,0,0};
float x_k_img[4]= {0,0,0,0};
int i = 0;
int j = 0;
int main(void){
for(i=0; i< 4; i++){
for(j=0; j<4; j++){
x_k_real[i] += x_n[j]* cos(2* 3.14 * i * j /4);
x_k_img[i] += -1 * x_n[j]* sin(2* 3.14 * i * j /4);
}
}
return 0;
}
%idft
#include<stdio.h>
#include<math.h>
float x_n[4] = {0,0,0,0};
float x_k_real[4]= {6,-2,-2,-2};
float x_k_img[4]= {0,2,0,-2};
int i = 0;
int j = 0;
int main(void){
for(i=0; i< 4; i++){
for(j=0; j<4; j++){
x_n[i] = 0.25*(x_k_real[j]* cos(2* 3.14 * i * j /4) - x_k_img[j]* sin(2*
3.14 * i * j /4));
}
}
return 0;
}
MATLAB Code:
FFT:
N-Point FFT Using Decimation in Time(DIT) Technique:
function X = DIT_FFT(x)
len = length(x);
wn = exp(-1i*pi*2*(0:len/2-1)/len);
if len == 1
X = x;
else
even = x(1:2:end);
odd = x(2:2:end);
Y0 = DIT_FFT(even);
Y1 = DIT_FFT(odd);
X = [Y0+Y1.*wn, Y0-Y1.*wn];
end
end
IFFT:
N-Point IFFT Using Decimation in Time(DIT) Technique:
function X = isolve(x)
N = length(x);
if N == 1
X = x;
return;
end
x_even = x(1:2:N);
x_odd = x(2:2:N);
X_even = isolve(x_even);
X_odd = isolve(x_odd);
X = zeros(1,N);
w = zeros(1,N/2);
for k = 1:N/2
w(k) = exp(2i*pi*(k-1)/N);
end
for k = 1:N/2
end
end
x=[0,1,2,3];
N = length(x) ;
y = solve(x) ;
XX=isolve(y)/N;
XX
FFT of x(n)=[1,2,3,4]:
y= 6.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 - 0.0000i -2.0000 -
2.0000i
IFFT of y:
XX= 0.0000 + 0.0000i 1.0000 + 0.0000i 2.0000 + 0.0000i 3.0000 -
0.0000i
MATLAB Code:
FFT:
N-Point FFT Using Decimation in Frequency(DIF) Technique:
% Input sequence
x = [0, 1, 2, 3];
p = log2(N);
% Initializations
for n = 0:(Half-1)
pow = (2^(stage-1)) * n;
x(pos) = a;
x(pos + Half) = b;
end
end
end
y = bitrevorder(x);
disp(y);
FFT of x(n)=[1,2,3,4]:
IFFT:
N-Point IFFT Using Decimation in Frequency (DIF) Technique:
N = length(x);
p=log2(N);
Half=N/2;
for stage=1:p
for index=0:(N/(2^(stage-1))):(N-1)
for n=0:(Half-1)
pos=n+index+1;
pow=(2^(stage-1))*n;
w=exp((1i)*(2*pi)*pow/N);
a=x(pos)+x(pos+Half);
b=(x(pos)-x(pos+Half)).*w;
x(pos)=a;
x(pos+Half)=b;
end
end
Half=Half/2;
end
y=bitrevorder(x)/N;
IFFT of y:
y= 0.0000 + 0.0000i 1.0000 + 0.0000i 2.0000 + 0.0000i 3.0000 -
0.0000i
IDFT of x[k]
Output of FFT of x(n):
C code Using DIT:
FFT:
N-Point FFT Using Decimation in Time(DIT) Technique:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void solve(double *x_re, double *x_im, double *X_re, double *X_im, int N) {
% Base case: If the length of the input is 1, return the input itself
if (N == 1) {
X_re[0] = x_re[0];
X_im[0] = x_im[0];
return;
% Allocate memory for even and odd parts of the input sequence
x_even_re[i] = x_re[2*i];
x_even_im[i] = x_im[2*i];
x_odd_re[i] = x_re[2*i+1];
x_odd_im[i] = x_im[2*i+1];
% Recursively compute FFT for even and odd parts of the input
w_re[k] = cos(-2*M_PI*k/N);
w_im[k] = sin(-2*M_PI*k/N);
free(x_even_re);
free(x_even_im);
free(x_odd_re);
free(x_odd_im);
free(w_re);
free(w_im);
int main() {
% Input sequence
% Compute FFT
IFFT:
N-Point IFFT Using Decimation in Time(DIT) Technique:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <complex.h>
#define PI 3.14159265358979323846
% Function prototype
int main() {
double complex x[] = {6.0000 + 0.0000*I, -2.0000 + 2.0000*I, -2.0000 + 0.0000*I, -2.0000 - 2.0000*I};
double complex X[N];
isolve(x, X, N);
printf("Result of IFFT:\n");
return 0;
if (N == 1) {
X[0] = x[0];
return;
x_even[i] = x[2*i];
x_odd[i] = x[2*i+1];
% Combine results
IFFT of X(k):
X(n)= 0.0000 + 0.0000i 1.0000 + 0.0000i 2.0000 + 0.0000i 3.0000 -
0.0000i