Lab Report 2
Lab Report 2
Objective: The main objective of that experiment is to find out the ‘Impulse and Step Response’ of a
system and convolution using difference equation by using MATLAB. In this lab we also came to know
how to find System response for a given input, Impulse and Step Response of a system using difference
equation, Convolution and Correlation of sequences, Modified function for convolution, Correlation of
sequences, Correlation using xcorr function, and Correlation Coefficient.
In Lab Evaluation
IE2.1:
Code:
b=[0.2]; a=[1,-0.3,];
n=[-5:20];
x=u(0,-5,20)-u(3,-5,20);
s=filter(b,a,x);
stem(n,s);
title('Step Response');
xlabel('n');ylabel('s(n)');
axis([min(n)-1,max(n)+1,min(s)-1,max(s)+1]);
sum(abs(s)); grid on;
Output:
Step Response
0.5
s(n)
-0.5
-1
-5 0 5 10 15 20
n
IE2.2:
Code:
%part a
x=[1 2 3]; y=[4 5 6];
r_1=xcorr(x,y);
r_1=r_1/max(r_1);
tx = 1:length(x);
ty = 1:length(y);
tr = ceil(-(length(x)+length(y)-1)/2) : floor((length(x)+length(y)-1)/2);
stem(tr,r_1); title('XC');grid on;
Output:
XC
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
%part b
x=[1 2 3];
y=[4 5 6];
r_2 = conv(x,fliplr(y));
r_2=r_2/max(r_2);
tx = 1:length(x);
ty = 1:length(y);
tr = ceil(-(length(x)+length(y)-1)/2) : floor((length(x)+length(y)-1)/2);
stem(tr,r_2); title('XC');grid on;
Output:
XC
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
%part c
X=[1 2 3];
Y=[4 5 6];
Xm=X-mean(X);
Ym=Y-mean(Y);
rxy = sum(Xm.*Ym)/sqrt(sum(Xm.^2)*sum(Ym.^2));
disp('The correlation coefficient is:');
disp(rxy)
Output:
Home Work
Code:
x1 = [0, 0, 1, 1, 1, 1, 1];
nx1 = [-3:3];
x2 = [0, 0, 0, 1, 1, 1];
nx2 = [-3:2];
[xc,nx] = conv_m(x1,nx1,x2,nx2);
xc=xc/max(xc);
subplot(3,1,1);
stem(nx1,x1); title('x1');axis([min(nx1)-1 max(nx1)+1 min(x1)-1 max(x1)+1]);
grid on;
subplot(3,1,2);
stem(nx2,x2); title('x2');axis([min(nx2)-1 max(nx2)+1 min(x2)-1 max(x2)+1]);
grid on;
subplot(3,1,3);
stem(nx,xc); title('xc');axis([min(nx)-1 max(nx)+1 min(xc)-1 max(xc)+1]);
grid on;
Output:
x1
2
-1
-4 -3 -2 -1 0 1 2 3 4
x2
2
-1
-4 -3 -2 -1 0 1 2 3
xc
2
-1
-6 -4 -2 0 2 4 6
Ans to the que no-2
Code:
% autocorrelation sequence
nx = [0:20]; x = 0.9 .^ nx; [xf,nxf] = sigfold(x,nx);
[rxx,nrxx] = conv_m(x,nx,xf,nxf);
stem(nrxx,rxx);
xlabel('n'); ylabel('r_x_x(n)');
title('Autocorrelation of x(n)');
axis([min(nrxx)-1,max(nrxx)+1,min(rxx),max(rxx)+1]);
grid on
Output:
Autocorrelation of x(n)
6
4
rxx(n)
% crosscorrelation sequence
nx = [0:20]; x = 0.9 .^ nx; ny = [-20:0]; y = 0.8 .^ (-ny);
[yf,nyf] = sigfold(y,ny); [rxy,nrxy] = conv_m(x,nx,yf,nyf);
stem(nrxy,rxy);
xlabel('n'); ylabel('r_x_y(n)');
title('Crosscorrelation of x(n) and y(n)');
axis([min(nrxy)-1,max(nrxy)+1,floor(min(rxy)),ceil(max(rxy))]);
grid on;
Output:
Crosscorrelation of x(n) and y(n)
3
2.5
2
rxy(n)
1.5
0.5
0
0 5 10 15 20 25 30 35 40
n
Ans to the que no-3
Code:
%a
fs=40;
f=1;
Ts=1/fs;
T=200*Ts;
t=0:Ts:T;
x=sin(2*pi*f*t);
%b
n=rand(size(t));
a=x+n;
t1=ones(1,200);
u=(1/200)*t1;
b=[1];
h=filter(u,b,a);
%d
subplot(3,1,1);
plot(x);
title('main signal');grid on;
subplot(3,1,2);
plot(x,'r');
hold on;
plot(a,'b');
legend('main signal','noise signal')
title('noise+main signal');grid on;
subplot(3,1,3);plot(h,'r');
title('filter signal');grid on;
Output:
main signal
1
-1
0 50 100 150 200 250
noise+main signal
2
main signal
1 noise signal
-1
0 50 100 150 200 250
filter signal
0.6
0.4
0.2
0
0 50 100 150 200 250