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

Name: COSAIN, Saimah S.

ECE104.1: Lab #3

1. Convolution of Non-Causal Signals


Use Matlab to make stem plots of the following signals. Decide for yourself what the range
of n should be. Note that both of these signals start to the left of n = 0.

f(n) = 3 δ(n + 2) − δ(n − 1) + 2 δ(n − 3)


g(n) = u(n + 4) − u(n − 3)
MATLAB Command:

For f(n) = 3 δ(n + 2) − δ(n − 1) + 2 δ(n − 3)


>> n = [-5:10];
>> x1 = 3*impseq(-2,0,15);
>> x2 = impseq(1,0,15);
>> x3 = 2*impseq(3,0,15);
>> [x11,n11] = sigadd(x1,n,-x2,n);
>> [f,n] = sigadd(x11,n11,x3,n); % For f(n) = 3δ(n+2)−δ(n−1)+2δ(n−3)
>> stem(n,f,'k','filled','markersize',4)
>> axis([-5.2 11 -1.2 3.2]);
>> xlabel('n'); ylabel('f(n)');
>> title('The Plot sequence of f(n)');
SCREENSHOT:

1
For g(n) = u(n + 4)− u(n − 3)
>> n1 = [-5:10];
>> x1 = stepseq(-4,0,15);
>> x2 = stepseq(3,0,15);
>> [g,n] = sigadd(x1,n1,x2,n1);
>> [g,n] = sigadd(x1,n1,-x2,n1); % For g(n)=u(n+4)−u(n−3)
>> stem(n,g,'k','filled','markersize',4);
>> axis([-5.2 10.2 -2 2]);
>> xlabel('n'); ylabel('g(n)')
>> title('The Stem Plot of g(n)')
SCREENSHOT:

Next, use Matlab to make a stem plot of x(n) = f(n) ∗ g(n)


>> % x(n) = f(n)*g(n)
>> x = conv(f,g);
>> stem(n,x(1:length(n)),'k','filled','markersize',4);
>> axis([-6 11 -2 3]);
>> xlabel('n'); ylabel('x(n)')
>> title('The Stem plot of x(n)')

2
2. Smoothing Data
Save the data file lab3data from the course website. Load the data into Matlab using the command
load lab3data Type whos to see your variables. One of the variables will be lab3data. For
convenience, rename is to x by typing: x = lab3data; This signal comes from measuring electrical
signals from the brain of a human subject.
Make a stem plot of the signal x(n).

You will see it doesn’t look good because there are so many points.
Make a plot of x(n) using the plot command.

As you can see, for long signals we get a better plot using the plot command. Although discrete-time signals are
most appropriately displayed with the stem command, for long discrete-time signals (like this one) we use the
plot command for better appearance.
Create a simple impulse response for an LTI system:
h = ones(1,11)/11;
Make a stem plot of the impulse response.
Find the output signal y(n) of the system when x(n) is the input (using the conv command). Make
3
plot of the output y(n).

h = ones(1,11)/11;
Make a stem plot of the impulse response.

(a) What is the effect of the convolution in this example?


The effect of the convolution command made the plot signal a bit smoother compared to the plot of the x(n) alone.
(b) (b) How is the length of y(n) related to the length of (n)?
The n is the range at which the signal will stop .
(c) (c) Plot x(n) and y(n) on the same graph. What problem do you see? The signals have different length and looking
both the signal it reveals that the plot of y(n) has finer or smoother line compared to the x(n) who is the line is
a bit squeaky. Can you get y(n) to “line up” with x(n)? No.

>> load lab3data.m;


>> whos lab3data;
>> x = lab3data;
>> h = ones(1,11)/11;
>> stem(x,'filled','markersize',4);
>> title('Stem plot of x(n)')
>> ylabel('y(n)');
>> xlabel('n');
>> subplot(4,1,2);
>> plot(x);
>> title('Plot of x(n)');
>> ylabel('y(n)');
>> xlabel('n');
>> subplot(4,1,3);
>> stem(h,'filled')
>> y = conv(h,x);
>> subplot(4,1,3);

4
>> stem(y,'filled');
>> title('Stem plot of h(n)');
>> ylabel('y(n)');
>> xlabel('n');
>> subplot(4,1,4);
>> plot(y)
>> title('Plot of y(n)');
>> ylabel('y(n)');
>> xlabel('n');

(d) Use the following commands:


y2 = y;
y2(1:5) = [];
y2(end-4:end) = [];

What is the effect of these commands? The command made the two signals i.e. x(n) and y2(n), to have the
same length. What is the length of y2? The y2 has a length of 128. Plot x and y2 on the
same graph. What do you notice now? The plot is hard to read because the plot is messy. (P.s : Ma’am I think
I am doing the wrong command again basing the graph now I don’t know if correct po).

5
MARLAB COMMAND AND CODE:
>> load lab3data;
>> x = lab3data;
>> h = ones(1,11)/11;
>> y = conv(h,x);
>> y2 = y;
>> y2(1:5) = [];
>> y2(end-4:end) = [];
>> subplot(3,1,1);
>> plot(x);
>> set(gca ,'YGrid','on')
>> xlabel('n');
>> title('Plot of the x');
>> subplot(3,1,2);
>> plot(y2);
>> set(gca ,'YGrid','on')
>> xlabel('n');
>> title('Plot of the y2');
>> subplot(3,1,3);
>> plot(y2,x);
>> set(gca, 'YGrid','on')
>> xlabel('n');
>> title('Plot of the x and y2 in the same graph');

6
(a) Repeat the problem, but use a different impulse response:
h = ones(1,31)/31;
What should the parameters in part (d) be now?
>> load lab3data;
>> x = lab3data;
>> h = ones(1,31)/31;
>> y = conv(h,x);
>> subplot(2,1,1);
>> plot(x);
>> set(gca,'YGrid','on');
>> legend('show')
>> title('Plot of the x)')
>> xlabel('n');
>> subplot(2,1,2);
>> plot(y);
>> set(gca,'YGrid','on')
>> xlabel('n');
>> title('Plot of the y')

(f) Repeat the problem, but use


h = ones(1,67)/67;
What should the parameters in part (d) be now?\
>> load lab3data;
>> x = lab3data;
>> h = ones(1,67)/67;
>> y = conv(h,x);
>> subplot(2,1,1);
7
>> plot(x);
>> set(gca,'YGrid','on');
>> legend('show')
>> title('Plot of the x)')
>> xlabel('n');
>> subplot(2,1,2);
>> plot(y);
>> set(gca,'YGrid','on')
>> xlabel('n');
>> title('Plot of the y')

Comment on your observations.


As the parameter of the h gets larger the signal, that is the plot of y, is stretching to a longer range
3. Difference Equations
Suppose a system is implemented with the difference equation:

y(n) = x(n) + 2 x(n − 1) − 0.95 y(n − 1)

8
>> n = [0:100];
>> b = [-1 -2];
>> a = [1 0.95];
>> x = [1, zeros(1,100)];
>> y = filter(b,a,x);
>> stem(n,y,'filled','k');
>> xlabel('n');
>> ylabel('y(n)');
>> axis([-2 120 -2 2]);
>> title('Stem plot of y(n)')
>> ylabel('y(n)');
(a) Is this system linear? Use your Matlab function to confirm your answer:
y1 = mydiffeq(x1)
y2 = mydiffeq(x2)
y3 = mydiffeq(x1+2*x2)
Use any signals x1, x2 you like.
(b) Is this system time-invariant? Confirm this in Matlab (how?).

(c) Compute and plot the impulse response of this system. Use x = [1, zeros(1,100)];
as input.
(d) Define x(n) = cos(π n/8) [u(n) − u(n − 50)]. Compute the output of the system in two
ways:
(1) y(n) = h(n) ∗ x(n) using the conv command.
>> n = [0:50];
>> x = cos(pi*n/8).*(stepseq(0,0,50) - stepseq(50,0,50));
>> h = cos(pi*n/8).*(impseq(0,0,50) - impseq(50,0,50));
>> [y,n] = conv_m(x,n,h,n);
>> stem(n,y,'filled','b')

9
>> axis([-20 120 -5 5]);
>> title('stem plot of no.3_1');
>> xlabel('n');
>> ylabel('y(n)');

(2) Use your function to find the output for this input signal.

Are the two output signals you compute the same?


(e) Write a new Matlab function for the system with the difference equation:

y(n) = x(n) + 2 x(n − 1)


Find and plots the impulse response of this system. What is the difference between the two impulse
responses?

10
>> n = [0:1];
>> h = impseq(0,0,1) + 2*impseq(1,0,1);
>> stem(n,h,'filled','b')
>> axis([-20 20 0 5]);
>> xlabel('n');
>> ylabel('y(n)');
>> title('Stem plot of no.3_e');
(f) Write a new Matlab function for the system with the difference equation:
y(n) = x(n) + 2 x(n − 1) − 1.1 y(n − 1)
Find and plots the impulse response of this system. What do you find? Discuss your
observations.
To turn in: The plots, your Matlab commands to create the signals and plots, and discussion.

11

You might also like