









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Generating plots using matlab software and manually plotting it then verification of plot
Typology: Lab Reports
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Name: COSAIN, Saimah S. ECE104.1: Lab #
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 = 3impseq(-2, 0 ,1 5 ); x2 = impseq(1, 0 ,1 5 ); x3 = 2impseq(3, 0 ,1 5 ); [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:
For g(n) = u(n + 4)− u(n − 3)
n1 = [- 5 :10]; x1 = stepseq(-4, 0 ,1 5 ); 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)')
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 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 do not have the same 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.
load lab3data.m; whos lab3data; x = lab3data; h = ones(1,11)/11; y = conv(x,h); subplot( 3 11) plot(x,’b’); title('Plot of x(n)') ylabel('y(n)'); xlabel('n'); subplot( 3 ,1,2); stem(x); title('Stem plot of x(n)'); ylabel('y(n)'); xlabel('n'); subplot( 3 ,1,3); plot(y); title('plot of y(n)'); ylabel('y(n)'); xlabel('n');
(a) Can you get y(n) to “line up” with x(n)? No,they almost line up but the y(n) has longer length than the signal x(n).
% Plotting the x(n) and y(n) on the same graph. plot(y,'b'); hold on plot(x,'r'); set(gca,'YGrid','on'); xlabel('n'); ylabel('y(n)'); title('lining up the two signals'); legend('signal y(n)','signal x(n)'); LINING UP THE TWO SIGNALS:
Plot x and y2 on the same graph. What do you notice now? Both the signal have lined up but the signal x(n) is not smoother unlike the signal y2(n) whos line is much smoother. MATLAB COMMAND AND CODE:
% Plotting the x(n) and y 2 (n) on the same graph. plot(y 2 ,'b'); hold on plot(x,'r'); set(gca,'YGrid','on'); xlabel('n'); ylabel('y(n)'); title('lining up the two signals'); legend('signal y 2 (n)','signal x(n)'); SETTING PARAMETERS FOR THE y(n) TO LINE UP WITH THE SIGNAL x(n) (a) Repeat the problem, but use a different impulse response: h = ones(1,31)/31; What should the parameters in part (d) be now? New Parameter: y2 = y; y2(1: 15 ) = []; plot(x,’b’); >> title('Plot of x(n)') >> ylabel('y(n)'); >> xlabel('n'); >> subplot( 3 ,1,2); >> stem(x); >> title('Stem plot of x(n)'); >> ylabel('y(n)'); >> xlabel('n'); >> subplot( 3 ,1,3); >> plot(y); >> title('plot of y(n)'); >> ylabel('y(n)'); >> xlabel('n'); (a) Can you get y(n) to “line up” with x(n)? No,they almost line up but the y(n) has longer length than the signal x(n). >> % Plotting the x(n) and y(n) on the same graph. >> plot(y,'b'); >> hold on >> plot(x,'r'); >> set(gca,'YGrid','on'); >> xlabel('n'); ylabel('y(n)'); >> title('lining up the two signals'); >> legend('signal y(n)','signal x(n)'); LINING UP THE TWO SIGNALS: Plot x and y2 on the same graph. What do you notice now? Both the signal have lined up but the signal x(n) is not smoother unlike the signal y2(n) whos line is much smoother. MATLAB COMMAND AND CODE: >> % Plotting the x(n) and y 2 (n) on the same graph. >> plot(y 2 ,'b'); >> hold on >> plot(x,'r'); >> set(gca,'YGrid','on'); >> xlabel('n'); ylabel('y(n)'); >> title('lining up the two signals'); >> legend('signal y 2 (n)','signal x(n)'); SETTING PARAMETERS FOR THE y(n) TO LINE UP WITH THE SIGNAL x(n) (a) Repeat the problem, but use a different impulse response: h = ones(1,31)/31; What should the parameters in part (d) be now? New Parameter: y2 = y; y2(1: 15 ) = []; y2(end- 14 :end) = []; MATLAB COMMAND AND CODE: h = ones(1,31)/31;
y = conv(h,x); subplot( 2 ,1,1); plot(x); set(gca,'YGrid','on'); title('Plot of the x(n)') xlabel('n'); ylabel('y(n)'); legend('Signal x(n)'); subplot(2,1,2); plot(y); set(gca,'YGrid','on') xlabel('n'); title('Plot of the y(n)') legend('Signal y(n)'); NEW PLOT OF THE SIGNALS WITH h = ones(1,31)/31; Length(x) = 600 Length(y) = 630 USING THE NEW PARAMETERS: y2 = y; y2(1: 15 ) = []; y2(end- 14 :end) = []; subplot(311) plot(x,'r'); xlabel('n'); ylabel('y(n)'); title('Plot of the Signal x(n)'); subplot(312);
set(gca,'YGrid','on'); xlabel('n'); ylabel('y(n)'); title('lining up the two signals'); legend('signal x(n)','signal y(n)'); LINING UP THE TWO SIGNALS: (f) Repeat the problem, but use h = ones(1,67)/67; What should the parameters in part (d) be now? New Parameter: y2 = y; y2(1: 33 ) = []; y2(end- 32 :end) = []; load lab3data.m x = lab3data; h = ones(1,67)/67; y = conv(h,x); subplot(211) plot(x,'b'); set(gca,'YGrid','on'); legend('Signal x(n)'); title('Plot of the x(n)');
xlabel('n'); ylabel('y(n)'); subplot(2,1,2); plot(y,'b'); set(gca,'YGrid','on') xlabel('n'); ylabel('y(n)'); legend('Signal y(n)'); title('Plot of the y(n)'); NEW PLOT OF THE SIGNALS WITH h = ones(1, 67 )/ 67 ; Length(x) = 600 Length(y) = 666 USING THE NEW PARAMETERS: y2 = y; y2(1: 33 ) = []; y2(end- 32 :end) = []; subplot(311) plot(x,'r'); xlabel('n'); ylabel('y(n)'); title('Plot of the Signal x(n)'); legend('Signal x(n)'); set(gca, 'YGrid','on'); subplot(312); plot(y,'r'); xlabel('n'); ylabel('y(n)'); title('Plot of the old signal y(n) '); set(gca, 'YGrid','on'); legend('Signal y(n)'); subplot(313); plot(y2,'r'); xlabel('n'); ylabel('y(n)');
Comment on your observations. **Upon observing the behavior of the plots as the length of the h gets longer and the input value of h gets smaller the signal after using the conv command the signal y(n) gets smoother and smoother but the two signals don’t line up togethers. Observing the first input signal of h the signal y(n) almost line up perfectly to the signal x(n) but the less smoother but as the h changes the line becomes smoother but don’t line up to the signal x(n)
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+2x2) 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. title('Plot of the old signal y(n) '); >> set(gca, 'YGrid','on'); >> legend('Signal y(n)'); >> subplot(313); >> plot(y2,'r'); >> xlabel('n'); ylabel('y(n)'); Comment on your observations. Upon observing the behavior of the plots as the length of the h gets longer and the input value of h gets smaller the signal after using the conv command the signal y(n) gets smoother and smoother but the two signals don’t line up togethers. Observing the first input signal of h the signal y(n) almost line up perfectly to the signal x(n) but the less smoother but as the h changes the line becomes smoother but don’t line up to the signal x(n) 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) >> 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+2x2) 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: subplot(313); >> plot(y2,'r'); >> xlabel('n'); ylabel('y(n)'); Comment on your observations. Upon observing the behavior of the plots as the length of the h gets longer and the input value of h gets smaller the signal after using the conv command the signal y(n) gets smoother and smoother but the two signals don’t line up togethers. Observing the first input signal of h the signal y(n) almost line up perfectly to the signal x(n) but the less smoother but as the h changes the line becomes smoother but don’t line up to the signal x(n) 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) >> 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+2x2) 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(pin/8).(stepseq(0,0,50) - stepseq(50,0,50)); h = cos(pin/8).*(impseq(0,0,50) - impseq(50,0,50)); [y,n] = conv_m(x,n,h,n); stem(n,y,'filled','b')
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.