Академический Документы
Профессиональный Документы
Культура Документы
Faculty of Engineering
Electrical Engineering Department
Spring-2012
ENG.MOHAMMED ELASMER
______________________________________________________________________________
DSP Laboratory (EELE 4110)
Lab#3 Discrete Time Signals
DISCRETE-TIME SIGNALS
Signals are broadly classified into analog and discrete signals. An analog signal will
be denoted by x(t), in which the variable t can represent any physical quantity, but we
will assume that it represents time in seconds. A discrete signal will be denoted by
x(n), in which the variable n is integer-valued and represents discrete instances in
time. Therefore it is also called a discrete-time signal, which is a number sequence
and will be denoted by one of the following notations:
𝑥 𝑛 = 𝑥 𝑛 = {… , 𝑥 −1 , 𝑥 0 , 𝑥 1 , … }
𝑥 𝑛 = {2,1, −1,0,1,4,3,7}
Generally, we will use the x-vector representation alone when the sample position
information is not required or when such information is trivial (e.g. when the
sequence begins at n=0). An arbitrary infinite-duration sequence cannot be
represented in MATLAB due to the finite memory limitations.
▪ TYPES OF SEQUENCES
We use several elementary sequences in digital signal processing for analysis
purposes. Their definitions and MATLAB representations are given below.
1 𝑛=0
𝛿 𝑛 =
0 𝑛≠0
However, the logical relation n == 0 is an elegant way of implementing δ(n). For
example, to implement:
1 𝑛 = 𝑛𝑜
𝛿 𝑛 − 𝑛𝑜 =
0 𝑛 ≠ 𝑛𝑜
Over the interval n1 ≤ n ≤ n2, we will use the following MATLAB function:
========================================================
% Function to generate x(n)=delta(n-no), n1<=n<=n2
function [x,n]=impseq(no,n1,n2)
n=[n1:n2];
x=[(n-no) == 0];
end
========================================================
In other way:
% n1=2,n2=6 ,no=5
n=[2:6]
x=[(n-5) == 0]
stem(n,x)
or:
[x,n]=impseq(5,2,6)
Results:
n =2 3 4 5 6
x=0 0 0 1 0
0.8
0.6
0.4
0.2
0
2 3 4 5 6
In MATLAB the function zeros(1,N) generates a row vector of N zeros, which can be
used to implement u(n) over a finite interval. However, the logical relation n == 0 is
an elegant way of implementing u(n). For example, to implement
1 𝑛 ≥ 𝑛𝑜
𝑢 𝑛 − 𝑛𝑜 =
0 𝑛 < 𝑛𝑜
Over the interval n1 ≤ n ≤ n2, we will use the following MATLAB function:
========================================================
% Function to generate x(n)=step(n-no), n1<=n<=n2
function [x,n]=stepseq(no,n1,n2)
n=[n1:n2];
x=[(n-no) >= 0];
end
========================================================
In other way:
% n1=2,n2=6 ,no=5
n=[2:6]
x=[(n-3) >= 0]
stem(n,x)
or:
[x,n]=stepseq(3,2,6)
Results:
n=2 3 4 5 6
x=0 1 1 1 1 1
0.8
0.6
0.4
0.2
0
2 2.5 3 3.5 4 4.5 5 5.5 6
𝑥 𝑛 = 𝑎𝑛 , ∀𝑛; 𝑎𝜖ℝ
>> n=[0:10];
>> x=(0.9).^n;
𝜎+𝑗 𝜔 𝑜 𝑛
𝑥 𝑛 =𝑒 , ∀𝑛
>> n=[0:10];
>> x=exp(2+3i).^n;
5. Sinusoidal sequence:
𝑥 𝑛 = 𝑐𝑜𝑠 𝜔𝑜 𝑛 + 𝜃 , ∀𝑛
Where θ is the phase in radians. A MATLAB function cos(or sin) used to generate
sinusoidal sequences. For example, to generate x(n) =3cos(0.lπn+π/3)+2sin(0.5πn),
0≤n≤10, we will need the following MATLAB script:
>> n=[0:10];
>> x=3*cos(0.1*pi*n+pi/3)+2*sin(0.5*pi*n);
6. Random sequences:
7. Periodic sequences:
▪ OPERATIONS ON SEQUENCES
1. Signal addition:
It's implemented by MATLAB using the operator "+". Notice that the two sequences
x1(n) and x2(n) must have the same length and the same indexing. We can use the
following MATLAB function to demonstrate the discrete sequences additions:
========================================================
% Function adds two discrete time sequences y(n)=x1(n)+x2(n)
function [y,n]=sigadd(x1,n1,x2,n2)
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;
end
========================================================
To understand the algorithm of the sigadd function, we will firstly describe find
function.
for example:
>> X = [1 0 4 -3 0 0 0 8 6];
>> indices = find(X) %% means find x that is not equal to zero
indices =
1 3 4 8 9
ind = 3 8 9
find(x>2) returns linear indices corresponding to the entries of X that are greater than 2.
The lines below can be used to add two sequences x1(n) and x2(n),
x1=[0,2,4,6,8]; % line#1
n1=[0,1,2,3,4]; % line#2
x2=[1,3,5,7,9]; % line#3
n2=[-2,-1,0,1,2]; % line#4
n=min(min(n1),min(n2)):max(max(n1),max(n2)); % line#5
y1=zeros(1,length(n)); % line#6
y2=y1; % line#7
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % line#8
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % line#9
y=y1+y2; % line#10
n= -2 -1 0 1 2 3 4
>> A=n>=min(n1) % n>=0 {0,0,1,1,1,1,1}
>> B=n<=max(n1) % n<=4 {1,1,1,1,1,1,1}
>> A&B
>> find(A&B==1) % index
A = 0 0 1 1 1 1 1
B = 1 1 1 1 1 1 1
ans = 0 0 1 1 1 1 1
ans =3 4 5 6 7
The values of all workspace variables can be seen in the following table,
line#1 n1 0 1 2 3 4
line#2 x1 0 2 4 6 8
line#3 n2 -2 -1 0 1 2
line#4 x2 1 3 5 7 9
line#5 N -2 -1 0 1 2 3 4
line#6 y1 0 0 0 0 0 0 0
line#7 y2 0 0 0 0 0 0 0
line#8 y1 0 0 0 2 4 6 8
line#9 y2 1 3 5 7 9 0 0
line#10 y1+y2 1 3 5 9 13 6 8
𝑥1 𝑛 . {𝑥2 𝑛 } = {𝑥1 𝑛 𝑥2 𝑛 }
It's implemented in MATLAB by array operator ".*". Once again the similar
restrictions apply for the ".*" operator as "+"
We can use the following MATLAB function to demonstrate the discrete sequences
multiplication:
========================================================
% Function multiplies two discrete time sequences y(n)=x1(n)*x2(n)
function [y,n]=sigmult(x1,n1,x2,n2)
n=(min(n1),min(n2)):(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;
end
========================================================
3. Signal scaling:
In this operation each sample is multiplied by a scalar α.
𝑎 𝑥 𝑛 = {𝑎𝑥 𝑛 }
4. Signal shifting:
In this operation each sample is shifted by amount of k to obtain the shifted sequence
y(n):
𝑦 𝑛 = 𝑥 𝑛−𝑘
1.5
0.5
0
-7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
Figure 4.3 sigshift(x,n,no)
5. Signal folding:
In this operation each sample of x(n) is flipped around n=0 to obtain a folded
sequence y(n):
𝑦 𝑛 = 𝑥 −𝑛
X = 1 2 3 becomes 3 2 1
For example:
x=[1,2,3];
n=[2,3,4];
y=fliplr(x);
n2=-fliplr(n);
% [y,n2]=sigfold(x,n)
stem(n,x),hold on,stem(n2,y)
0
-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6
This operation adds all sample values of x(n) between n1 and n2.
𝑛2
𝑥 𝑛 = 𝑥 𝑛1 + ⋯ + 𝑥 𝑛1
𝑛 =𝑛 1
>> sum(x(n1:n2));
7. Sample product:
This operation multiplies all sample values of x(n) between n1 and n2.
𝑛𝑜
𝑥 𝑛1 × … × 𝑛2
𝑛1
>> prod(x(n1:n2));
8. Signal energy:
𝐸𝑥 = |𝑥 𝑛 |2
𝑛=−∞
>> E=sum(abs(x).^2);
9. Sample power:
The average power of a periodic sequence with fundamental period N is given by:
𝑁−1
1
𝑃𝑥 = |𝑥 𝑛 |2
𝑁
𝑛 =0
Exercise 1
Generate and plot each of the following sequences over the indicated intervals:
a) x1 (n) = 2δ(n + 2) − δ(n − 4), −5 ≤ n ≤ 5
b) x2 (n) = n[u(n) − u(n − 10)] + 10exp(−0.3(n − 10)) ∗ [u(n − 10) − u(n − 20)]
, 0 ≤ n ≤ 20
c) x3 (n)(n) = cos(0.4πn) + 0.2w(n), 0 ≤ n ≤ 50
, where w(n) is a Guassian random sequence with 0 and variance 1.
d) x1 (n)(n) = exp(−0.1 + j0.3)n, −10 ≤ n ≤ 10
▪ DISCRETE SYSTEMS
Linear systems:
A discrete time system is called linear system if it satisfies the superposition principle.
𝐿 𝑎1 𝑥1 𝑛 + 𝑎2 𝑥2 𝑛 = 𝑎1 𝐿 𝑥1 𝑛 + 𝑎2 𝐿 𝑥 2 𝑛 , ∀𝑎1 , 𝑎2 , 𝑥1 𝑛 , 𝑥2 𝑛
A linear system in which an input-output pair, x(n) and y(n), is invariant to a shift n in
time is called a linear-time invariant system. For LTI system the L[.] and the shifting
operators are available as shown below.
This is a very important concept in linear system theory. The primary reason for
considering stability is to void building harmful systems and to avoid burnout or
saturation in systems operation. A system is said to be bounded-input bounded
output stable if every input produced bounded output.
An LTI system is BIBO if and only if its impulse response is absolutely summable.
∞
For example:
n
for the system ∶ y n = −0.9 u n
n=0:200;
y=(-0.9).^n;
sum(y)
ans = 0.5263 , then the system is stable.
2. Casuality
This important concept is necessary to make sure that systems can be built. A system
is said to be casual if the output at index no depends only on the input up to and
including the index no; that is the output doesn't depend on the future values
An LTI system is casual if and only if its impulse response:
▪ CONVOLUTION
Example:
Given the following two sequences
𝑥 𝑛 = 3, 11, 7, 0, −1, 4, 2 , − 3 ≤ 𝑛 ≤ 3
ℎ 𝑛 = 2, 3, 0, −5, 2, 1 , −1≤𝑛≤4
========================================================
% Modified convolution routine
function [y,ny]=conv_m(x,nx,h,nh)
nyb=nx(1)+nh(1);
nye=nx(length(x))+nh(length(h));
ny=[nyb:nye];
y=conv(x,h)
end
========================================================
Example:
1, −9 ≤𝑛 ≤0
Consider a LTI system with input 𝑥 𝑛 =
0, 𝑒𝑙𝑠𝑒
and the impulse response ℎ 𝑛 = 2−𝑛 𝑢 𝑛 . Using conv_m find the output y(n).
y(n)=x(n)*h(n)
nx=-9:0;
x=[1,1,1,1,1,1,1,1,1,1];
nh=0:20;
h=(2).^-nh;
[y,ny]=conv_m(x,nx,h,nh)
stem(ny,y)
1.8
1.6
1.4
1.2
0.8
0.6
0.4
0.2
0
-10 -5 0 5 10 15 20
▪ CORRELATION
The cross-correlation between two sequences can be evaluated by the formula:
rxy () x ( n ) y ( n )
n
, 0, 1, 2,....
Example:
Let 𝑥 𝑛 = 1, 2, 4 , and ℎ(𝑛) = {1, 1, 1, 1}
>> x=[1,2,4];
>> h=[1,1,1,1,1];
>> y=xcorr(x,h)
y = 1 3 7 7 7 6 4 0 0
▪ DIFFERENCE EQUATIONS
𝑎𝑘 𝑦 𝑛 − 𝑘 = 𝑏𝑚 𝑥 𝑛 − 𝑚 , ∀𝑛
𝑘=0 𝑚 =0
𝑦 𝑛 = 𝑏𝑚 𝑥 𝑛 − 𝑚 − 𝑎𝑘 𝑦 𝑛 − 𝑘
𝑚 =0 𝑘 =1
>> y=filter(b,a,x)
Example:
Suppose we have two systems with the following difference equations
𝑦(𝑛) = 𝑥(𝑛) + 2𝑥(𝑛 − 1) + 𝑥(𝑛 − 2)
Assume that y(−1) = 0.
(a) Find the impulse responses of these systems.
(b) State whether each system is FIR or IIR, and why.
(c) State whether each system is stable, and show why.
b=[1 2 1];
a=[1];
[x,n]=impseq(0,-20,20);
h=filter(b,a,x);
stem(n,h)
2
1.5
0.5
0
-20 -10 0 10 20
From impulse response we can see that the system is FIR (and all FIR systems are
stable).
or if the sequence is summable sequence then the system is stable
∞
Exercise 4