You are on page 1of 6

Name: your name

Reg. Number: your reg. number


Exp. No. 3 Date: Name of the Experiment: exp. name
clc;
clear all;
close all;

disp('Press any key to Continue Linear convolution');


pause;

x_seq = input('Enter the x vector sequence = '); % x vector input


h_seq = input('Enter the h vector sequence = '); % y vector input

y_seq = conv(x_seq,h_seq); % convolution ouput


disp('convolution ouput signal');
disp(y_seq);
disp('Press any key to continue plotting');
pause;

figure(1);

subplot(4,1,1);
stem(x_seq,'g');
title ('Input x(n) sequence');
xlabel ('"n" samples no.');
ylabel ('Amplitude');
subplot(4,1,2);
stem(h_seq,'r');
title ('Input h(n) sequence');
xlabel ('"n" samples no.');
ylabel ('Amplitude');
subplot(4,1,3);
stem(y_seq,'g');
title ('Output (convolved) x(n)*h(n) sequence');
xlabel ('"n" samples no.');
ylabel ('Amplitude');

DIGITAL SIGNAL PROCESSING LABORATORY CODE: ECE381


Name: your name
Reg. Number: your reg. number
Exp. No. 3 Date: Name of the Experiment: exp. name
disp('press any key to continue circular convolution');
pause;

% Linear Convolution using Circular convolution

len_1 = length(x_seq);
len_2 = length(h_seq);
N = len_1+len_2-1;
% N = abs(len_1-len_2);
x_new = x_seq;
h_new = h_seq;
zer_1 = zeros(1,N-len_1);
zer_2 = zeros(1,N-len_2);
h_new = [h_seq zer_2];
x_new = [x_seq zer_1];
h_new2 = fliplr(h_new);
h_new3 = h_new2;
h_new4 = h_new3;

for j = 1:N
for i=1:N
tmp = h_new3(N);
if i<N
h_new4(i+1) = h_new3(i);
end
if i == N
h_new4(1) = tmp;
end
end
h_new3 = h_new4;
y_new(j) = x_new*h_new3';
end
disp('Linear convolution using circular convolution ouput signal');
disp(y_new);

DIGITAL SIGNAL PROCESSING LABORATORY CODE: ECE381


Name: your name
Reg. Number: your reg. number
Exp. No. 3 Date: Name of the Experiment: exp. name
subplot(4,1,4);
stem(y_new,'g');
title ('Output (convolved) x(n)*h(n) sequence');
xlabel ('"n" samples no.');
ylabel ('Amplitude');
pause(5);

% Convolution of periodic signal using Circular convolution

M_N = max(len_1,len_2);
dum = 0;
while (M_N>0)
M_N = M_N-2;
dum = dum+1;
end

N2 = 2^dum;
x_new = x_seq;
h_new = h_seq;
zer_11 = zeros(1,N2-len_1);
zer_21 = zeros(1,N2-len_2);

h_new1 = [h_seq zer_21];


x_new1 = [x_seq zer_11];

h_new21 = fliplr(h_new1);
h_new31 = h_new21;
h_new41 = h_new31;

for j = 1:N2
for i=1:N2
tmp = h_new31(N2);
if i<N2
h_new41(i+1) = h_new31(i);

DIGITAL SIGNAL PROCESSING LABORATORY CODE: ECE381


Name: your name
Reg. Number: your reg. number
Exp. No. 3 Date: Name of the Experiment: exp. name
end
if i == N2
h_new41(1) = tmp;
end
end
h_new31 = h_new41;
y_new1(j) = x_new1*h_new31';
end
disp('circular convolution ouput signal');
disp(y_new1);
figure(3);
stem(y_new1,'g');
title ('Output (Circular convolved) x(n)*h(n) sequence');
xlabel ('"n" samples no.');
ylabel ('Amplitude');
pause(5);

%Correlation a) Cross correlation b) Auto Correlation


%Correlation of Two different signals (Cross correlation)

x_c = input('Enter the first sequence of correlation = ');


h_c = input('Enter the second sequence of correlation = ');
y_cxy = xcorr(x_c,h_c);
figure(2);
title('Cross correlation');
subplot(3,2,1);
stem(x_c);
title ('first sequence of Cross correlation');
xlabel ('Samples "n"');
ylabel ('Amplitude');

subplot(3,2,3);
stem(h_c);
title ('second sequence of Cross correlation');

DIGITAL SIGNAL PROCESSING LABORATORY CODE: ECE381


Name: your name
Reg. Number: your reg. number
Exp. No. 3 Date: Name of the Experiment: exp. name
xlabel ('Samples "n"');
ylabel ('Amplitude');
y_cxy_1 = y_cxy(1:length(x_c)+length(h_c)-1);
subplot(3,2,5);
stem(y_cxy_1);
title ('Cross correlation of x(n) & y(n)');
xlabel ('Samples "n"');
ylabel ('Amplitude');
disp('Cross correlation of x(n) & y(n)'),
disp(y_cxy_1);

%Correlation of same signal (Auto correlation)

x_a = input('Enter the sequence of correlation = ');


y_cxx = xcorr(x_a,x_a);
disp('Auto correlation of x(n) ='),
disp(y_cxx);
subplot(3,2,2);
stem(x_a);
title ('Auto correlation sequence');
xlabel ('Samples "n"');
ylabel ('Amplitude');

subplot(3,2,4);
stem(y_cxx);
title ('Auto correlation of x(n)');
xlabel ('Samples "n"');
ylabel ('Amplitude');

Enter your Input and output details here :


Press any key to Continue Linear convolution
Enter the x vector sequence = [1 2 3 4 5]
Enter the h vector sequence = [1 2 3 ]
convolution ouput signal

DIGITAL SIGNAL PROCESSING LABORATORY CODE: ECE381


Name: your name
Reg. Number: your reg. number
Exp. No. 3 Date: Name of the Experiment: exp. name
1 4 10 16 22 22 15

Press any key to continue plotting


press any key to continue circular convolution
Linear convolution using circular convolution ouput signal
1 4 10 16 22 22 15

circular convolution ouput signal


1 4 10 16 22 22 15 0

Enter the first sequence of correlation = [1 2 3 4]


Enter the second sequence of correlation = [1 2 3]
Cross correlation of x(n) & y(n)
-0.0000 3.0000 8.0000 14.0000 20.0000 11.0000

Enter the sequence of correlation = [1 2 3 4]


Auto correlation of x(n) =
4.0000 11.0000 20.0000 30.0000 20.0000 11.0000 4.0000

DIGITAL SIGNAL PROCESSING LABORATORY CODE: ECE381

You might also like