1. Beirut Arab University
Computer Engineering Program
Spring 2015
Digital Signal Processing (COME 384)
Instructor: Prof. Dr. Hamed Nassar
Discrete Signals, Impulse Response
Linear Systems
Textbook:
◦ V. Ingle and J. Proakis, Digital Signal Processing Using
MATLAB 3rd Ed, Cengage Learning, 2012
1
Dr. Hamed Nassar, Beirut Arab Univ
3. Sampling an analog signal
MATLAB script that displays a sine wave and samples it
t=linspace(0, 4*pi, 200); %We’ll plot 2 periods of sine, range = 200
points
subplot(2,1,1); % Two rows, one column, first plot
Gs=plot(t,sin(t)), grid;%Here t is a must, or it'll take t=1, 2, ...
title('sin t plotted as a continuous function');
n = linspace(0,4*pi,41); % Sine will be plotted discretely ( range =41
points)
x = sin(n);
subplot(2,1,2); % Two rows, one column, first plot
Hs=stem(n,x), grid; %As above but plotted discretely
set(Hs,'markersize',2); % circle size
title('sin t plotted as a sequence)');
Prof. H. Nassar, BAU
4. Sampling an analog signal
Let f(t) be a continuous-time function, such as sin(t).
This function can be used to obtain a sequence x(n),
simply by sampling it.
Suppose we sample the cont-time function f every T, i.e.
with a sampling rate (frequency) = 1/T, then
x(n)=f(nT) n=0, 1, 2, …
And for the sin in particular, we have
x(n)=sin(nT) n=0, 1, 2, …
Above we converted the cont-time function to a sequence, by
sampling. Below, we will do the opposite. If we are given the above
sequence x(n), we can represent it as a time function fs(t), by
expressing it as a summation of very special functions, namely “delta”
functions, as follows (assuming T=1)
This the same as
Prof. H. Nassar, BAU
5. Echo Generation
The most basic of all audio effects is that of time delay,
echoes.
It is used as the building block of more complicated effects
such as reverb or flanging.
For example, the combination of the direct sound
represented by discrete signal y and a single echo
appearing D samples later (which is related to delay in
seconds) can be generated by the (difference) equation
x[n]= y[n]+ αy[n − D], |α| < 1
where x is the resulting signal and α the attenuation of the
direct sound.
Difference equations are implemented in MATLAB using
the filter function
filter(b,1,y)
where b is a vector having the parameters, among them D,
α, e.g. Prof. H. Nassar, BAU
6. An Audio application
DSP has applications in a wide variety of fields, and most
importantly in sound and images.
DSP can be used to add echo to a sound piece. It can also
be used to remove echo. Here is a demo.
%MA_SoundEcho.m: Add and remove echo from a sound piece.
%9 s of Handel's Halleluja sampled at 8192 sam/sec.
%We'll play it with echo delayed by D = 4196 samples, i.e 0.5 s
load handel; %Load sound piece, so we can play it
sound(y,Fs); % Play original: signal in y and sampling freq
in Fs
pause(5);
alpha = 0.9; D = 8000; %Attenuation=alpha, Delay=D
samples
b= [1,zeros(1,D),alpha]; % Filter parameters
x= filter(b,1,y); % Generate sound plus its echo
Prof. H. Nassar, BAU
8. Unit Impulse
Among all sequences, the impulse (which is unit unless
otherwise specified) has particular importance in the study
of systems, for it is used as input to a system to reveal its
nature.
Recall that a system’s response can be divided into 2
types:
◦ Forced (steady state) response, and this is what the system
exhibits in long run, way after the input is applied. It is due to
a sustained, persistent input signal, e.g. a unit step.
◦ Natural (transient) response, and this is what the system
exhibits in the short run, immediately after the input is
applied. It is due to an instantaneous input signal, exclusively
the impulse (whose ferq transform (z or L) is unity, meaning
that when multiplied by the TF of the sys results in the TF
being itself the outputm
This tells that the impulse response discloses the internal
characteristics of systems in general. But in digital
(discrete) signals, in particular, the signal is nothing but a
Prof. H. Nassar, BAU
12. Important Note
In MATLAB, a sequence is represented by TWO vectors
◦ An index vector, e.g. n = 3:7 (indexes are ALWAYS
sequential), , and
◦ A value vector, e.g. x = [7 3 4 5 9] (see plot below)
When you process the sequence you HAVE TO use both
Always enter the index vector first, as you may generate
from it the value vector (e.g. n then sin n)
For example, to plot: stem(n,x, 'LineWidth',4)
Prof. H. Nassar, BAU
13. MATLAB Processing of discrete signals:
addition
Sequence addition, x1(n) + x2(n), is implemented in
MATLAB by the arithmetic operator “+”.
However, the lengths of x1(n) and x2(n)must be the same.
If sequences are of unequal lengths, or if the sample
positions are different for equal length sequences, then we
cannot directly use the operator +.
We have to first augment x1(n) and x2(n)so that they have
the same position vector n (and hence the same length).
This requires careful attention to MATLAB’s indexing
operations.
In particular, relational operations like “==”, and the find
function are required to make x1(n) and x2(n)of equal
length.
Prof. H. Nassar, BAU
14. Sequence Shifting: the 3 point method
Often times we will deal with sequences that are shifted,
either left or right. Here is a good method to know where
a shifted sequence goes.
Note that the general rule of thumb, that h(n-k) goes left
and h(n+k) goes right (where k is positive) is reversed
when n is negative.
Prof. H. Nassar, BAU
15. MATLAB Processing of sequences:
representation
In MATLAB, a sequence is actually entered as two
sequences: one representing the sequence itself and one
representing the indexes
Let us denote the former by x and the latter by n
◦ x and n must be of the same size
◦ the MATLAB default for n is the sequence 1, 2, …, |n|
Example: x(n) = 3, 7, 9, 5, 4 n = -1, 0, 1, 2, 3
MATLAB:
>> n=-1:3
n = -1 0 1 2 3
It is better to enter the index vector first, then the value
vector
>> x=[3 7 9 5 4] Prof. H. Nassar, BAU
16. MATLAB Processing of discrete signals:
Addition – special case
To add 2 sequences they must be of the
1) same size, and
2) same underlying index sequence.
If one or both of these two conditions are missing, we can fix by
padding with 0’s as in following example
Example: Find x(n)+ x(n-2)
where x = 3, 7, 9, 5, 4 n = -1, 0, 1, 2, 3
>> n = -1:3;
x =[ 3 7 9 5 4];
>> n1=[n 4 5]
n1 = -1 0 1 2 3 4 5
>> y1=[x 0 0]
y1 = 3 7 9 5 4 0 0
>> y2=[0 0 x]
y2 = 0 0 3 7 9 5 4
>> y=y1+y2
y = 3 7 12 12 13 5 4 Prof. H. Nassar, BAU
17. MATLAB Processing of discrete signals-
addition
Preliminaries for the general case: MIN, MAX
In the previous slide, we handled a special case, where the
two sequences were actually a sequence and a version of
it shifted R by 2. We want now to write a program for any
two sequences.
To that end we will need the following MATLAB functions.
min (max): Works in 2 modes:
◦ Takes a sequence (one argument) & returns the smallest (largest)
element.
◦ Takes two values (two arguments) & return the smaller (larger) of
them
Example:
>> x=[3 7 9 5 4]
x = 3 7 9 5 4
>> max(x)
ans = 9 Prof. H. Nassar, BAU
18. MATLAB Processing of discrete signals-
addition
Preliminaries for the general case: FIND
find: Takes a sequence and a condition, and returns the
indices of the elements that meet the condition.
Example 1:
>> x=[3 7 9 5 4]
x = 3 7 9 5 4
>>a= find(x>4)
2 3 4
Example 1:
>> x=[3 7 9 5 4]; >> y=[0, 2, 1, 0, 3];
>> find((x & y)==1)
ans = 2 3 5
where & gives the element-wise intersection [needed in
book’s sol., not mine]of two sequences of equal size, with
the intersection of the two elements being 1 if both are
Prof. H. Nassar, BAU
19. Preliminaries for the general case:
ACCESSING SPECIFIC ELEMENTS IN A
VECTOR
In the C language, suppose we declared an array
int x[6] = {5,4 , 9, 2, 6, 7}
Then we would access the 3rd element, say, using x[2]=9
In MATLAB it is different. Suppose we defined an array
x = [5 4 9 2 6 7]
Then we would access the 3rd element, say, using x(3)=9
Note the notational and indexing differences.
In MATLAB, unlike C, we can access multiple elements:
>> x = [5 4 9 2 6 7]; u=[2 1 5];
>> w=x(u)
w = 4 5 6
>> x(2:4)=[1 1 1]
x = 5 1 1 1 6 7 Prof. H. Nassar, BAU
20. MATLAB Processing of discrete signals:
Addition general case
Book Method:
Example:
x1=1:5; n1=2:6
x2=2:2:8; n2=-1:2;
n= min(min(n1),min(n2)):max(max(n1),max(n2)); % duration
of y(n)
y1 = zeros(1,length(n)); y2 = y1; % prepare for 0
padded x1,x2
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % x1 with duration
of y
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % x2 with duration
of y
y= y1+y2 % sequence addition
y = 2 4 6 9 2 3 4 5
Prof. H. Nassar, BAU
21. MATLAB Processing of discrete signals:
Addition general case
Simpler approach:
Example:
x1=1:5; n1=2:6;
x2=2:2:8; n2=-1:2;
n= min(min(n1),min(n2)):max(max(n1),max(n2)); % duration
of y(n)
y1 = zeros(1,length(n)); y2 = y1; % prepare for 0
padded x1,x2
//Below we access the y arrays as in y(3:5)=[4 8 3]
y1(find(n==min(n1)):find(n==max(n1)))=x1;
y2(find(n==min(n2)):find(n==max(n2)))=x2;
y= y1+y2 % sequence addition
y = 2 4 6 9 2 3 4 5
In using “FIND”, although we target the “y’s”, we work on
Prof. H. Nassar, BAU
22. Sequence Examples and Notes
Enter delta as: ndelta=0 & delta=1, or ndelta=[0] & delta=[1]
For shifting, and to avoid mistakes, always do it formally outside
MATLAB first, as in the following examples
Ex: 1st term part (a) below. Assuming the RED above entered,
we do:
◦ 1- Since y1[n]=delta[n+2], assume m=n+2. Then n=m-2.
Thus, y1[m-2]=delta[m]. That is, to get delta[n+2], just enter:
n-2.
◦ 2- Carry out the factor of the sequence, here 2, when doing the addition.
That is, do NOT make while composing y1.
Prof. H. Nassar, BAU
25. Example
We need first to list (write down) the elements of the two
sequences.
Note: In MATLAB, while building sequences, we
sometimes (not always) have a choice whether to use the
[ ] notation or not. (For example, A=1:5 & A=[1:5] are
equivalent. But in some other times, we are forced to use
[ ]. For example, to generate the x above, we HAVE TO
write: x=[1:7 6:-1:1]
Prof. H. Nassar, BAU
31. Solution
n =-10:10; a = -0.1+0.3j; x =exp(a*n);
subplot(2,2,1); stem(n,real(x));title('Real part');xlabel('n')
subplot(2,2,2); stem(n,imag(x));title('Imaginary part');xlabel('n')
subplot(2,2,3); stem(n,abs(x));title('Amplitude part');xlabel('n')
subplot(2,2,4); stem(n,(180/pi)*angle(x));title('Phase part');xlabel('n')
Prof. H. Nassar, BAU
32. Same Solution but manual caculation
n =-10:1:10; RE=exp(-0.1*n) .* cos(0.3*n)% .*, or it'll think
2 vecs
IM=exp(-0.1*n) .* sin(0.3*n); AMP=exp(-0.1*n); ANG=0.3*n
subplot(2,2,1); stem(n,RE);title('1-Real');xlabel('n')
subplot(2,2,2); stem(n,IM);title('2-Imaginary');xlabel('n')
subplot(2,2,3); stem(n,AMP);title('3-Amplitude');xlabel('n')
subplot(2,2,4); stem(n,ANG);title('4-Angle');xlabel('n')
Prof. H. Nassar, BAU
The difference
between this and the
previous is that we
do here the parts
manually.
This requires that we
should know how to
covert between polar
and Cartesian.
33. SOME USEFUL RESULTS
Note that this (i.e. the sum and the delta function) is
an ingenious technique to represent a numerical
sequence (collection of stand alone numbers) as a
mathematical construct that can be manipulated
rigorously. And with the sum notation it can be put in
a compact form. Prof. H. Nassar, BAU
36. Ex 2: T[x(n)]=2x(n-2)+5
Note that we prove linearity in two steps:
◦ 1- We apply the system’s rule to the LC (linear combination)
a1x1+a2x2, treating it as if it were one signal (not LC)
◦ 2 – We calculate the linear combination of the outputs of the two
signals, after applying the LS’s rule to each of its them.
Step 1: L[a1x1(n)+a2x2(n)] = 2{a1x1(n-2)+a2x2(n-2)} + 5
= 2 a1x1(n-2)+2 a2x2(n-2) + 5 (*)
Step 2: L[a1x1(n)+a2x2(n)] = a1L[x1(n)]+a2L[x2(n)]
= 2 1ax1(n-2)+5a1 + 2 a2x2(n-2) + 5a1
(**)
Since (*) (**), the system is not linear.
Prof. H. Nassar, BAU
37. Ee
We note in passing that proving that a system is linear
resembles the proof by induction technique in
mathematics.
Ex: Prove that sum of an arithmetic series s(n) = n(n+1)/2.
Step I: We want to sum up to n+1, instead of n, utilizing
s(n).
Then, n(n+1)/2+(n+1)=(n+1) (n+2)/2= (n+1)((n+1)+1)/2 (*)
Prof. H. Nassar, BAU
40. LTI: Linear Time-Invariant Systems
A time-invariant system means that on the time scales that
are used, the characteristics of these systems remain
unchanged.
Prof. H. Nassar, BAU
49. Causality
Note that when the input is an impulse, the response
(output) in the frequency domain is the product of 1 (L of
delta) by G (TF of system, which characterizes the
system) which produces G itself. The implication of this is
that you can read off the characteristics of the system from
its impulse response.
Prof. H. Nassar, BAU
50. Ex 1: Response of a Causal LTI System
Graphically Illustrated
.
Prof. H. Nassar, BAU
51. Causal LTI System Ex 1 (cntd): Discrete Convolution
(conv sum)
Prof. H. Nassar, BAU
52. Ex 2: Response of a NON-causal LTI system
A
Prof. H. Nassar, BAU
53. Ex 3: Response of a causal LTI system
A
Prof. H. Nassar, BAU
54. Ex 3: Response of a causal LTI system using
MATLAB
This is a MATLAB script to compute y(n) given x(n) and
h(n).
clear all; %Name the 2 input seqs: x, h & their indexes nx,
nh
nx=0:4; x=[3 7 5 8 2] %input signal
nh=[0:2]; h=[6 1 4] %impulse response
m=length(x); k=length(h);
n=m+k-1; %just befor end of overlap)
H=zeros(m, n);%Response matrix
for i=1:m%insert h into H, shifting Right 1 position every
row
H(i, i:i+k-1)=h; end
y=x*H %Obtain output signal
InPlot=subplot(1,2,1),stem(0:m-1,x,'LineWidth',3)
title('Input signal')
Prof. H. Nassar, BAU