Task – 3
AIM:
To perform circular convolution between any two sequences and to find DFT of a sequence using
MATLAB.
1) DFT:
i) Without using builtin function:
Code:
clc
clear all
close all
N = input('Enter the value N value for DFT: ');
inp = input('Enter the input sequence: ');
m = length(inp);
x = [inp,zeros(1,N-m)];
X = zeros(1,N);
for k = 1:N
X(k) = 0;
for n = 1:N
X(k) = X(k)+(x(n)*exp((-1j)*2*pi*(n-1)*(k-1)/N));
end
end
figure
subplot(3,1,1)
stem(inp)
xlim([0,m+1])
[Link] KUMAR
title('Input Sequence')
xlabel('Time')
ylabel('Amplitude')
% Magnitude of X[k]
subplot(3,1,2)
stem(abs(X))
title('Magnitude Response')
xlabel('Frequency')
ylabel('|X(k)|')
% Phase of X[K]
subplot(3,1,3)
plot(angle(X))
title('Phase Response')
xlabel('Frequency')
ylabel('Phase')
Output in command window:
Enter the value N value for DFT: 8
Enter the input sequence: [2+2i 3 5 3-4i 6 7 1-i 2]
>> abs(X)
ans =
29.1548 9.8176 6.3246 6.8150 5.0990 7.7211 8.2462 8.8066
>> angle(X)
ans =
[Link] KUMAR
-0.1031 2.8364 -0.3218 2.2522 1.7682 -1.1200 1.8158 2.2758
Output figure:
Input Sequence
5
Amplitude
-5
0 1 2 3 4 5 6 7 8 9
Time
Magnitude Response
40
|X(k)|
20
0
1 2 3 4 5 6 7 8
Frequency
Phase Response
5
Phase
-5
1 2 3 4 5 6 7 8
Frequency
(ii) With using builtin function:
Code:
% FFT command
[Link] KUMAR
X=fft(x,N);
figure
subplot(3,1,1)
stem(inp)
xlim([0,m+1])
title('Input Sequence')
xlabel('Time')
ylabel('Amplitude')
% Magnitude of X[k]
subplot(3,1,2)
stem(abs(X))
title('Magnitude Response using FFT')
xlabel('Frequency')
ylabel('|X(k)|')
% Phase of X[K]
subplot(3,1,3)
plot(angle(X))
title('Phase Response using FFT')
xlabel('Frequency')
ylabel('Phase')
Output in command window:
Enter the value N value for DFT: 8
Enter the input sequence: [2+2i 3 5 3-4i 6 7 1-i 2]
>> abs(X)
[Link] KUMAR
ans =
29.1548 9.8176 6.3246 6.8150 5.0990 7.7211 8.2462 8.8066
>> angle(X)
ans =
-0.1031 2.8364 -0.3218 2.2522 1.7682 -1.1200 1.8158 2.2758
Output figure:
Input Sequence
5
Amplitude
-5
0 1 2 3 4 5 6 7 8 9
Time
Magnitude Response using FFT
40
|X(k)|
20
0
1 2 3 4 5 6 7 8
Frequency
Phase Response using FFT
5
Phase
-5
1 2 3 4 5 6 7 8
Frequency
Manual Calculation of DFT:
x(n) = {0,1,2,3}
[Link] KUMAR
0
x 4 = [ 1]
2
3
𝑊40 𝑊40 𝑊40 𝑊40 1 1 1 1
𝑊0 𝑊41 𝑊42 𝑊43 1 −𝑗 −1 𝑗
W4 = 40 = [ ]
𝑊4 𝑊42 𝑊44 𝑊46 1 −1 1 −1
[𝑊40 𝑊43 𝑊44 𝑊49 ] 1 𝑗 −1 −𝑗
6
−2 + 𝑗2
X 4 = W4 x4 = [ ]
−2
−2 + 𝑗2
2) CIRCULAR CONVOLUTION:
a. Without using builtin function:
Code:
clc
clear all
close all
x1=input('enter the first sequence:');
x2=input('enter the second sequence:');
N1=length(x1);
N2=length(x2);
N=max(N1,N2);
N3=N1-N2;
if(N3>0)
x2=[x2,zeros(1,N3)];
else
x1=[x1,zeros(1,-N3)];
end
for n=1:N
[Link] KUMAR
y(n)=0
for i=1:N;
j=n-i+1;
if(j<=0)
j=N+j;
end
y(n)=[y(n)+(x1(i)*x2(j))];
end
end
stem(y);
Output in the command window:
enter the first sequence:[1 2 3]
enter the second sequence:[1 2 1 2]
y=
8 10 8 10
Output figure:
[Link] KUMAR
Circular Convolution output
10
6
amplitude--->
0
1 1.5 2 2.5 3 3.5 4
time--->
b. With Using builtin function:
Code:
clc
clear all
close all
x=input('enter the first sequence')
h=input('enter the second sequence')
y=cconv(x,h,4);
disp('The resultant is:');
stem(y);
xlabel('N--->');
ylabel('amplitude--->');
title('Circular Convolution output');
[Link] KUMAR
Output in the command window:
Output figure:
Circular Convolution output
10
6
amplitude--->
0
1 1.5 2 2.5 3 3.5 4
N--->
c. Using DFT and IDFT:
Code:
clc
[Link] KUMAR
clear all
close all
x1=input('enter the first sequence:');
x2=input('enter the second sequence:');
N1=length(x1);
N2=length(x2);
N=max(N1,N2);
N3=N1-N2;
if(N3>0)
x2=[x2,zeros(1,N3)];
else
x1=[x1,zeros(1,-N3)];
end
ccirc = ifft(fft(x1).*fft(x2));
disp('Result:');
ccirc
stem(ccirc);
xlabel('time--->');
ylabel('amplitude--->');
title('Circular Convolution output');
grid on
Output in the command window:
[Link] KUMAR
Output figure:
Circular Convolution output
10
6
amplitude--->
0
1 1.5 2 2.5 3 3.5 4
time--->
Circular Convolution using Matrix Method:
[Link] KUMAR
x1 (n) = {1, 2,3,1}
x2 (n) = {4, 3,2,2}
Matrix Method:
1 1 3 2 4 17
[2 1 1 3] [3] = [19]
3 2 1 1 2 22
1 3 2 1 2 19
[Link] KUMAR