8/12/2019 Numerical Examples Module 5
1/28
HelpNumerical Examples: Module 5
Linear time-invariant systems
Basic propertiesLinearity
We start by verifying the linearity of a convolution system. Let us consider the example presented in
the course, where the system impulse response is given by , being the step
function.
Let us take a system impulse response with , and as input signal one of the tones used in
the Fourier example "three tones". We will use the command
y = conv(x,h,'valid');
in Matlab or
y = conv2(x,h,'valid');
in Freemat, in order to perform the convolution between the input and the impulse response .
Notice that such a command uses only the available data without performing zero padding, as on the
contrary done by
y = conv(x,h);
When using only the available data in performing a convolution between a sequence of length
and a sequence of length , the convolution expression using only the available data can be
written as:
if
if
[
] =
[
]
[
]
= 0 . 9 9 9
M
L
M>
L
[
] =
[
]
[
] ,
=
L+ 1 , ,
M
L
= 1
L>
M
[
] =
[
]
[
] ,
=
M+ 1 , ,
L
M
= 1
https://class.coursera.org/dsp-002/help/pages?url=https%3A%2F%2Fclass.coursera.org%2Fdsp-002%2Fwiki%2Fview%3Fpage%3Dmod5_numexhttps://class.coursera.org/dsp-002/help/pages?url=https%3A%2F%2Fclass.coursera.org%2Fdsp-002%2Fwiki%2Fview%3Fpage%3Dmod5_numex8/12/2019 Numerical Examples Module 5
2/28
The resulting signal is of length
max(length(x)-max(0,length(h)-1),0).
So we have
alpha = 0.999;
h = alpha.^(0:99); % construct the impulse response of the system o
f length 100
x1 = sin(2*pi*40*(1:1000)/1000);
y1 = conv(x1,h,'valid');
You can visualize or play the input and the output to see the effect of the system. Notice that
the dimension of is of 901 samples.
We now consider another one of the three tones of the Fourier example "three tones", namely
x2 = sin(2*pi*80*(1:1000)/1000);
The output of the system when the input is $$x2$ is given by
y2 = conv(x2,h,'valid'); % in FreeMat use y2 = conv2(x2,h,'valid');
Once again, plotting or playing the output gives us an idea how the input has been modified.
Given that the system is linear, its output when the input is must be equal to
. Let's check this out by taking and
a = 2;
b = -10;
xx = a*x1+b*x2; % linear combination of the inputs
yy = conv(xx,h,'valid'); % in FreeMat use yy = conv2(xx,h,'valid');
Now we compute the error between the output of the system when the input is the linear combination
and the linear combination of the outputs
error = abs(yy-(a*y1+b*y2)); % compute the error vector
maxError = max(error); % get the maximum value of the error vector
1
1
1
2
a
1 +
b
2
a
1 +
b
2
a= 2
b= 1 0
= a 1 + b 2 a 1 + b 2
8/12/2019 Numerical Examples Module 5
3/28
8/12/2019 Numerical Examples Module 5
4/28
the system of length 100
Generate now a Gaussian white noise (sequence of independent random numbers that are
generated according to the same Gaussian distribution)
sigma2 = 0.1; % power of the noise
noise = sigma2*randn(1,2000); % Gaussian noise
that we add to the sequence of two pulses and that we have used in the examples "three
tones" of the Fourier module, that is
x1 = sin(2*pi*40*(1:1000)/1000);
x2 = sin(2*pi*80*(1:1000)/1000);
x = [x1 x2];
xNoisy = noise+x; % Noisy version of x
You can play or plot and to hear or see the effect of the noise
Notice that we have plot here the values of the sample 750 to the sample 1250, where the transition
between the two tones occurs.
We now apply the leaky integrator to the noisy signal
1
2
N
8/12/2019 Numerical Examples Module 5
5/28
y = conv(xNoisy,h,'valid'); % in FreeMat use y = conv2(xNoisy,h,'va
lid');
Once again you can play or plot the output of the signal to hear and see the effect of the filter.
When plotting it, be careful that, due to the convolution of finite length sequences, the output
sequence is of length 1901 and the transition between the two tones happens at the sample
number 901 (and not number 1000 as of the input signal )
As you can see, the output is "smoothed", that is, denoised. You can also remark the different effect
on the two tones: The tone has been attenuated with
respect to the tone . The different attenuation of the
two tones will appear clear as soon we will discuss the convolution theorem.
Jingle!
So let's now apply our denoising method to a human voice signal. Load the filejingle.mat into Matlab
or FreeMat using the command
load('jingle.mat')
By typing
whos
N
2 = ( 2 8 0 ( 1 : 1 0 0 0 ) / 1 0 0 0 )
1 = ( 2 4 0 ( 1 : 1 0 0 0 ) / 1 0 0 0 )
https://spark-public.s3.amazonaws.com/dsp/num_examples/Module%205/code/jingle.mat8/12/2019 Numerical Examples Module 5
6/28
you can see that the vector (signal samples) jingle is of size 1x255000. Fs is the variable
containing the frequency at which the song jingle has been sampled. By playing it you will
probably recognize part of a famous jingle, but with a swiss yodeling touch!
In Matlab use
sound(jingle,Fs)
while in FreeMat use
wavplay(jingle,Fs)
We now create a white noise signal
sigma2 = 0.01;noise = sigma2*randn(1,255000);
to be added to the signal
jingleNoisy = jingle+noise;
Play it so hear the effect of the noise!
We now apply the Leaky Integrator filter on jingleNoisy
y = conv(jingleNoisy,h,'valid'); % in FreeMat use y = conv2(jingleN
oisy,h,'valid');
Play the denoised signal ... what do you hear? The noise is partially gone by the voice sounds bass
.. what happened?
The effect we have seen on the two tones (higher tone attenuated) and on the jingle signal (bass
voice) arise the natural question of what is the effect of the Leaky Integrator filter on the frequency
content of the signal. Question that finds a straightforward answer from the convolution theorem, as
discussed further in these numerical examples.
The Sluzky-Yule effectLet us also filter a random process with a moving average filter seen in class. The random process
is obtained by sampling independently at random a normal random variable with zero mean and
variance 1.
[
]
8/12/2019 Numerical Examples Module 5
7/28
The impulse response of the filter is given by
The following code computes the moving average for various values of by convolving the input
signal with the filter impulse response using the function conv(...).
clear all
close all
clc
% Parameters
M = 10:10:50;
x = randn(100, 1);
figure
plot(x);
fori=1:length(M)
current_M = M(i);
h = 1/current_M*ones(current_M, 1);
y = conv2(x(:), h(:), 'valid');
[
] =
,
1
0
i f 0
M
1
o t h e r w i s e
M
8/12/2019 Numerical Examples Module 5
8/28
figure
plot(y)
end
First, note that the output is of size . Indeed, remember that when
convolving a signal with its impulse response, we flip the impulse response in time and shift itprogressively on the right until there is no more overlap between the two signals. There is first
points that are formed by a partial overlap of and , than
points that are formed by a complete overlap of and and, again,
points that are formed by a partial overlap of and . Let us now display the result
for various values of , the length of the filter.
(
) + (
) 1
(
) 1
(
) (
) + 1
(
) 1
M
M= 1 0
M= 2 0
8/12/2019 Numerical Examples Module 5
9/28
We observe that, as increases, a cycle appears in the process. Whereas the original signal is
random and very difficult to predict, the filtered signal appears very predictable as it contains a cycle.
Imagine that the original process is the return of a stock, have we find a method to predict it? The
answer is (unfortunately) no. The cycle is just a spurious effect known as the Slutzky-Yule effect.
Although moving average is a commonly used tool to denoise a signal, we should remember that it
creates spurious effect for large or when applying the filter repeatedly.
The convolution theorem & frequency
response of filtersThe convolution theorem tells us that the Fourier transform of the convolution between two
sequences is equal to the product of the Fourier transform of each of the two sequences.
Working with finite sequences, we need to be careful on the choice of the length of the Fourier
M= 4 0
M= 5 0
M
M
8/12/2019 Numerical Examples Module 5
10/28
transform. We will use here the command for a K point fft
fft(X,K)
When convoluting a sequence of length with a sequence of length $$L
y=conv(x,h,'valid'); % in FreeMat use y=conv2(x,h,'valid');
the result is of length N-L+1.
Therefore, we will consider N-L+1 point fft.
Two tones again
Let's take our noisy two tones (length N=2000), the Leaky Integrator filter (length L=100),
and plot their spectral content, that is, the absolute value of their Fourier transform
% Construct the impulse response of the system of length 100
M=10;
lambda=(M-1)/M;
h=(1-lambda)*lambda.^(0:99);
% Generate two tones
x1=sin(2*pi*40*(1:1000)/1000);
x2=sin(2*pi*80*(1:1000)/1000);x=[x1 x2];
% Generate the noise
sigma2=0.1;
noise=sigma2*randn(1,2000);
% Add noise to signal
xNoisy=noise+x;
N=length(xNoisy);
L=length(h);
% DFT/DFS of noisy signal and impulse response
XNoisy=fft(xNoisy,N-L+1);
H=fft(h,N-L+1);
figure;
normFreq=(1:(N-L+1))/(N-L+1);
subplot(2,1,1); plot(normFreq,abs(XNoisy));
subplot(2,1,2); plot(normFreq,abs(H));
N
N
8/12/2019 Numerical Examples Module 5
11/28
The plot of the absolute value of the product (samplewise) of the two Fourier transforms
Y=H.*XNoisy;
plot(normFreq,abs(Y));
reads
8/12/2019 Numerical Examples Module 5
12/28
As expected from the shape of , the spectral content of the second tone, which is at higher
frequencies than the first tone, is attenuated. The Leaky Integrator filter has a low pass effect!
Of course, the inverse Fourier transform of give us the denoised two tones in the time domain
y=ifft(Y);
Low passing the Jingle!
Let's now see what happens in the frequency domain with the jingle signal
load('jingle.mat')
N=length(jingle);
% Generate the noise
sigma2=0.01;
noise=sigma2*randn(1,N);
% Add noise to signaljingleNoisy=jingle+noise;
% Construct the impulse response of the system of length 100
M=10;
lambda=(M-1)/M;
|H
|
Y
8/12/2019 Numerical Examples Module 5
13/28
h=(1-lambda)*lambda.^(0:99);
L=length(h);
% DFT/DFS of noisy signal and impulse response
JingleNoisy=fft(jingleNoisy,N-L+1);
H=fft(h,N-L+1);
figure;normFreq=(1:(N-L+1))/(N-L+1);
subplot(2,1,1); plot(normFreq,abs(JingleNoisy));
subplot(2,1,2); plot(normFreq,abs(H));
The plot of the absolute value of the product (samplewise) of the absolute value of two Fourier
transforms compared to the absolute value of the Fourier transform of the
original signal (around the origin for visualization ease)
Y=H.*JingleNoisy;
Jingle=fft(jingle,N-L+1);
figure;
normFreq=(1:50000)/(N-L+1);
subplot(2,1,1); plot(normFreq,abs(Jingle(1:50000)));
subplot(2,1,2); plot(normFreq,abs(Y(1:50000)));
reads
|H
| | J N |
8/12/2019 Numerical Examples Module 5
14/28
One can see that in the absolute value of the spectrum of the denoised signal the content of
higher freuquencies is severely reduced.
Measuring risk in financeNotice: To run this example, you need some data and code files that we provide here in compressed
format exampleFinanceRisk.zip.
The goal of this example is to apply the concept of filters seen in the lecture to compute commonly
used risk measures in finance. Consider the following problem faced by an investor. He is interested
in a certain stock whose evolution of the price over 5 years sampled at daily frequency is
obtained using the following code and depicted in the figure below.
clear all
close all
clc
% ParametersFILENAME = 'data.mat';
M = 100;
LAMBDA = 0.94;
% Load data
% - dates_ts: vector of dates
% - price_ts: vector of price
load(FILENAME);
% Plot price
figure
semilogy(price_ts)
ylabel('Log-price')
datetick(gca, [1 length(price_ts)], dates_ts)
grid on
|Y
|
[
]
https://spark-public.s3.amazonaws.com/dsp/num_examples/Module%205/code/exampleFinanceRisk.zip8/12/2019 Numerical Examples Module 5
15/28
We provide you with the datetick function to substitute the index of the sample by the corresponding
date. More than the price, an investor is interested in the return of his investment, defined as
obtained using the following code and depicted in the figure below.
% Compute simple return time-series
return_ts = (price_ts(2:end) - price_ts(1:(end - 1)))./price_ts(2:e
nd);
dates_ts = dates_ts(2:end);
length_ts = length(return_ts);
% Plot return
figure
plot(return_ts, 'x');
ylabel('Return')
datetick(gca, [1 length_ts], dates_ts);
grid on
[
]
[
] = ,
[
]
[
1 ]
[
]
8/12/2019 Numerical Examples Module 5
16/28
At the same time, an investor would like to assess the risk of his investment. Since the seminal work of
Markowitz, this is done (imperfectly) by measuring the volatility or, in signal processing terms, the
standard deviation of returns. The volatility is not directly observable and must be estimated. We now
see how the paradigm of filters can be applied to compute several estimates of volatility.
The moving average filter
Let us come back to the problem of measuring the volatility. A first way of measuring it is to compute
its value over the last observations of returns. Let us first define the average return over the
last observations
In other terms, this is the convolution of the return signal with the moving average filter. The volatility
is then given by
Again, this formula can be interpreted in terms of filtering. It is the convolution between the moving
average filter with the process that is obtained by squaring the difference between contemporaneous
return and the point moving average return.
% Compute the M-point volatility
volatility = zeros(length_ts, 1);
M[
]
M
[
] =
(
)
1
M
M 1
= 0
[
] =
2
1
M
M 1
= 0
(
[
] [
] )
2
[
] = [
]
2
M
http://en.wikipedia.org/wiki/Harry_Markowitz8/12/2019 Numerical Examples Module 5
17/28
h = 1/M*ones(M, 1);
fori = M:length_ts
temp = return_ts((i - M + 1):i);
moving_average = h'*temp;
volatility(i) = h'*(temp - moving_average).^2;
end
volatility = sqrt(volatility);
% Plotfigure
plot(volatility);
ylabel('M points volatility')
datetick(gca, [M length_ts], dates_ts)
grid on
We obtain the following figure.
The leaky integrator
In the previous formula, when computing the average return or volatility, the same weight is given toeach observation ( ). Since more recent observations are more relevant to predict the future, we
can consider instead the exponentially weighted volatility, given by the formula
1 /M
[
] =
[
1 ] + ( 1
)
[
]
8/12/2019 Numerical Examples Module 5
18/28
We recognize the recursive formula of the leaky integrator. Thus, this is again a simple filtering by the
impulse response of the leaky integrator. The coefficients of the filter decreases exponentially, hence
the name exponentially weighted volatility.
% Compute the exponentially weighted volatility
exp_average = 0;
exp_volatility = zeros(length_ts + 1, 1);
fori = 1:length_ts
exp_average = LAMBDA*exp_average + (1 - LAMBDA)*return_ts(i);
exp_volatility(i+1) = LAMBDA*exp_volatility(i) + (1 - LAMBDA)*(retu
rn_ts(i) - exp_average)^2;
end
exp_volatility = sqrt(exp_volatility(2:end));
figure
plot(exp_volatility)
ylabel('Exponentially weighted volatility')
datetick(gca, [1 length_ts], dates_ts)
grid on
We obtain the following image
[
] =
[
1 ] + ( 1
) (
[
] [
]
2
2
)
2
[
] = [
]
2
8/12/2019 Numerical Examples Module 5
19/28
Dual-tone multi-frequency (DTMF)
signalingNotice: To run this example, you need some data and code files that we provide here in compressed
format exampleDTMF.zip.
Let us come back to the example of a two-tones signal. Such signal seems very simplistic, but it is the
signal underlying a former signaling system known as DTMF. The role of a signaling system in a
telephone network is, among others, to setup a call, in particular by determining the number dialed by
a user. Before the emergence of touch screen mobile phone, push-button fixed phonewere
ubiquitous, as some of you may recall. When the user presses a button, the phone generates a two-
tones signals. The frequencies composing the signal corresponds to the frequency of the column and
the frequency of the row where the button lies in, as illustrated by the following table.
1209 Hz1336 Hz1477 Hz
697 Hz1 2 3
770 Hz4 5 6
852 Hz7 8 9
941 Hz* 0 #
For example, upon pressing a 1, the signal generated contains frequencies 1209 Hz and 697 Hz. We
obtain the following continuous time signal
Furthermore, the various frequencies were chosen such that none can be obtained as a result of
(
) = ( 2 4 1 8 ) + ( 1 3 9 4 ) .
http://en.wikipedia.org/wiki/Push-button_telephonehttps://spark-public.s3.amazonaws.com/dsp/num_examples/Module%205/code/exampleDTMF.zip8/12/2019 Numerical Examples Module 5
20/28
simple operations of two others. This is meant to avoid interference.
The generated signal is transmitted over an analog transmission line to a switching center where the
button that was pressed should be determined. This is done by analyzing the frequencies present in
the signal. We have seen that this could be done by using Fourier analysis. This is however not the
procedure that is used in DTMF, as it is computationally expensive. Instead, the following digital
processing system is used, whose block diagram is represented below.
The signal is first sampled at a frequency so as to obtain the digital signal . The later is
filtered by a series of 7 passband filtered placed in parallel. Each of them attenuates all frequenciesexcept the one corresponding to one of the frequencies of DTMF .
On the one hand, the frequency is a real-world, continuous frequency, expressed in Hz. On the
other hand, the filters are digital and their frequency is a digital, unitless frequency . The two are
related by the relation
.
This link between continuous frequency will be made clear in the next lectures on sampling. In the
mean time, let us accept it as it. Finally, a block called decoder outputs the digit depending on the
energy of the signals placed at its input. We focus now on the design of one of the filter.
IIR filter
(
)
[
]
0
0
0
= 2
0
0
0
8/12/2019 Numerical Examples Module 5
21/28
One possibility consists of designing an IIR filter. In order to let only a certain frequency pass, the
filter has a pole at the desired . The filter also has a pole at , so as to obtain a real impulse
response. The pole-zero diagram for
Since the poles are located on the unit circle, the filter is not stable. This can be a great problem in
practice, as a small error can get amplified and lead to an decoding error. As a solution we can place
instead the poles at and with . The poles-zeros diagram looks like
0
0
0
0
0
0
0
8/12/2019 Numerical Examples Module 5
22/28
The z-transform of the filter is then given by
This filter is stable, as all the poles are located inside the unit circle. It is called a resonator. In the
time-domain, given the input the output of the filter is then given by the following iterative
formula
The filter is implemented using the following code.
clear all
close all
clc
F0 = 697;
FS = 2000;
LAMBDA = 0.98;
% Generate discrete-time signal x
n = 1/FS:1/FS:0.1;
x = sin(2*pi*697*n) + sin(2*pi*1209*n);
figure
plot(x)
H(
) =
1
( 1
1 ) ( 1
1 )
0
0
H(
) =
1
1 2 c ( ) 1 + 2
0
2
[
]
[
]
[ ] = [ ] 2 c
( ) [ 1 ] + [ 2 ]
0
2
8/12/2019 Numerical Examples Module 5
23/28
grid on
% Plot pole diagram of the filter
omega0 = 2*pi*F0/FS;
figure
poles = [exp(j*omega0) exp(-j*omega0)];
plot(real(poles(1)), imag(poles(1)), 'xr');
hold on
plot(real(poles(2)), imag(poles(2)), 'xr');plot(exp(j*(0:0.01:1)*2*pi), 'b')
axis square
grid on
% Plot pole diagram of the filter
omega0 = 2*pi*F0/FS;
figure
poles = LAMBDA*[exp(j*omega0) exp(-j*omega0)];
plot(real(poles(1)), imag(poles(1)), 'xr');hold on
plot(real(poles(2)), imag(poles(2)), 'xr');
plot(exp(j*(0:0.01:1)*2*pi), 'b')
axis square
grid on
% Filter signal
omega0 = 2*pi*F0/FS;
b = 1;a = [1; -2*LAMBDA*cos(omega0); LAMBDA^2];
y1 = my_filter(x, b, a);
% Plot the filtered signal
figure
plot(y1)
grid on
% Plot the magnitude of the DFT/DFS of the filtered signal
figure
Y1 = fft(y1);
plot((1:length(Y1))/length(Y1),abs(Y1))
We have used the function my_filter(...) , which implements a filter written in direct form
We provide you with an implementation of this function. For example, if we place as input of this filter
the signal obtained by pressing a '1', sampled at Hz
H(
) =
b( 1 ) +
b( 2 ) 1 + +
b( )
b
1
b
1 +a
( 2 ) 1 + + a
( )
a
1
a
= 2 0 0 0
8/12/2019 Numerical Examples Module 5
24/28
we obtain the following output
Let us look at the magnitude of the DFT/DFS of this signal
8/12/2019 Numerical Examples Module 5
25/28
This is exactly what we wanted, a filter that let a certain frequency pass while attenuating other
frequencies. Finally, let us count the number of operations to compute the output of the filter. Each
iteration requires 2 multiplications and 2 additions, thus a total of multiplications and
additions.
Instead of designing an IIR filter, we could instead choose to design an FIR filter. The Parks-McClellan
algorithm is a standard procedure that finds the optimal FIR filter given a set of specifications. Here
optimal means that the resulting filter minimizes the weighted maximum error. The Matlab users can
use the function firpm(...) which is part of the signal processing toolbox. Alternatively, we provide
you with an implementation of the algorithm compatible with Freemat.
As before, we would like to design a bandpass filter, but this time we specify certain tolerance and
in the stop band and pass band, respectively.
clear all
close all
clc
% Parameters
F0 = 697;
FS = 2000;
% Generate discrete-time signal x
n = 1/FS:1/FS:0.1;
x = sin(2*pi*697*n) + sin(2*pi*1209*n);
% figure
2N
2N
8/12/2019 Numerical Examples Module 5
26/28
% plot(x)
% grid on
% Parks-McClellan
numbands = 3;
bands = 0.5*(1/FS)*[0 F0-200 F0-50 F0+50 F0+200 FS];
df = bands(3) - bands(2);
des = [0 0 1 1 0 0];
dp = 0.1;ds = 0.05;
k = ds/dp;
weight = [1 k 1];
type = 'bandpass';
griddensity = 16;
numtaps = ceil(1-(10*log10(dp*ds)+13)/(2.324*2*pi*df)) + 1;
h = my_remez(numtaps, numbands, bands, des, weight, type, griddensi
ty);
% Filter
y1 = my_filter(x, h, 1);
figure
plot(y1);
grid on
figure
Y1 = fft(y1);plot((1:length(Y1))/length(Y1),abs(Y1))
grid on
Applying the filter to the same input signal , we obtain the following output
8/12/2019 Numerical Examples Module 5
27/28
where one tones has been attenuated, as illustrated by the norm of its Fourier transform
$$
8/12/2019 Numerical Examples Module 5
28/28
Created Wed 30 Jan 2013 1:16 PM PET
Last Modifi ed Tue 19 Nov 2013 5:03 AM PET
Top Related