Vous êtes sur la page 1sur 3

DISCRETE SIGNAL PROCESSING

Experiment 5

CODE:

%Program to calculate the circular convolution of two signals x(n) and h(n)

%Taking input from user: Number of values in x(n) and h(n)


L=input('Enter the number of values in x(n) and h(n):');

%Assuming that number of DFT values to be found out are equal to length of
%input signal
N=L;

%Taking input signal x(n) from user


for n = 1:1:L
x(n)=input('Please enter the value for x(n)');
end

%Finding DFT for x(n)


for k=1:1:N
X(k)=complex(0,0);
for n=1:1:L
c=complex(cos(2*pi*(k-1)*(n-1)/N),-sin(2*pi*(k-1)*(n-1)/N));
X(k)=X(k)+(x(n)*c);
end
end

%Taking input signal h(n) from user


for n = 1:1:L
h(n)=input('Please enter the value for h(n)');
end

%Finding DFT for h(n)


for k=1:1:N
H(k)=complex(0,0);
for n=1:1:L
c=complex(cos(2*pi*(k-1)*(n-1)/N),-sin(2*pi*(k-1)*(n-1)/N));
H(k)=H(k)+(h(n)*c);
end
end

%Finding Y(k) using multiplication of X(k) * Y(k)


for k=1:1:N
Y(k)=X(k)*H(k);
end

%Calculating the IDFT of Y(k)


for n=1:1:L
y(n)=complex(0,0);
for k=1:1:N
c=complex(cos(2*pi*(k-1)*(n-1)/N),sin(2*pi*(k-1)*(n-1)/N));
y(n)=y(n)+((Y(k)*c))/N;
end
end

%Displaying the IDFT of Y(k)


disp('Value of Circular Convulation calculated by program:');
disp(abs(y));
%Displaying the value of circular convulation calculated by predefined function
disp('Value of Circular Convulation calculated by predefined function cconv:');
disp(cconv(x,h,N));

%Creating graphs
subplot(4,2,1);
stem(x);
xlabel('n'); ylabel(x(n)); title('Input function x(n)');
subplot(4,2,2);
stem(X);
xlabel('k'); ylabel(X(k)); title('DFT(x(n))=X(k)');
subplot(4,2,3);
stem(h);
xlabel('n'); ylabel(h(n)); title('Input function h(n)');
subplot(4,2,4);
stem(H);
xlabel('k'); ylabel(H(k)); title('DFT(h(n))=H(k)');
subplot(4,2,5);
stem(Y);
xlabel('k'); ylabel(Y(k)); title('Y(k)=H(k)*X(k)');
subplot(4,2,6);
stem(y);
xlabel('n'); ylabel(y(k)); title('y(n)=IDFT(Y(k))');
subplot(4,2,7);
stem(abs(y));
xlabel('n'); ylabel(y(k)); title('absolute values of y(n)=IDFT(Y(k))');
subplot(4,2,8);
stem(cconv(x,h,N));
xlabel('n'); ylabel('x(n)©h(n)'); title('Circular Convulation of x(n) and h(n)');
OUTPUT:

Enter the number of values in x(n) and h(n):4


Please enter the value for x(n)2
Please enter the value for x(n)1
Please enter the value for x(n)2
Please enter the value for x(n)1
Please enter the value for h(n)1
Please enter the value for h(n)2
Please enter the value for h(n)3
Please enter the value for h(n)4
Value of Circular Convulation calculated by program:
14 16 14 16

Value of Circular Convulation calculated by predefined function cconv:


14 16 14 16

Vous aimerez peut-être aussi