Lab Manual
Signals and Systems Muhammad Hassan Idrees (08CS11) (5th Semester Computer Systems Engineering)
Page 1
Contents
Lab 1 An Introduction to MATLAB Lab 2 Using Built-in functions of MATLAB Using MATLAB Help Lab 3 Plotting Graphs in MATLAB Lab 4 Signal Generation and Processing
Programming and Signal Analysis in MATLAB Lab 5 To generate a discrete time triangular wave To generate a triangular wave To draw the ramp function Lab 6 Get help for the built-in functionsinc and hence plot sinc function Plot a rectangular function Find 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 inputs Find impulse response and step response of the system described by the difference equation
Page 2
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.
Page 3
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
1 1 z = 0 0
you can type z = [1; 1; 0; 0]; or z = [1 1 0 0]; 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
1 M = 3 2 4
the most obvious ways are to type M = [1 2; 3 4]; or M = [1 2 3 4]; but there are more perverse ways such as M = [[1 3][2 4]];
Page 4
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 help To get information on a specific function (inv for example) type help inv The 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 ones ONES(N) is an N-by-N matrix of ones ONES (M,N) or ONES (M,N) is an M by-N matrix of ones. . 2-help zeros ZEROS(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 det DET Determent. DET(X) is the determinant of the square matrix X. 4 help linespace LINESPACE Linearly spaced vector. LINESPACE(X1,X2)generates row vector of 100 linearly equally spaced points between x1,x2 LINESPACE(x1,x2,N) generates N points between x1 and x2
Page 5
Page 6
Graphs The 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
Page 7
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 w 0 and duty cycle . To generate such a signal we use the command A*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 here q=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
Page 8
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:
Page 9
To Draw the ramp function x[n]=n (ramp function) X=n; Stem(n,x) Axix[1 25 0 2]
Page 10
Lab 6
Get help for the built-in functionsinc 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])
Page 11
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)
-1
Page 12