Lab03
description
Transcript of Lab03
EE213: Introduction to Signal Processing Fall semester, 2015
Lab 3: Basic Operations on Discrete Signals
1 Objectives
• Represent discrete signals in MATLAB.
• Perform basic operation on discrete signals.
2 Introduction
2.1 Plot discrete signals
A discrete signal is commonly described in a form which is similar to the following expression
x[n] = {1, 2, 3↑
n=0
, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1} (1)
where the up arrow ↑ indicates the sample when n = 0. In the above expression we havex[−2] = 1, x[−1] = 2, x[0] = 3, x[1] = 4, ..., x[10] = 1. It is also implicitly assumed that x[n] = 0 ifn < −2 or n > 10. Note that, in MATLAB, index of a vector starts from 1. Thus, we cannot writex(−1)=2 or x(0)=3 for the signal above in MATLAB.
To represent a discrete signal in MATLAB, a good way is to use two vectors, one for storingthe values of the signal (i.e., x[n]) and the other one for storing the sample points (i.e., n). For thesignal given in (1) we can do it as follows
n=−2:10;x = [1 :7 6 : −1:1 ] ;
The important thing that should kept in mind is that that x(1) is the value of the signal at the samplepoint n(1). Furthermore we usually use the stem command to plot discrete signals. For example,the following code plots the signal given in (1).
n = −2:10;x = [ 1 : 7 6 : −1:1 ] ;stem (n , x ) ;x l abe l ( ’ n ’ ) ;y l abe l ( ’ x [ n ] ’ ) ;x l im ([−2.1 1 0 ] ) ;se t ( gca , ’ XTick ’ ,n ) ;
NOTE: If you copy the above code in MATLAB, there are possibly some redundant spaceswhich should be deleted before running the code.
EE213: Introduction to Signal Processing Fall semester, 2015
2.2 Time shifting
Suppose we have a discrete signal given by
x[n] = {2, 2, 1, 5↑, 4, 5}
The graphical representation is shown in the following figure
n
x[n]
0 1 2 3 4−4 −3 −2 −1
2 2
1
54
5
Now we want to create a shifted sequence
y[n] = x[n− 4] (2)
That is, y[n] is created by shifting x[n] by 4 samples to the right. We can rewrite (2) as
y[n+ 4] = x[n]
We can see that the vector that collects the sample index of y is changed by adding 4 to eachelement of that of x. Consequently, we can use the following code to obtain y
n = ( −3:2) ; % sample index of xx = [2 2 1 5 4 5 ] ; % values of xy = x ;n1 = n+4; % n1 i s the sample index of y
In general we can use the following function to create a shifted signal
f unc t i on [ xsh i f t ed , nsh i f t ed ]= mysh i f t ( x , n , n0 )x sh i f t e d = x ;nsh i f t ed = n+n0 ;
In the above function, x and n are the vectors that store the values and sample index of theinput signal, and n0 indicates the shifting parameter. When n0 is positive, the function shifts theoriginal signal by n0 samples to the right, otherwise it shifts the original signal by n0 samples tothe right.
2.3 Time reversal
Suppose we want to created a time-reversed signal of the signal given in (2). Mathematicallyspeaking, the time-reversed signal is written as
y[n] = x[−n]
equivalentlyy[−n] = x[n]
EE213: Introduction to Signal Processing Fall semester, 2015
The flipping operation is illustrated by the figure below.
n
x[n]
0 1 2 3 4−4 −3 −2 −1
2 2
1
54
5
n
y[n] = x[−n]
0 1 2 3 4−4 −3 −2 −1
2 2
1
54
5
The folding operation can be implemented by the following function
f u n c t i o n [ x f o l d , n f o l d ]= myfo ld ( x , n )n f o l d = −n ( end : −1 :1 ) ; % x ( end ) i s the l a s t e n t r y o f x .x f o l d = x ( end : −1 :1 ) ;
A more efficient code which exploits Matlab built-in functions is given below (type help fliplr forfurther information)
f u n c t i o n [ x f o l d , n f o l d ]= myfo ld ( x , n )n f o l d = − f l i p l r ( n ) ; % x ( end ) i s the l a s t e n t r y o f x .x f o l d = f l i p l r ( x ) ;
2.4 Addition
Care should be taken when adding two discrete signals. If the two signals are of unequal lengthsor if the sample points are different for equal-length sequences, then we cannot directly usethe operator + in MATLAB. The following two examples illustrate this point.
Example 1 (two signals of unequal lengths)
Consider two signals x1[n] and x2[n] given by
x1[n] = {1, 5↑, 4, 5}
andx2[n] = {−2, 2
↑, 4}
To manually add the two signals, we can create the following table
n -1 0 1 2x1[n] 1 5 4 5x2[n] -2 2 4 0
x3[n] = x1[n] + x2[n] -1 7 8 5
In the above table the first row indicates the sampling points. Note also that we manuallyassign the value of x2[n] when n = 2 to be 0. From the above table we can write the sum x3[n] =x1[n] + x2[n] as
x3[n] = {−1, 7↑, 8, 5}
In MATLAB we would write
EE213: Introduction to Signal Processing Fall semester, 2015
x1 = [ 1 , 5 , 4 , 5 ] ;n1 = −1:2;
and
x2 = [ −2 ,2 ,4 ] ;n2 = −1:1;
to represent x1[n] and x2[n], respectively. However, if we put
x3 = x1+x2
in MATLAB, there will be an error because x1 and x2 are of unequal lengths. In this case weneed to represent x2 asx2 = [ −2 ,2 ,4 ,0 ] ; % add 0 to x2n2 = −1:2; % add the t ime index 2 to n2
Now
x3 = x1+x2
will give the correct answer.
Example 2 (two signals of different sampling points)
Consider another example wherex1[n] = {1, 5
↑, 4, 5}
andx2[n] = {−3,−2, 2
↑, 4}
By carefully adding the values of the two signals at the correct sampling points, then the signalx3[n] = x1[n] + x2[n] is (check it !!!)
x3[n] = {−3,−1, 7↑, 8, 5} (3)
However, if we just type
x3 = x1+x2
in MATLAB (try it), there won’t be any error (since two signals have the same length) but what weget is
x3 = [−2 ,3 ,6 ,9]
which is a wrong answer. The reason is that the two signals x1[n] and x2[n] are not aligned in thesame position vector. Note that the position vectors of x1[n] and x2[n] are
n1 = −1:2;
and
n2 = −2:1
respectively. We can see that n1 ̸=n2. From the above analysis, to perform x1[n] + x2[n] we needto
EE213: Introduction to Signal Processing Fall semester, 2015
• create a vector that contains all sample points of the resulting signal. In the example above,the sample points of the sum x3[n] are from −2 to 2.
• argument the two signals so that they have the same position vector.
In the above example, if we extend x1[n] and x2[n] by adding a proper number of zeros, i.e.,
x1 = [ 0 , 1 , 5 , 4 , 5 ] ;
and
x2 = [−3 ,−2 ,2 ,4 ,0] ;
Then the sum of x1[n] and x2[n] is simply obtained by using the operator + as
x3 = x1+x2 ;
which yields the correct answer
x3 = [−3 ,−1 ,7 ,8 ,5] ;
The vector of sample points is found as
n3 = −2:2
The following m-function returns the sum of two signals of arbitrary sizes.
f unc t i on [ y , n ] = mysigadd ( x1 , n1 , x2 , n2 )n = min ( n1 (1 ) , n2 (1 ) ) :max( n1 ( end ) ,n2 ( end ) ) ;i f ( n1 (1 ) >n2 (1 ) ) % the sampling po in t o f x1 s t a r t s e a l i e r than
than of x2x1 = [ zeros (1 , n1 (1 )−n2 (1 ) ) x1 ] ; % add 0s to x1
else % the sampling po in t o f x2 s t a r t s e a l i e r than than of x1x2 = [ zeros (1 , n2 (1 )−n1 (1 ) ) x2 ] ; % add 0s to x2
end
i f ( n1 ( end )>n2 ( end ) )x2 = [ x2 zeros (1 , n1 ( end )−n2 ( end ) ) ] ;
e lsex1 = [ x1 zeros (1 , n2 ( end )−n1 ( end ) ) ] ;
endy = x1+x2 ;
In Matlab the command zeros(m,n) creates a zero matrix of size m × n. The following codecan do the same but is much more efficient in MATLAB
f unc t i on [ y , n ] = mysigadd ( x1 , n1 , x2 , n2 )n = min ( n1 (1 ) , n2 (1 ) ) :max( n1 ( end ) ,n2 ( end ) ) ; % n conta ins a l l
sampling po in t sy1 = zeros (1 , leng th ( n ) ) ;y2 = y1 ;y1 ( f i n d ( ( n>=n1 (1 ) ) &(n<=n1 ( end ) ) ==1) ) = x1 ;y2 ( f i n d ( ( n>=n2 (1 ) ) &(n<=n2 ( end ) ) ==1) ) = x2 ;y = y1+y2 ;
The MATLAB representation of two important elementary signals are described below.
EE213: Introduction to Signal Processing Fall semester, 2015
2.5 Unit sample sequences
To implement
δ[n− n0] =
{1 n = n0
0 n ̸= n0
over the n1 ≤ n0 ≤ n2 interval, we can use the following code
n = n1 : n2 ;x = zeros ( n2−n1+1 ,1 ) ;x ( n0−n1+1) = 1;
or a better way is
n=n1 : n2 ;x = [ ( n−n0 )==0 ] ;
2.6 Unit step sequences
To implement
u[n− n0] =
{1 n ≥ n0
0 n < n0
over the n1 ≤ n0 ≤ n2 interval, we can use the following code
n = n1 : n2 ;x = [ ( n−n0 ) >=0] ;
3 Procedures
Let x[n] = {1, 2, 3↑, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1}. Write an M-file to determine and plot the following signals
1. x1[n] = x[n− 2] (see the example in Section 2.2).
2. x2[n] = x[−n] (see the example in Section 2.3).
3. x3[n] = x[2− n] (i.e., folding and shifting).
4. x4[n] = 2x[n− 5]− 3x[n+ 4] (see the example in Section 2.4).
5. x5[n] = x[3− n] + x[n]x[n− 2] (You may need to write an m- function that returns the multi-plication of two signals, which follows the same steps as those for addition, except replacing+ by .* ).
6. x6[n] = x[2n− 1] (downsampling and shifting)
7. x7[n] = x[n/2 + 1] (upsampling and shifting)
Write an M-file to generate and plot each of the following sequences
8. x[n] = 2δ[n+ 2]− δ[n− 4], −5 ≤ n ≤ 5
9. x[n] = n [u[n]− u[n− 10]] + 10e−0.3(n−10) [u[n− 10]− u[n− 20]]
EE213: Introduction to Signal Processing Fall semester, 2015
4 Reports
Your report should have the following structure
• All the plots you obtained during the lab and the your comments and opinions on the results.
• A summary of what you gained in the lab.
4.1 Submission
• Each student submits a single report.
• To be uploaded via moodle before 6PM the following day.
• A penalty 10% of each day will be applied to late submission.
• Poorly written report is subject to deduction.