Mehran University of Engineering and Technology, Jamshoro Department of Computer Systems Engineering Analogue and Digital Signal Processing Lab 2: Generation of Commonly Used Signals 1. • • Objectives: After this lab, students should be able to: Generate and plot commonly used continuous time signals. Generate and plot commonly used discrete time signals Software Required: MATLAB with Signal Processing Toolbox 2.0 Generation of Periodic Signals: Continuous time 2.1 Continuous time Periodic Triangular Wave: MATLAB has a built-in command sawtooth to generate a periodic triangular waveform. Following example help you draw such a waveform: Example: Draw a triangular wave of amplitude = 2 units, a frequency of π radians per second and a width of 0.5 units. Solution: Use the following code A=2; % amplitude w =pi; % frequency W=0.5; % width t=0:0.001:10; % time vector y=A*sawtooth(w*t+W); plot(t,y) xlabel(‘Time in seconds’) ylabel(‘Triangular Wave’) 2.2 Continuous time Periodic Square Wave A periodic square wave can be generated by using the following command: A*square(w0*t); Example: Generate a square wave of amplitude 1, fundamental frequency 10π radian per second. A=1; w0=10*pi; t=0:0.001:1; % you may also use linspace here q=A*square(w0*t); plot(t, q) axis([0 1 -2 2]) % optional 2.3 Discrete time signals 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 and amplitude = 1 unit. This signal is generated using the following commands: A=1; w=pi/4; n=-10:1:10; x=A*square(w*n); stem(n,x) Exercise 1: (a) Generate a discrete time triangular wave of unity amplitude with width 0 and frequency π/8 radians per second. Plot for 100 seconds (0 to 100 sec). (b) Draw the following sinusoidal signals for 100 seconds: (i) Acos(wn + φ) (ii) Asin(wn + φ) where A = 4; w = π/8 and φ = 30 degrees. Note: Convert degrees into radians. 2.4 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. Exercise 2: Draw the following signals: (a) x(t) = 5e-6t (b) y(t) = 3e5t (b) x(n) = 2(0.85)n (c) z(t) = 60sin(20πt)e-6t (d) y(n) = 60sin(20πn)e-6n. 2.5 Other signals: A discrete time unit step function u(n) may be created as follows: n = 0:1:20; x = ones(1, length(n)); stem(n,x) axis([-1 25 0 2]) % optional Exercise 3: (i) Draw the signal x[n] = n (ramp function) (ii) Plot continuous time and discrete time sinc function (i.e. sin(x)/x) for x between -5 and 5. Use the built-in function sinc. (iii) Plot a rectangular function of width 3 units. (use the built-in function “rectpuls”. (iv) Draw a discrete time triangular pulse using the built-in function “tripuls”. Plot the function from -3 to 3 seconds. (v) Find and plot u(n) – u(n-5), where u(n) is a discrete time unit step signal. 2.6 Even and Odd Signals The even and odd parts of a signal x(t) are computed by using the following formula: 1 [x(t ) + x(− t )] 2 1 x o (t ) = [x(t ) − x( − t )] 2 xe (t ) = Example: Plot the signal given below. Also plot even and odd parts of the signal. 1 -1 1 Solution: Mathematically, the above signal is represented as follows: 12 (t + 1) − 1 ≤ t ≤ 1 x(t ) = elsewhere 0 This signal can be plotted by using the following code t = -1:0.0001:1; x = ½*(t + 1); plot(t, x), grid Now even and odd parts of the signal may be plotted as follows: x1 = fliplr(x); % folded version of the signal xe = ½*(x + x1); % Even part xo = ½*(x – xe); % Odd part subplot(2,1,1) plot(t, xe) xlabel (‘Time’) ylabel(‘Even part of x(t)’) subplot(2,1,2) plot (t, xo) xlabel(‘Time’) ylabel(‘Odd part of x(t)’) Exercise 4: (a) Plot the following signals for -5≤t≤5 seconds. Also plot even and odd parts of each. (i) x(t) = cos(t )+ sin(t) + cos(t)sin(t) (ii) x(t) = (1 + t3)cos3(10t) (b) Repeat exercise 4(a) for the following discrete time signals. (i) x(n) = [1 1 1 1 0 0 0]; (ii) y(n) = [1 0 0 1 0 1 1 0 1 1] (c) Compute the energy of x(n) and y(n) in part (b). Note: Energy of a signal x(n) may be computed as sum(abs(x.^2)).