Você está na página 1de 13

DIGITAL SIGNAL PROCESSING

Lab Session 1: Operations on digital signals Objectives     Learn how to define and call functions in MATLAB Learn how to define general signals/functions (i.e. unit impulse, unit step, sinusoidal) in MATLAB Learn how to add and multiply two sequences of unequal length. Learn the use of commands 'find', 'stem', 'subplot', 'hold', 'fliplr'.

Types of discrete time signals (sequences): Exercise #1.1 Unit Impulse/Sample Sequence, H(n) Function H(n) has value zero everywhere except at n=0.

The following code was used to get the figure shown above. n0 defines the location where the impulse is located. >>function [x, n] = impseq(n0, n1, n2) >>n= [n1:n2]; >>x= [(n-n0) ==0] >>stem (n, x)
Prepared By Sabeena Fatima

1. Write this function in MATLAB and copy your figure in the space provided for any values of n0, n1 and n2. (3 points)

1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 -2

-1

2. Now change the input values you provided to call the function above and notice the changes. How does the output (figure) of the called function changes with changing the second and third input values? (3 points) Changing the second and third input values to the function varies the range of the function output. 3. What does the stem command do? Change the stem command in your function defined above with plot command, observe the change. Give your comments on the difference in using stem or plot command in the space provided below. (2 points) Stem command plots the dicrete values on the graph whereas the Plot command maps the vector x onto each continuous value of vector n.

4. Explain the use of the logical operation, == used in the statement, x= [(n-n0) ==0] in the function defined above. (2 points) This logical operation compares the instant where the n0 values has to be marked with reference to all others discrete locations. The impulse is at this location.

Prepared By Sabeena Fatima

Exercise # 1.2 Unit Step Sequence u(n):

 1. Using your understating of the function defined for impulse sequence write down the function to generate a plot for unit step sequence. Your function should plot a graph shown above. Write your code in the space provided below. (3 points) Code:
function[x,n]=stepseq (n0,n1,n2) n0=0; n1=-8; n2=8; if ((n0<n1)|(n0>n2)|(n1>n2)) error ('arguments must satisfy n1<=n0<=n2') end n=[n1:1:n2]; x=[(n-n0)>=0]; stem(n,x)

2. Using the unit step function defined in step 1, plot the step sequence for  5 e n e 5 . (2 points)

Prepared By Sabeena Fatima

Figure:
1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 -5

-4

-3

-2

-1

Exercise # 1.3 Sinusoidal Sequence Define two periodic signals in MATLAB x(n ) ! 3 v cos( nT / 3) and y (n) ! 2 sin(5Tn) for 0 n 10 in step size of 0.1. Plot the resulting graphs in the same figure window. Properly label your plotted figure. (Hint: You will need to use hold command to plot the signals on the same figure window. Also use i. xlabel command to label x-axis in the graph plotted above, ii. ylabel command to label x(n) and y(n) in the graph plotted iii. gtext command to write the equations of x(n) and y(n) on the plotted graph. Paste the figure plotted in the space provided below. (5 points)

Prepared By Sabeena Fatima

2 y = (3c os (n*pi/3)) and y1 = (2s in(5*pi*n))

-1

-2

-3

5 6 0< = n< = 10

10

VARIOUS OPERATIONS ON DISCRETE TIME SIGNALS/ SEQUENCES Exercise #1.4 Signal Addition To add two functions in MATLAB we simply use + operator. To add two functions in MATLAB the sequences must be of the same length. If sequences are of unequal lengths or if the sample positions are different then we cannot directly use the operator '+'. For the signals of unequal length or different positions we first augment x1(n) and x2(n) with zeros so that they have the same position vector n. Only then the sequences can be directly added. Code to augment and add two signals of unequal length is shown below:

Prepared By Sabeena Fatima

The output of the above code looks like the figure shown below:

function[y,n]=sigadd(x1,n1,x2,n2) n1=[1,2,3,4,5]; n2=[-2,-1,0,1,2,3,4]; x1=[1,3,7,9,7];


Prepared By Sabeena Fatima

x2=[5,8,1,3,4,2,1]; n=min(min(n1),min(n2)):max(max(n1),max(n2)); y1=zeros(1,length(n)); y2=y1; y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; y=y1+y2; subplot(3,1,1) stem(n1,x1) subplot(3,1,2) stem(n2,x2) subplot(3,1,3) stem(n,y) 1. Write the function in MATLAB. Briefly explain what you understand by the (2 points) command find. It is to set a value for the output of the function at the specific instant of the input n. This n is the point where the output of the function is to be mapped onto.

2. Let z1 (n1 ) = {0,3,2,1} , z2(n2 ) = {1,2,3,0} , n1={1,2,3,4}, n2 = {-1,0,1,2). Plot z1(n1), z2 (n2 ) and z1(n1 ) + z2(n2 ) on the same graph. Write the code in the space provided also copy the figure in the space provided. (Hint: You will need to use command subplot) (5 points) function[y,n]=sigadd(x1,n1,x2,n2) n1=[1,2,3,4]; n2=[-1,0,1,2]; x1=[0,3,2,1]; x2=[1,2,3,0]; n=min(min(n1),min(n2)):max(max(n1),max(n2)); y1=zeros(1,length(n)); y2=y1; y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; y=y1+y2;

Prepared By Sabeena Fatima

subplot(3,1,1) stem(n1,x1) subplot(3,1,2) stem(n2,x2) subplot(3,1,3) stem(n,y)

3 2 1 0 3 2 1 0 -1 3 2 1 0 -1 -0.5 0 0.5 1 1.5 2 2.5 3 3.5 4 -0.5 0 0.5 1 1.5 2 1 1.5 2 2.5 3 3.5 4

Exercise #1.5 Signal Multiplication Similar restrictions apply for multiplication as for addition.

Prepared By Sabeena Fatima

1. Using your understanding of the function signal addition write a function for multiplying two signals of unequal lengths. (3 points) function[y,n]=sigmull(x1,n1,x2,n2) n1=[1,2,3,4,5]; n2=[-2,-1,0,1,2,3,4]; x1=[1,3,7,9,7]; x2=[5,8,1,3,4,2,1]; n=min(min(n1),min(n2)):max(max(n1),max(n2)); y1=zeros(1,length(n)); y2=y1; y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; y=y1.*y2; subplot(3,1,1) stem(n1,x1) subplot(3,1,2) stem(n2,x2) subplot(3,1,3) stem(n,y)

2. Let z1 (n1 ) = {0,3,2,1} , z2(n2 ) = {1,2,3,0} , n1={1,2,3,4}, n2 = {-1,0,1,2). Multiply the (3 points) two sequences. Paste your figure in the space provided.

Prepared By Sabeena Fatima

3 2 1 0 3 2 1 0 -1 1 -0.5 0 0.5 1 1.5 2 1 1.5 2 2.5 3 3.5 4

-1 -1

-0.5

0.5

1.5

2.5

3.5

Exercise #1.6 Signal Shift x(n  N ) implies x (n) shifted to the left by N; x(n  N ) implies x(n) shifted to the right by N. The figure shows sequence x (n) and its shifted version:.

Prepared By Sabeena Fatima

1. Write a function that generates a shifted sequence. (5 points) function[y,n]=sigshift(x1,n1) n1=[1,2,3,4,5]; N=3; x1=[1,3,7,9,7]; n=n1-N; y=x1; subplot(2,1,1) stem(n1,x1) subplot(2,1,2) stem(n,y)

2. By how much is x (n) shifted? N=3;

(2 points)

Prepared By Sabeena Fatima

Exercise #1.7 Signal Flip 1. Use the MATLAB function fliplr to define x(-n). Let x(n) ! {1,2,5,8,3,2} plot x(n) and x(-n) on the same figure. x(0)=5. Output is shown below. (5 points) Note: Since the fliplr function flips the terms of x about its center proper indexing is required i.e. keep a check on n. function[y,n]=flip(x1,n1) n1=[1,2,3,4,5]; x1=[1,3,7,9,7]; n=-fliplr(n1); y=fliplr(x1); subplot(2,1,1) stem(n1,x1) subplot(2,1,2) stem(n,y)

Prepared By Sabeena Fatima

Figure:

8 6 4 2 0 -2 8 6 4 2 0 -3 x (-n) x (n)

-1.5

-1

-0.5

0.5

1.5

2.5

-2.5

-2

-1.5

-1

-0.5

0.5

1.5

Prepared By Sabeena Fatima

Você também pode gostar