Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

United International University

Department of Electrical and Electronics


Engineering
Digital signal processing Laboratory
Summer 212-EEE - 3310 – A
Tanzidul Aziz
ID: 021191049
Lab report – 02
Objective:
1. To know the way of solution to the differential equation in MATLAB.
2. To know the determination system response using the filter function.
3. To know the procured of the Convolution and correlation sequence in
MATLAB.
Lab Session 2.1: Impulse and Step Response of a system using difference equation.
A routine called filter is available in MATLAB to solve the differential equation
numerically, given the input and the difference equation coefficients.
Example 2.1: Given the following difference equation
y(n ) − y(n − 1) + 0.9 y(n − 2) = x (n ); n

(a) Calculate and plot the impulse response h(n) at n= [ -20:100]


(b) Calculate and plot the step response s(n) at n= [ -20:100]
(c) Is the system specified by h(n) stable?
Solution : The step response gives information about how the system responds to a
signal which will be there for a longer time. The impulse response will say something
about how the system reacts when it gets a smack to start, how long will it go on
MATLAB code:
b=[1];
a=[1,-1,0.9];
x=delta(0,-20,120);n=[-20:120];
h=filter(b,a,x);
subplot(2,1,1);stem(n,h);
title('Impulse Response');
xlabel('n');
ylabel('h(n)');
axis([min(n)-2,max(n)+2,min(h)-2,max(h)+2]);
x1=u(0,-20,120);
s=filter(b,a,x1);
subplot(2,1,2);stem(n,s);
title('Step Response');
xlabel('n');
ylabel('s(n)');
axis([min(n)-2,max(n)+2,min(s)-2,max(s)+2]);
sum(abs(h))
Lab Session 2.2: System response for a given input.
Example 2.2 : Let the input and impulse response of a system are given by,
x (n ) = u (n ) − u (n − 10) and, h (n ) = (0.9) n u (n ) Determine the system response for n=
[-5:50];

Solution : The corresponding difference equation is y(n ) − 0.9 y(n − 1) = x (n )


The step response provides a convenient way to figure out the impulse response of
a system.
MATLAB code :
b=[1];
a=[1,-0.9];
n=[-5:50];
x=u(0,-5,50)-u(10,-5,50);
y=filter(b,a,x);
stem(n,y);
title('Output sequence');
xlabel('n');
ylabel('y(n)');
axis([min(n)-2,max(n)+2,min(y)-2,max(y)+2]);

Lab Session 2.3: Convolution and Correlation of sequences.


In general, the convolution operation is used to describe the response of an LTI
system. In DSP it is an important operation and has many other uses. MATLAB
does provide a built-in function called conv the computes the convolution between
two finite-duration sequences.
Example 2.3 : If x= {3 11 7 6} and h= [4 -3 2], find their convolution.
Solution: Correlation is the measurement of the similarity between two
signals/sequences. Convolution is the measurement of the effect of one signal on the
other signal. The mathematical calculation of Correlation is the same as a
convolution in the time domain, except that the signal is not reversed, before the
multiplication process.
Example 2.4 : If x = {3 11 7 0 -1 4 2}, -3<n< 3 and h= {2 3 0 -5 2 1}, -1<n<4
Find their convolution.
Solution: Here two signals x and h have a fixed boundary. To calculate the
convolution of this signal we use the custom convolution function.
MATLAB Code and Output:
x=[3 11 7 0 -1 4 2];nx=[-3:3];
h=[2 3 0 -5 2 1]; nh=[-1:4];
[y,ny] = conv_m(x,nx,h,nh);
subplot(3,1,1);
stem(nx,x);
axis([min(ny)-1 max(ny)+1 min(x)-1 max(x)+1])
subplot(3,1,2);
stem(nh,h);
axis([min(ny)-1 max(ny)+1 min(h)-1 max(h)+1])
subplot(3,1,3);
stem(ny,y);
axis([min(ny)-1 max(ny)+1 min(y)-1 max(y)+1])
Correlation of sequences: Correlation is an operation used in many applications
in digital signal processing. It is a measure of the degree to which two sequences
are similar. Given two real-valued sequences x(n) and y(n) of finite energy, the
crosscorrelation of x(n) and y(n) is a sequence rxy (l ) , defined as

rxy (l ) =  x ( n) y ( n − l )
n =−

Example 2.5 : Consider, y(n)=x(n-2) where x(n)= [3 11 7 0 -1 4 2].


Find the cross-correlating x(n) with y(n) and discuss the result.
Solution: In signal processing, cross-correlation is a measure of similarity of two
series as a function of the displacement of one relative to the other. This is also
known as a sliding dot product or sliding inner product. Here the arrow signed
means the boundary of the signal.
MATLAB Code:
x = [3,11,7,0,-1,4,2]; nx = -3:3;
[y,ny] = sigshift(x,nx,2);

[y1,ny1] = sigfold(y,ny);
[rxy,nxy] = conv_m(x,nx,y1,ny1);
rxy=rxy/max(rxy);

subplot(3,1,1);
stem(nx,x); axis([min(nxy)-1 max(nxy)+1 min(x)
max(x)]);
subplot(3,1,2);
stem(ny,y); axis([min(nxy)-1 max(nxy)+1 min(y)
max(y)]);
subplot(3,1,3);
stem(nxy,rxy); axis([min(nxy)-1 max(nxy)+1 0
max(rxy)]);

Example 2.6:Correlation using xcorr function:


Find and plot the correlation between the two following sequences:
X ={1 2 3 4}, h={4 3 2 1};Use xcorr function.
Solution : xcorr is a MATLAB built is function to calculate the correlation of two
signal.
MATLAB Code:
x=[1 2 3 4];
h=[4 3 2 1];
r_1=xcorr(x,h);
r_1=r_1/max(r_1);
tx = 1:length(x);
ty = 1:length(h);
tr = ceil(-(length(x)+length(h)-1)/2) :
floor((length(x)+length(h)-1)/2);
stem(tr,r_1); title('XC');

Example 2.7:
Let x(n) = [3, 11, 7, 0, -1, 4, 2] be a prototype sequence and y(n) = x(n-2) + w(n)
be its noise-corrupted and shifted version. Where w(n) is Gaussian sequence with
mean 0 and variance 1. Compute the crosscorrelation between y(n) and x(n).

Solution: This question has two signals one is a sequence signal and another is a
shifting signal with the addition of a noise signal. To generate this crosscorrelation
We use MATLAB built-in randn for noise and a modified function with xcorr.
MATLAB Code:
x =[3, 11, 7, 0, -1, 4, 2];
nx = [-3:3]; % given siganl x(n)

[y, ny] = sigshift(x, nx, 2); % obtain x(n-2)


w = randn(1, length(y)); nw= ny; % generate w(n)
[y, ny] = sigadd(y, ny, w, nw); % obtain y(n) = x(n-
2) + w(n)

rxy=xcorr(x,y); rxy=rxy/max(rxy); ny1 = -fliplr(ny);


nxy = [(nx(1) + ny1(1)):(nx(end) + ny1(end))];

subplot(3,1,1); % plot x
stem(nx,x); axis([min(nxy)-1 max(nxy)+1 min(x)
max(x)]);
subplot(3,1,2); % plot y
stem(ny,y); axis([min(nxy)-1 max(nxy)+1 min(y)
max(y)]);
subplot(3,1,3); % plot rxy against lag variable
stem(nxy,rxy); axis([min(nxy)-1 max(nxy)+1 0
max(rxy)]);
xlabel('lag variable l'); ylabel('rxy');
title('Crosscorrelation: noise sequence 1');
Example 2.8: Write a MATLAB program to compute correlation coefficient of
two sequence.
(a) x(n) = [1 2 3] y(n) = [1 2 3]
(b) x(n) = [1 2 3] y(n) = [-1 -2 -3]
(c) x(n) = [1 2 3] y(n) = [3 2 1]
Solution: The correlation coefficient is determined by dividing the covariance by
the product of the two variables' standard deviations. Standard deviation is a
measure of the dispersion of data from its average.
MATLAB Code:
X=input('Input the 1st sequence:');
Y=input('Input the 2nd sequence:');
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)

a.
b.

c.
Lab Session 2.4:
IE2.1: A difference equation is given as, y(n)-0.3y(n-2) = 0.2x(n) Find y(n) for an
input x (n) =u(n)-u(n-3)
Solution : Difference equation, mathematical equality involving the differences
between successive values of a function of a discrete variable.
MATLAB 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)-2,max(n)+2,min(s)-1,max(s)+1]);
sum(abs(s))
IE2.2 : Two sequences are given by, x(n) = [1 2 3] ; y(n) = [4 5 6]
(a) Find their crosscorrelation using xcorr function
(b) Find their crosscorrelation using conv function
(c) Find the correlation coefficient

Solution: This question has 3 different parts, two of them are used to plot the
crosscorrelation by using a different method. And the last one is to find the
coefficient of the two signals.
MATLAB Code: (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');
MATLAB Code: (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)+leng
th(y)-1)/2);
stem(tr,r_2); title('XC');

MATLAB Code: (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)
Home Work :
Q1) Invoke the following MATLAB commands and plot the result (y versus ny)
x1 = [0, 0, 1, 1, 1, 1, 1]; nx1 = [-3:3]; x2 = [0, 0, 0, 1, 1, 1]; nx2 = [-3:2];
Find the crosscorrelation of x1 and x2.
Solution : To find the crosscorrelation of x1 & x2 I use modified conv_m function.
That helps us to generate the function in a certain boundary area.
MATLAB 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]);
subplot(3,1,2);
stem(nx2,x2); title('x2');axis([min(nx2)-1 max(nx2)+1
min(x2)-1 max(x2)+1]);
subplot(3,1,3);
stem(nx,xc); title('xc');axis([min(nx)-1 max(nx)+1
min(xc)-1 max(xc)+1]);

Q2. Determine the autocorrelation sequence and crosscorrelation sequence for the
following sequences.
x (n ) = (0.9) n , 0  n  20
y(n ) = (0.8) n , − 20  n  0

What is your observation?


Solution: Autocorrelation is a mathematical representation of the degree of
similarity between a given time series. Cross-correlation is a measurement that
tracks the movements of two or more sets of time series data relative to one
another.
MATLAB Code:
%Autocorrelation
nx = [0:20];
ny = [-20:0];
x= 0.9.^nx;
[xf, nxf]=sigfold(x,nx);
[rxx,nrxx]=conv_m(x,nx,xf,nxf);
subplot(211);
stem(nrxx,rxx);
title('Autocorrelation');
axis([min(nrxx)-1 max(nrxx)+1 min(rxx)-1 max(rxx)+1]);
%Crosscorrelation
nx = [0:20];
ny = [-20:0];
x= 0.9.^nx;
y = 0.8.^ny;
[yf,nyf] = sigfold(y,ny);
[rxy,nrxy] = conv_m(x,nx,yf,nyf);
subplot(212);
stem(nrxy,rxy);
title('Crosscorrelation');
axis([min(nrxy)-1 max(nrxy)+1 min(rxy)-40
max(rxy)+40]);

Crosscorrelation is dependent on two separate sequences. On the other hand,


Autocorrelation depends on its own folded signal. Two signal output is slightly
different from each other.
Q3. Consider a 20point moving average filter

[ x (n ) + x (n − 1) + .......... .......... .......... ......... + x (n − 19)]


1
y( n ) =
20

(a) Generate 200 samples of a 1 Hz sine wave sampled at 40 Hz.


(b) Add some noise to generate a noisy signal.
(c) Filter the noisy signal through the 20point MA filter.
Plot each signal to display the effects of noise and smoothing
Solution :
MATLAB 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');
subplot(3,1,2);
plot(x,'r');
hold on;
plot(a,'b');
legend('main signal','noise signal')
title('noise+main signal');
subplot(3,1,3);plot(h,'r');
title('filter signal');
Conclusion: Correlation is the measurement of the similarity between two
signals/sequences. Convolution is the measurement of the effect of one signal on the
other signal. The mathematical calculation of Correlation is the same as a
convolution in the time domain, except that the signal is not reversed, before the
multiplication process. We use some modified and built-in MATLAB functions to
generate the filter, correlation, and convolution signal.

You might also like