Matlab Signal
Matlab Signal
Matlab Signal
Part 3
Discrete-Time Signals & Systems
Case Studies
S R Taghizadeh <srt@unl.ac.uk>
January 2000
Introduction
Matlab and its applications in analysis of continuous-time signals and systems has
been discussed in part 1 and 2 of this series of practical manuals. The purpose of part
3 is to discuss the way Matlab is used in analysis of discrete-time signals and systems.
Each section provides a series of worked examples followed by a number of
investigative problems. You are required to perform each of the worked examples in
order to get familiar to the concept of Matlab environment and its important functions.
In order to test your understanding of the concept of discrete-time signals & systems
analysis, you are required to complete as many of the investigation / case study
problems as possible. The areas covered are designed to enforce some of the topics
covered in the formal lecture classes. These are:
where f is the signal frequency, fs is the sampling frequency, q is the phase and A is
the amplitude of the signal. The program and its output is shown below:
% Program: W2E1b.m
% Generating 64 samples of x(t)=sin(2*pi*f*t) with a
% Frequency of 1KHz, and sampling frequency of 8KHz.
N=64; % Define Number of samples
n=0:N-1; % Define vector n=0,1,2,3,...62,63
f=1000; % Define the frequency
fs=8000; % Define the sampling frequency
x=sin(2*pi*(f/fs)*n); % Generate x(t)
plot(n,x); % Plot x(t) vs. t
grid;
title('Sinewave [f=1KHz, fs=8KHz]');
xlabel('Sample Number');
ylabel('Amplitude');
0.8
0.6
0.4
0.2
Amplitude
-0.2
-0.4
-0.6
-0.8
-1
0 10 20 30 40 50 60 70
Sample Number
% Program W2E2.m
% Generating the signal x(t)=exp(-0.1t)
t=0:0.1:40;
x=exp(-0.1*t);
plot(t,x);
grid;
title('Exponential Signal');
xlabel('Time [mS]');
ylabel('Amplitude');
0.9
0.8
0.7
0.6
Amplitude
0.5
0.4
0.3
0.2
0.1
0
0 5 10 15 20 25 30 35 40
Time [mS]
ì1 n=0
d ( n) = í
î0 elsewhere
% Program W2E3.m
% Generating 64 Samples of a unit impulse signal
N=64; % Define the number of samples
n=-(N/2):((N/2)-1); % Define a vector of sample numbers
x=zeros(1,N); % Define a vector of zeros
x((N/2)+1)=1.0; % Make the first sample to be 1 (i.e.at
t=0)
plot(n,x); % Plot the impulse
grid;
title('A Unit Impulse Signal');
xlabel('Sample Number');
ylabel('Amplitude');
0.9
0.8
0.7
0.6
d
litue
0.5
Amp
0.4
0.3
0.2
0.1
0
-40 -30 -20 -10 0 10 20 30 40
Sample Number
The following Matlab Program generates and plots a unit step signal:
% Program: W2E4.m
% Generates 40 samples of a unit step signal, u(n)
N=40; % Define the number of samples
n=-20:20; % Define a suitable discrete time axis
u=[zeros(1,(N/2)+1),ones(1,(N/2))]; % Generate the
signal
plot(n,u); % Plot the signal
axis([-20,+20,-0.5,1.5]); % Scale axis
grid;
title('A Unit Step Signal');
xlabel('Sample Number');
ylabel('Amplitude');
1
Amplitude
0.5
-0.5
-20 -15 -10 -5 0 5 10 15 20
Sample Number
(i) x(n)=u(n)-u(n-1)
(ii) g(n)=u(n-1)-u(n-5)
Output,
s(n)=x(n)+w(n)
Noise Output,
Input, x(n)
+
S Cancellation y(n)@x(n)
+ algorithm
Noise, w(n)
Matlab provides two commands, which may be applied to generate random numbers.
The following Matlab program generates random signals using each distribution.
% Program: W2E5.m
% Generates Uniformly and Normally Distributed random
signals
subplot(2,2,3);
plot(R2);
grid;
title('Uniformly Distributed Random Signal');
xlabel('Sample Number');
ylabel('Amplitude');
subplot(2,2,4);
hist(R2);
grid;
title('Histogram [Pdf] of a uniformly Random Signal');
xlabel('Sample Number');
ylabel('Total');
250
2
200
Amplitude
Total
0 150
100
-2
50
-4 0
0 200 400 600 800 1000 1200 -4 -2 0 2 4
Sample Number Sample Number
Uniformly Distributed Random Signal Histogram [Pdf] of a uniformly Random Signal
1 150
0.8
100
Amplitude
0.6
Total
0.4
50
0.2
0 0
0 200 400 600 800 1000 1200 0 0.2 0.4 0.6 0.8 1
Sample Number Sample Number
Output,
Input, x(n) Z -D y(n)=x(n-D)
Here 'D' is the number of samples which the input signal must be delayed. For
example if x(n)=[1, 2, 3, 4, 5], then x(n-3)=[0,0,0,1,2,3,4,5].
Output,
Input, x1(n)
+
S y(n)=x1(n)+x2(n)
+
Input, x2(n)
When adding two signals together, signals must have the same number of samples. If
one signal has less than number of samples than the other, then this signal may be
appended with zeros in order to make it equal length to the second signal before
adding them.
d
Input, x(t) Output,
dt
d
Differentiatin y ( n) = x(t )
dt
Output,
Input, x1(n)
+
X y(n)=x1(n)*x2(n)
+
Input, x2(n)
A Matlab program given below, provide an example of each of the above basic
operations:
% Program: W2E6.m
% Program demonstrating Basic Signal Manipulation
N=128;
f1=150;
f2=450;
f3=1500;
fs=8000;
n=0:N-1;
x1=sin(2*pi*(f1/fs)*n);
x2=(1/3)*sin(2*pi*(f2/fs)*n);
x3=sin(2*pi*(f3/fs)*n);
figure(1);
subplot(1,1,1);
subplot(2,3,1);
plot(n,x1);
grid;
title('Signal, x1(n)');
subplot(2,3,2);
plot(n,x2);
grid;
title('Signal, x2(n)');
subplot(2,3,3);
plot(n,x3);
grid;
title('Signal, x3(n)');
% Signal Delay
x1d=[zeros(1,20), x1(1:N-20)];
subplot(2,3,4);
plot(n,x1d);
0 0 0
-1 -0.4 -1
0 100 200 0 100 200 0 100 200
0 0 0
-1 -1 -1
0 100 200 0 100 200 0 100 200
N -1
X( k ) = å x( n )e - j 2pnk / N k=0,1,2,…,N-1
n =0
The magnitude of the X(k) [i.e. the absolute value] against k is called the
spectrum of x(n). The values of k is proportional to the frequency of the
signal according to:
kFs
fk =
N
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN .^ nk;
Xk=x * WNnk;
Use the above routine to determine and plot the DFT of the following signal:
MagX=abs(Xk);
PhaseX=angle(Xk)*180/pi;
figure(1);
Investigate:
(i) At what value of the index k does the magnitude of the DFT of x has major
peaks.
(ii) What is the corresponding frequency of the two peaks.
(iii) Perform the above for N=32, 64 and 512.
(iv) Comment on the results
(v) Set 128 and generate x(n) for n=0,1,2,…,N-1. Append a further 512 zeros to
x(n) as shown below: [Note the number of samples, N=128+512=640].
Xe=[x, zeros(1,512)];
Perform the DFT of the new sequence and plot its magnitude and phase.
Compare with the previous result. Explain the effect of zero padding a signal
with zero before taking the discrete Fourier Transform.
N -1
1
x ( n) =
N
å X ( k )e
k =0
j 2pnk / N
for 0 £ n £ N - 1
Use the IDFT to transfer the DFT results (i.e. Xk sequence) to its original
sequence.
f s = 128 Hz and N = 256 samples , obtain the FFT of the windowed signal using
rectangular and hamming windows, zero padded to N1=1024. Plot the normalised
FFT magnitude of the windowed signals. Which windowed signal shows a narrower
mainlobe?. Which windowed signal shows the smaller peak sidelobe?.
-100
-200
-300
-400
0 10 20 30 40 50 60 70
Frequency, Hz
Spectrum of x(t) using Hamming Windows
0
Normalised Magnitude, [dB]
-50
-100
-150
0 10 20 30 40 50 60 70
Frequency, Hz
It can be seen from the spectrum plots that both the rectangular window and
hamming window have their peak amplitude at f=30Hz corresponding to the signal
frequency. While the rectangular window has a narrower mainlobe, hamming
window provides less peak sidelobes than the rectangular window. There are many
other types of windows which are available as Matlab functions and are listed
below:
fft
psd
spectrum
In the following examples, we illustrate their use. One of the most important aspects
of spectral analysis is the interpretation of the spectrum and its relation to the signal
under investigation.
Task 1:
Generate 256 samples of a sinusoidal signal of frequency 1KHz with a
sampling frequency of 8KHz. Plot the signal and its spectrum.
-50
-100
Magnitude (dB)
-150
-200
-250
-300
-350
0 1000 2000 3000 4000
Frequency (Hz)
It is clear from the spectrum, that the signal consists of a single sinusoidal
components of frequency 1000Hz. Other artefacts in the figure are due to the limited
number of samples, windowing effects, and computation accuracy.
Task 1:
Consider, the notch filter in the previous section. The transfer function of the
notch filter is repeated here for convenience:
subplot(1,3,1);
y=filter(b,a,x);
0
20
20
-2
0 -4
0
-6
Magnitude, dB
Magnitude, dB
Magnitude, dB
-20
-8 -20
-40
-10
-40
-60 -12
-14
-60
-80
-16
It can be seen from the plot, that the input signal consists of two sinusoidal
components at 60Hz and 120Hz. From the magnitude response of the filter, it is clear
that the filter attenuates components whose frequencies are at 60 Hz. Therefore, the
output spectrum consists of the 120 Hz signal and attenuated (0 dB) signal at 60Hz.
A final example considers the use of the Matlab command 'spectrum' in order to
estimate the spectrum of a given signal.
Task 3:
Consider estimating the spectrum of a signal consisting of three sinusoidal signals at
frequencies, f1=500Hz, f2=1000Hz, and f3=1500Hz. Assume a sampling frequency
of 8KHz.
0
10
Magnitude, dB
-5
10
-10
10
-15
10
0 500 1000 1500 2000 2500 3000 3500 4000
Frequency, Hz
1 T
R xx ( t ) = lim
T ® ¥ 2T
ò-T x( t )x( t + t )dt (1)
For sampled signal (i.e. sampled signal), the autocorrelation is defined as either
biased or unbiased defined as follows:
:
N - m +1
1
R xx ( m ) =
N-m
å x( n )x( n + m - 1 ) [Biased Autocorrelation]
n =1
(2)
N - m +1
1
R xx ( m ) =
N
å x( n )x( n + m - 1 ) [Uniased Autocorrelation]
n =1
for m=1,2,…,M+1
The cross correlation function however measures the dependence of the values of one
signal on another signal. For two WSS (Wide Sense Stationary) processes x(t) and
y(t) it is described by:
1 T
R xy ( t ) = lim
T ®¥ T
ò-T x( t ) y( t + t )dt (3)
or
1 T
R yx ( t ) = lim
T ®¥ T
ò-T y( t )x( t + t )dt (4)
N - m +1
1
R yx ( m ) =
N
å y( n )x( n + m - 1 ) (5)
n =1
m=1,2,3,..,N+1
Where N is the record length (i.e. number of samples).
The properties of cross correlation function are listed in table 1.2.
R xx ( 0 ) ³ R xx ( k ) for k ¹ 0
[2] Periodicity:
If the autocorrelation function of a WSS random process is such that:
periodic.
[3] The autocorrelation function of a periodic signal is also periodic:
A2
Example: if x( n ) = A sin( w 0 n + j ) , then, R xx ( m ) = cos( w 0 m )
2
2p
Therefore if w 0 = , then R xx ( m ) is periodic with periodic N and x(n) is mean-
N
square periodic.
[4] Symmetry:
The autocorrelation function of WSS process is a conjugate symmetric function of m:
R xx ( m ) = R *xx ( -m )
For a real process, the autocorrelation sunction is symmetric: R xx ( m ) = R xx ( -m )
The autocorrelation function of a WSS process at lag, m=0, is equal to the mean-
square value of the process:
R xx ( m ) = R ss ( m ) + R ww ( m )
[7] The mean value:
The mean or average value (or d.c.) value of a WSS process is given by:
mean , x = R xx ( ¥ )
function.
[3] R xy ( -m ) = R yx ( m )
2
[4] R xy ( m ) £ R xx ( 0 )R yy ( 0 )
[5] R xy ( m ) £
1
2
[
R xx ( 0 ) + R yy ( 0 )]
[6] When R xy ( m ) = 0 , x(n) and y(n) are Sid to be ‘uncorrelated’ or they are said to
Notice that when using the function xcorr, to estimate the autocorrelation sequence , it
has double the number of samples as the signal x(n). An important point to remember
when using the function xcorr is that the origin is in the middle of the figure (here it is
at lag=1024).
0.5
Amplitude
-0.5
-1
0 1 2 3 4 5 6
Time, [s]
Autocorrelation function of the sinewave
1000
Autocorrelation
500
-500
0 500 1000 1500 2000 2500
lags
Note that if you want to write your own autocorrelation function, you may use
equation (2) to do this. Here is a how it may be written:
function [Rxx]=autom(x)
% [Rxx]=autom(x)
% This function Estimates the autocorrelation of the sequence
of
% random variables given in x as: Rxx(1), Rxx(2),…,Rxx(N),
where N is
% Number of samples in x.
N=length(x);
Rxx=zeros(1,N);
for m=1: N+1
for n=1: N-m+1
Rxx(m)=Rxx(m)+x(n)*x(n+m-1);
end;
end;
N=1024;
f1=1;
FS=200;
n=0:N-1;
x=sin(2*pi*f1*n/FS);
t=[1:N]*(1/FS);
subplot(2,1,1);
plot(t,x);
title('Sinwave of frequency 1000Hz [FS=8000Hz]');
xlabel('Time, [s]');
ylabel('Amplitude');
grid;
Rxx=autom(x);
subplot(2,1,2);
plot(Rxx);
grid;
title('Autocorrelation function of the sinewave');
xlabel('lags');
ylabel('Autocorrelation');
Note this version of estimating the autocorrelation function generates the same
number of samples as the signal itself and that the maximum is now placed at the
origin. (Rxx(1) is the origin).
0.5
Amplitude
-0.5
-1
0 1 2 3 4 5 6
Time, [s]
Autocorrelation function of the sinewave
600
400
Autocorrelation
200
-200
-400
-600
0 200 400 600 800 1000 1200
lags
Example 2: Crosscorrelation
Pure Sinewave
1
0.5
-0.5
-1
0 200 400 600 800 1000 1200
y(n), Pure Sinewave + Noise
40
20
-20
-40
0 200 400 600 800 1000 1200
Cross correlation Rxy
500
-500
0 500 1000 1500 2000 2500
From the results shown for example 2, what function cross correlation has performed.
Question [2]
A2
R xx ( t ) = cos( wt )
2
Explain this result.
The Target
r( t ) = ax( t - D ) + w( t )
Transmitting &
Receiving Dish
A pulse x(t) is transmitted, the reflected signal from an object is returned to the
receiver. The returned signal (r(t)) is delayed (i.e. D seconds) , noisy and attenuated.
The objective is to measure (estimate) the time delay between the transmitted and the
returned signal.
Analysis: Let the transmitted signal be x(t), then the returned signal r(t) may be
modelled as:
r( t ) = ax( t - D ) + w( t )
R rx ( t ) = E{r( t )x( t + t )}
= E{[ax(t - D) + w(t)][x( t + t )]}
= E{ax(t - D)x(t + t ) + w(t)x(t + t )}
(6)
Hence
R rx ( t ) = aR xx ( t - D ) + R wx ( t )
This is also stated in table 1.2 under the property number [6].
Hence the cross-correlation function between the transmitted signal and the received
signal may be written as:
R rx ( t ) = aR xx ( t - D ) (7)
Crosscorrelation Rrx
350
300
250
200
150
100
50
-50
-100
0 50 100 150 200 250 300
Estimated Time
Delay as number of
samples
Investigation
[1] Generate a single pulse for the transmitted signal as shown below:
x(n)
n
0 n=4 N=256
xd(n)
5a
n
0 n=34 N=256
[3] Generate N=256 samples of Gaussian random signal and call this w(n).
[4] Generate the simulated received signal by adding the transmitted signal x(n)
and the noise signal w(n), i.e.
r( n ) = ax( n - d ) + sigman ´ w( n )
[5] using subplot(2,2,1), plot the signals x(n), xd(n), and r(n). Appropriately label
and grid the each plot.
[6] Estimate the cross-correlation sequence Rrx ( m ) and plot in the fourth
quadrant of the figure. Note , plot only half the samples of the cross
correlation sequence returned by the function xcorr. This can be done as
follows:
% Assuming there are N samples of x
Y=xcorr(r,x);
R=Y(1:N);
Rrx=fliplr(R);
.
.
r( t ) = ax( t - D ) + w( t )
Pr ( w ) = aX ( w )e - jwD + W ( w )
Or taking the Fourier Transform of the cross correlation of r(t) and x(t) gives:
Assuming that the transmitted signal and the contaminated noise are uncorrelated, we
get:
Therefore by having an estimate of the cross spectral function of the transmitted and
the received signal, we can estimate the time delay from its phase:
Phase = wD
= 2pfD
Slope = 2pD
Or
Slope
D=
2p
Here is how you may estimate the cross spectrum between the transmitted signal and
the received signal and to obtain the phase plot.
Investigation
Generate N=256 samples of a sinusoidal signal of amplitude 1 volts, frequency,
f 1 =1Hz. Use a sampling frequency, Fs =200Hz, and call this signal the transmitted
signal, x(t).(i.e. x( t ) = sin( 2pf 1t ) )
From this plot estimate the delay time (Time where it peaks) and compare to the
theoretical value of 2.5s.
Estimate the attenuation factor using:
q Repeat the experiment for the noise variance of 0, 0.1,0.4, and 0.8.
q Estimate the error in each case.
q The cross spectral density and phase between x(t) and r(t).
From the phase plot, estimate the slope of the plot in the following table:
N Slope
128
256
512
1024
What is the relationship between the time delay and the slope of the phase?
[Note: That yet another method of estimating the time delay is via a system
identification approach. This method is described in the appendix for those
interested.]
“The cross correlation of the output ,y(n) and the input, x(n) [i.e. R yx ( m ) ] is equal
The objective of this part of the case study is to illustrate this property.
% M_ex4
% By: saeed Reza Taghizadeh [January 2000]
% Application of cross correlation in System Analysis
0.8
0.6
0.4
0.2
-0.2
-0.4
0 5 10 15 20 25 30 35
0.8
0.6
0.4
0.2
-0.2
-0.4
0 5 10 15 20 25 30 35
Note also that since the input is white noise and being random, the experiment will
not be valid for simply one run, hence as shown in the program, the simulation is
carried out for 128 times and the final result is the average of the all the runs. When
simulating with random processes, you must perform a simulation over a number of
runs and average the results for the final outcome.
Investigation
A digital filter is described by the following transfer function:
One of the most simplest and yet effective digital filters are ‘avergers’. This is a
system which computes the average of previous q-samples of its input. For example a
5-point averager is defined as:
1
y( n ) = [x( n ) + x( n - 1 ) + x( n - 2 ) + x( n - 3 ) + x( n - 4 )]
5
a=1;
b=[1/5, 1/5, 1/5, 1/5, 1/5];
[h,f]=freqz(b,a,256);
H=abs(h);
f=f/(2*pi);
plot(f,H);
q Use the cross correlation procedure illustrated above to estimate its impulse
response by applying white noise with zero mean and unit variance and plot its
response (call this the estimated response, he).
q Plot the error as E= The true response (i.e ht) – The estimated impulse response (i.e.
he).
Digital filters play an important role in every digital audio or video equipment. Some
simple digital filters are to be examined in this section both analytically and via
simulation.
Investigation
For each of the following systems,:
2 -6
+
input +
x(n) S Output
y(n)
[Case Number 1]
Z -k
+
x(n) +
S y(n)
K=4, 5, 6
[Case Number 2]
Z -k
-1
+
x(n) +
S y(n)
K=4, 5, 6
[Case Number 3]
Here, we assume that the transmitted signal r(t) is the output of a linear time-invariant
system whose input is the transmitted signal x(t). Another word, we model a system
whose transfer function characteristic processes x(t) in a way as to produce the
transmitted signal r(t) in its output. Once this is done, the information concerning the
estimation of delay (i.e. D) may be extracted from its transfer function. Here is the
mathematical analysis of the above procedure.
Pxr ( w ) = H ( w ).Pxx ( w )
Which states that “The cross spectrum of the input-output, Pxr ( w ) is equal to the
transfer function H ( w ) times the power spectrum of the input signal, Pxx ( w ) .”
We therefore have:
Pxr ( w )
H(w ) =
Pxx ( w )
Having obtained the transfer function of the model, the followings may be obtained:
The time delay (i.e.D) may be obtained from the slope of the phase angle f ( w ) as
before.