Lab4 Final
Lab4 Final
Lab4 Final
ECE104.1: Lab #3
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:
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.
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');
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')
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.
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