ECE2111 Signals and Systems

Total Page:16

File Type:pdf, Size:1020Kb

ECE2111 Signals and Systems

ECE2111 Signals and Systems UMD, Fall 2009 Experiment 1: Representation and manipulation of basic signals in MATLAB

MATLAB is a tool for doing numerical computations with matrices and vectors. It can also display information graphically. The best way to learn MATLAB is to start with matrices.

1. - Entering a two 3x3 matrices and storing them in the variables A & B

A = [ 2 3 5 ; 6 3 2 ; 3 6 8 ]; A

B = [ 6 4 8 ; 9 5 3 ; 2 4 8 ]; B

2. - Compute the square of matrix A

Asqr = A * A

3. - Compute the product A * B

AB = A * B

4. - Compute the product B * A

BA = B * A

5. - Add the two matrices A + B

Sum = A + B

6. - Invert the matrix Sum iSum = inv(Sum)

Page 1 of 6 I. Generating Continuous Time Signals MATLAB only deals with discrete time signals, continuous time signals are represented in the form of discrete time signals.

7.- Plot y = sin(t) on the interval t = 0 to t = 10 then plot X = e(-2*t), Z = e(-0.2*t) , Q = e(-0.02*t) The x-axis: in the time interval: 0 < t < 10 with increments of 0.3 t = [0:0.3:10]; y = sin(t); plot(t,y); x = exp(-2*t); z = exp(-0.2*t); q = exp(-0.02*t); plot(t,x,t,z,t,q);

8.- Plot the complex exponential signal: y = ej(π/8 t) t = [0:0.3:20] y = exp(j*(pi/8)*t) plot(t,y)

Question 1: What Warning do you get from Matlab after typing the codes above? What you can do to improve?

9.- Labeling plots and generating subplots Repeat example 8) using subplots and labeling the graphs t = [0:0.3:20]; y = exp(j*(pi/8)*t); subplot(2,2,1),plot(t,real(y)) title('Real part of the signal exp(j*pi/8)*t ') xlabel('time (sec) ') ylabel('Real part of y ') subplot(2,2,2),plot(t,imag(y)) title('Imaginary part of the signal exp(j*pi/8)*t ') xlabel(' time (sec) ') ylabel('Imaginary part of y ') subplot(2,2,3), plot(t,abs(y)) title('Magnitude of the signal exp(j*pi/8)*t ') xlabel(' time (sec) ') ylabel('Magnitude of y ') subplot(2,2,4), plot(t,angle(y)) title('Phase of the signal exp(j*pi/8)*t ') xlabel(' time (sec) ') ylabel('Phase of y (radians) ')

Now replace the highlighted lines with the following: %NOTE: to convert the radians to degrees use: plot(t,angle(y)*(180/pi)) subplot(2,2,4), plot(t,angle(y)*(180/pi))

Page 2 of 6 title('Phase of the signal exp(j*pi/8)*t ') xlabel(' time (sec) ') ylabel('Phase of y (degrees) ')

Question 2: What’s the difference after changing the codes?

II. Operation with signals MATLAB allows you to add, subtract, multiply, divide, scale, and exponentiate signals. Be careful! The vector representation of the signals should have the same time origins and the same number of elements.

10.- Given the signals x1 and x2 perform the following operations: 3 y1 = x1 + x2 ; y2 = x1 - x2 ; y3 = x1 * x2 ; y4 = x1 / x2 ; y5 = 2 x1 ; y6 = (x1)

Matlab codes are given as follows:

% - A symbol used to make comments for coding; codes starting with’%’ are not executed

% Defining the signals x1 = 5*sin((pi/4)*[0:0.1:15]); x2 = 3*cos((pi/7)*[0:0.1:15]); % Plotting the signals subplot(2,4,1), plot(x1) title('x1 = 5 sin(pi/4)t ') xlabel(' time (sec) ') ylabel('x1 (volts) ') subplot(2,4,2), plot(x2) title('x2 = 3 cos(pi/7)t ') xlabel(' time (sec) ') ylabel('x2 (volts) ') % Addition y1 = x1 + x2; % addition y2 = x1 - x2; % subtraction y3 = x1 .* x2; % multiplication y4 = x1 ./ x2; % division y5 = 2*x1; % scaling y6 = x1 .^3; % exponentiation % Plotting the signals subplot(2,4,3), plot(y1) title('y1 = x1 + x2 ') xlabel(' time (sec) ') ylabel('y1 (volts) ') subplot(2,4,4), plot(y2) title('y2 = x1 – x2 ')

Page 3 of 6 xlabel(' time (sec) ') ylabel('y2 (volts) ') subplot(2,4,5), plot(y3) title('y3 = x1 * x2 ') xlabel(' time (sec) ') ylabel('y3 (volts)^2 ') subplot(2,4,6), plot(y4) title('y4 = x1 / x2 ') xlabel(' time (sec) ') ylabel('x1/x2 ') subplot(2,4,7), plot(y5) title('y5 = 2*x1 ') xlabel(' time (sec) ') ylabel('y5 (volts) ') subplot(2,4,8), plot(y6) title('y6 = x1 ^ 3 ') xlabel(' time (sec) ') ylabel('y6 (volts)^3 ')

III. Generating Discrete Time Signals

11.- Plot the discrete time signal defined as: x[n] = 2n in the time interval: -3 < n < +3, 0 otherwise a) Plotting only the interval [ -3 +3 ] n = [-3: 3] x = 2 * n stem(n,x) b).- Plotting the time interval [ -5 +5 ] n = [-5: 5] x = [ 0 0 x 0 0] stem(n,x) c).- Plotting the time interval [ -50 +50 ] n = [-50:50] x = [zeros(1,45) x zeros(1,45)]; stem(n,x)

12.- Generate and plot the sequence g[n] = A (a )n with A = 10 and a = -0.9, in the interval [-10 +10] n = [-10:1:10] ; % defining the samples g = 10*(-0.9).^n; % defining the sequence figure % open a new window for the graphic stem(n,g) axis([-10, 10, -30, 30]);

Page 4 of 6 xlabel('sample number, n '); ylabel('g[n] '); title('Exponential sequence '); grid text(0.6, 15, 'g[n] = 10(-0.9).^n ');

Control flow MATLAB has the following flow control constructs: • if statements • switch statements • for loops • while loops • break statements The if, for, switch and while statements need to terminate with an end statement.

Create and run m-files containing the following examples:

M-files: To open an M-file you have to go to File menu and select New M-file. Then the M-file editor will show up. After writing your program, save it to the only place to save file on desktop.

To run the M-file in the command window, first specify the location of the file by typing the following: cd C:\Documents and Settings\ece\desktop\the only place to save

To run m-files, type the name in the command window. if statement: x=-3; if x>0 str='positive'; elseif x<0 str='negative'; elseif x==0 str='zero'; else str='error'; end What is the value of ’str’ after execution of the above code?

Hint: to get the value of a variable, type the name of it. while statement:

Shortcut to convert between codes and comments:

Page 5 of 6 Select the codes->Ctrl R: to convert the codes to comments (starting with %) without deleting them, if you don’t want to run them now but probably want to use them later Select the codes->Ctrl T: to convert the comments back to codes x=-10; while x<0 x=x+1; end

What is the value of x after execution of the above loop? for loop: x=0; for i=1:10 x=x+1; end

What is the value of x after execution of the for loop? break statement: The break statement lets you exit early from a for or a while loop: x=-10; while x<0 x=x+2; if x==-2 break; end end

What is the value of X? Report Requirements:

1- Individual lab report

2- Attached with the cover page (available on website)

3- Due on Feb 4 (Thursday)

This lab report should contain the following:

 Present the results from 2 - 6  Print out all the plots of 7 - 12 and provide explanation  Answer the question in 8 and 9  Provide the results from Control Flow  A list of new Matlab commands you learned from this experiment

Page 6 of 6

Recommended publications