51645962 Lab Manual Signals and Systems
description
Transcript of 51645962 Lab Manual Signals and Systems
Department of Computer Systems Engineering
The Islamia University of Bahawalpur
University College of Engineering and Technology
Department of Computer Systems Engineering
Lab ManualSignals and Systems
Muhammad Hassan Idrees (08CS11)(5th Semester – Computer Systems Engineering)
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 1
Department of Computer Systems Engineering
Contents
Lab 1An Introduction to MATLAB
Lab 2Using Built-in functions of MATLAB
Using MATLAB Help
Lab 3Plotting Graphs in MATLAB
Lab 4Signal Generation and Processing
Programming and Signal Analysis in MATLAB
Lab 5
To generate a discrete time triangular waveTo generate a triangular waveTo draw the ramp function
Lab 6
Get help for the built-in function”sinc” and hence plot sinc functionPlot a rectangular functionFind and plot u[n]-u[n-5],where u[n] is a discrete time unit step signal.
Lab 7
Find the convolutions sum of user defined inputsFind impulse response and step response of the system described by the difference equation
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 2
Department of Computer Systems Engineering
Lab 1An Introduction to MATLAB
1. What is MATLAB:dicussion.
The name MATLAB is short for MATrix LABoratory. It is a commercial software package whose main function is to perform calculations on matrices, row vectors and column vectors. It is widely used in both industry and academic institutions. It possesses many of the features of a high level, numerically oriented programming language, but in addition has a large collection of built-in functions for performing matrix and vector operations in a way that is very simple for the user. For example, to find the determinant of a matrix A one need only enter:det(A)MATLAB commands can be entered one line at a time, or the user can write programs of MATLAB code and define his or her own functions. In this session, we shall only use the one-line-at-a-time “interpretive” mode for simplicity, but the regular user will find that the power of the package is considerably extended by writing his or her own programs and functions.
2. Entering and quitting MATLAB
To enter MATLAB, simply double click on the MATLAB icon. To leave MATLAB and return to the PC’s operating systemsimply typequit
3. Creating and manipulating matrices and vectors
MATLAB works on matrices and vectors that may be real or complex. Variables do not have to be “declared”. Results can be displayed in a variety of different formats.Let us enter a row vector into the MATLAB workspace. Type in:
v = [2 4 7 5]
This creates a variable v whose current value is a row vector with four elements as shown. After pressing “return” the value of v will have been echoed back to you. To suppress the echo, one uses a semi-colon following the command line. Thus
w = [1 3 8 9];
Creates another row vector w, but does not echo. To check that w has appeared in the MATLAB workspace, type,
who
Which will display all the variables in the MATLAB workspace, and to check the value of w simply type w
Operations on row vectors can be best illustrated by some simple exercises.
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 3
Department of Computer Systems Engineering
Exercise : Investigate the effect of the following commands:(a) v(2) (b) sum = v + w (c) diff = v – w (d) vw = [v w] (e) vw(2:6) (f) v’
One way to generate a column vector is to take the transpose of a row vector, as you will have found in exercise 1(f). Column vectors can be typed in directly in one of two ways. For example, to enter the command vector
=
0
0
1
1
z
you can type
z = [1; 1; 0; 0]; or
z = [1100];
Exercise 2: Investigate the effect of the following commands
(i) z’ (b) z*v (c) [v; w] (d) v*z (e) [z; v’] (f) z + v’
One way of creating matrices is by multiplying row and column vectors as seen in the above exercises. There are various ways of entering matrices. For example, to enter the matrix
=
43
21M
the most obvious ways are to type
M = [1 2; 3 4]; orM = [1 2
3 4];
but there are more perverse ways such as
M = [[1 3]’[2 4]’];
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 4
Department of Computer Systems Engineering
Lab 2
Using Built-in functions of MATLAB
Using MATLAB Help
Built-in Functions and MATLAB help
You have already encountered some built-in functions. In addition to standard arithmetic operators, such as “+” and “*”, you have met inv(A) that generates the inverse of A.
eye(n) that generates the n-by-n identity matrix. MATLAB has a very large number of built-in functions. To see the full list of those variables on your machine type
helpTo get information on a specific function (“inv” for example) type
help invThe help command is very useful. Most of the MATLAB built-in functions have very descriptive names and are easily remembered or guessed.
Exercise : Use the help command to find out the following built-in functions
1.Ones 2:.zeros 3: det 4: Linspace 5:Logspace
• 1-help onesONES(N) is an N-by-N matrix of onesONES (M,N) or ONES (M,N) is an M by-N matrix of ones.
. 2-help zerosZEROS(N) is an N-by-N matrix of zeros.ZEROS(M,N) or ZEROS (M,N)is an M-by –N matrix of zeros.
• 3-help detDET Determent.DET(X) is the determinant of the square matrix X.
• 4 help linespaceLINESPACE Linearly spaced vector.
LINESPACE(X1,X2)generates row vector of 100 linearly equally spaced points between x1,x2LINESPACE(x1,x2,N) generates N points between x1 and x2
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 5
Department of Computer Systems Engineering
• 5-help logspaceLOGSPACE(X1,X2) generates a row vector of 50 logarithmically.
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 6
Department of Computer Systems Engineering
Lab 3
Plotting Graphs in MATLAB
GraphsThe results of signal processing computations in MATLAB are huge matrices containing, for example, frequency responses, which one would like to display in graphical form. We will go through a simple example of generating data, plotting the graph, putting labels on the axes etc. We will plot the graph of
y = sin(t);for t going from 0 to 20 seconds. First we need to generate a vector t containing the values of time that we want to use. Type:
t=linspace(0,20,100);
which makes t into a row vector of 100 values from 0 to 20 inclusive. Then type
ys = sin(t);
To plot the set of points type:
Plot(t,ys)
The basic graph can be prettied up using the following self explanatory sequence of commands:
xlabel(‘Time in seconds’)
ylabel(‘sin(t)’)
title(‘your Roll No.’)
grid
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 7
Department of Computer Systems Engineering
Lab. 4Signal Generation and Processing
.
Generation of signals:
2.1 Periodic Signals:It is an easy matter to generate periodic signals such as square waves, triangular waves and sinusoidal signals
using MATLAB. Consider first the generation of a triangular wave of amplitude A, fundamental frequency w0 (measured in radians per
second) and width W. The basic command for generating this signal is
A*sawtooth(w0*t + W);Thus to generate a triangular wave of amplitude = 1 unit, a frequency of 10π radians per second and a width of 0.5 unit, we use the following commands: A=1; w0=10*pi;W=0.5;»t=0:0.001:1; tr=A*sawtooth(w0*t+W);plot(t,tr)Consider next the generation of a square wave of amplitude A, fundamental frequency w0 and duty cycle ρ . To generate such a signal we use the commandA*square(w0*t + rho);The square wave with amplitude 1, fundamental frequency 10π radian per second and duty cycle = 0.5 can be generated by using the following commands:A=1;w0=10*pi;rho=0.5;t=0:0.001:1; %1 you may also use linspace hereq=A*square(w0*t + rho);plot(t,sq)axis([0 1 -2 2]) % this is an optional command and helps in clear visualization of the signal.
To visualize a discrete time signal, we may use the stem command. Specifically, stem(n,x) depicts the data contained in vector x as a discrete time signal at the time values defined by n. The vectors n and x must, of course, have compatible dimensions.Consider, for example, the discrete time square wave, with frequency π /4 radians per second, duty cycle = 0.5 and amplitude = 1 unit. This signal is generated using the following commands:A=1;w=pi/4;rho=0.5;n=-10:1:10;x=A*square(w*n+rho);
1 In MATLAB workspace, any statement starting with % is a comment.
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 8
Department of Computer Systems Engineering
stem(n,x)
2.2 Exponential Signals:
There are two types of exponential signals: decaying exponentials and growing exponentials. The MATLAB command for decaying exponential Be-at is
B*exp(-a*t);
To generate a growing exponential Beat ,we use command
B*exp(a*t);
In both cases, the exponential parameter a is positive.
2.3 Other Functions:A discrete time unit step function may be created as follows:
n = 0:1:20;
x = ones(1, length(n));
stem(n,x)
axis([-1 25 0 2]) % optional
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 9
Department of Computer Systems Engineering
Programming and Signal Analysis in MATLAB
Lab 5
To generate a triangular wave of amplitude =1 unit, a frequency of 10pi radians per second and a width of 0.5 unit we use the following commands:
A=1; W0=10*pi; W=0.5; T=0:0.001:1;
To generate a discrete time triangular wave of unity amplitude with width 0.5 and frequency 10pi radians per second.
A=1; W0=10*pi; W=0.5; T=0:0.001:1; Tr=A*sawtooth (w0*t+W); Plot(t,tr) x(t)=5e-6t A=5; T=0:0.001:1; B=6; C=A*exp(-B*t); Plot (t,C) (b) y(t)=3e^5t A=3;t=0:0.001:1; B=5; C=A*exp(B*t); Plot(t,C)
To Draw the ramp function
x[n]=n (ramp function) X=n; Stem(n,x) Axix[1 25 0 2]
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 10
Department of Computer Systems Engineering
Lab 6
Get help for the built-in function”sinc” and hence plot sinc function.
help sinc
W=4*pi; T=-5:0.001:5; C=sinc(w*t); Plot(t,C)
Plot a rectangular function of width 3 units function “rectpuls”?
N=-3:0.001:3;C=rectpuls(n);Plot (n,c)
Find and plot u[n]-u[n-5],where u[n] is a discrete time unit step signal.
n=0:1:5; X=ones[1,length(n); Stem(n,x) Axix([1 25 0 2])
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 11
Department of Computer Systems Engineering
Lab 7
Find the convolutions sum of user defined inputs
a=input(‘Type in the first sequence =‘’); B=input(‘Type in the second sequence=‘); C=conv(a,b); M=length(c)-1 N=0:1:M; disp(‘Output sequence=‘); disp(c) step(n,c) xlabel(‘Time index n’) Ylabel(‘Ampltude’) Type in the first sequence=[1 2 3 4] Type in the Second sequence=[1 2 3 4]
• Output sequence= 5 16 34 60 61 52 32
Find impulse response and step response of the system described by the difference equation
Y[n]=0.7y[n-1]-0.1y[n-2]+2x[x]-[n-2] A=[-0.7 0.1]; B=[2 -1]; N=41; X=[1 zeros(1,N-1)]; Yfilter(a,b,x); K=0:1:N-1 Stem(k,y) Xlabel(‘time index’)Ylabel(‘amplitude’)
Lab Manual – Signals and SystemsMuhammad Hassan Idrees – 08CS11 Page 12
-1