Вы находитесь на странице: 1из 28

Numerical Examples: Module 5

Help

Linear time-invariant systems


Basic properties
Linearity
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 h[n] = n u[n] , u[n] being the step function. Let us take a system impulse response with = 0.999, and as input signal one of the tones used in the Fourier example "three tones". We will use the command

y=c o n v ( x , h , ' v a l i d ' ) ;

in Matlab or

y=c o n v 2 ( x , h , ' v a l i d ' ) ;

in Freemat, in order to perform the convolution between the input x and the impulse response h. Notice that such a command uses only the available data without performing zero padding, as on the contrary done by

y=c o n v ( x , h ) ;

When using only the available data in performing a convolution between a sequence x of length and a sequence h of length L, the convolution expression using only the available data can be written as: if M
> L

y [n] =

L l=1

h[l]x[n l] ,

n = L + 1, , M

if L

> M

y [n] =

M m=1

x[m]h[n m] ,

n = M + 1, , L

The resulting signal is of length

m a x ( l e n g t h ( x ) m a x ( 0 , l e n g t h ( h ) 1 ) , 0 ) .

So we have

a l p h a=0 . 9 9 9 ; h=a l p h a . ^ ( 0 : 9 9 ) ;%c o n s t r u c tt h ei m p u l s er e s p o n s eo ft h es y s t e mo fl e n g t h1 0 0 x 1=s i n ( 2 * p i * 4 0 * ( 1 : 1 0 0 0 ) / 1 0 0 0 ) ; y 1=c o n v ( x 1 , h , ' v a l i d ' ) ;

You can visualize or play the input x1 and the output y1 to see the effect of the system. Notice that the dimension of y1 is of 901 samples. We now consider another one of the three tones of the Fourier example "three tones", namely

x 2=s i n ( 2 * p i * 8 0 * ( 1 : 1 0 0 0 ) / 1 0 0 0 ) ;

The output of the system when the input is $$x2$ is given by

y 2=c o n v ( x 2 , h , ' v a l i d ' ) ;%i nF r e e M a tu s ey 2=c o n v 2 ( x 2 , h , ' v a l i d ' ) ;

Once again, plotting or playing the output y2 gives us an idea how the input has been modified. Given that the system is linear, its output when the input is a
a y1 + b y2 . x1 + b x2 b = 10

must be equal to

Let's check this out by taking

a = 2

and

a=2 ; b=1 0 ; x x=a * x 1 + b * x 2 ;%l i n e a rc o m b i n a t i o no ft h ei n p u t s y y=c o n v ( x x , h , ' v a l i d ' ) ;%i nF r e e M a tu s ey y=c o n v 2 ( x x , h , ' v a l i d ' ) ;

Now we compute the error between the output of the system when the input is the linear combination xx = ax1 + bx2 and the linear combination of the outputs ay 1 + by 2

e r r o r=a b s ( y y ( a * y 1 + b * y 2 ) ) ;%c o m p u t et h ee r r o rv e c t o r m a x E r r o r=m a x ( e r r o r ) ;%g e tt h em a x i m u mv a l u eo ft h ee r r o rv e c t o r

If now we printout maxError, by simply typing

m a x E r r o r

we obtain 6.0840e-14, a negligible value which is purely due to numerical computation errors. Therefore
(ax1 + bx2) h = ay 1 + by 2

Time invariance
Let us now now check the time invariance of a convolution system. Consider the same system as above with input x1 and output y1 . We now introduce a time delay in the input signal, say of 100 samples and compute the corresponding output

x 1 D e l a y e d=x 1 ( 1 0 0 : 1 0 0 0 ) ; y 1 I n p u t D e l a y e d=c o n v ( x 1 D e l a y e d , h , ' v a l i d ' ) ;%i nF r e e M a tu s ey 1 I n p u t D e l a y e d=c o n v 2 ( x 1 D e l a y e d , h , ' v a l i d ' ) ;

Given the time invariance of the system, y1I nputDelayed must be equal to 100 samples, i.e., y1I nputDelayed
= y 1(100 : 901) .

y1

with a time delay of

Indeed

y 1 D e l a y e d=y 1 ( 1 0 0 : 9 0 1 ) ; e r r o r=a b s ( y 1 I n p u t D e l a y e d y 1 D e l a y e d ) ;%c o m p u t et h ee r r o rv e c t o r m a x E r r o r=m a x ( e r r o r ) ;%g e tt h em a x i m u mv a l u eo ft h ee r r o rv e c t o r

By simply typing

m a x E r r o r

we obtain the value of the error which is 0.

Denoising with the Leaky Integrator


Two simple tones
Consider a Leaky Integrator filter of order 10. As seen on the filtering lecture, the corresponding n impulse response is given by h[n] = (1 ) u[n] , where = (10 1)/10 = 0.9.

M=1 0 ; l a m b d a=( M 1 ) / M ; h=( 1 l a m b d a ) * l a m b d a . ^ ( 0 : 9 9 ) ;%c o n s t r u c tt h ei m p u l s er e s p o n s eo f

t h es y s t e mo fl e n g t h1 0 0

Generate now a Gaussian white noise (sequence of independent random numbers that are generated according to the same Gaussian distribution)

s i g m a 2=0 . 1 ;%p o w e ro ft h en o i s e n o i s e=s i g m a 2 * r a n d n ( 1 , 2 0 0 0 ) ;%G a u s s i a nn o i s e

that we add to the sequence of two pulses x1 and tones" of the Fourier module, that is

x2

that we have used in the examples "three

x 1=s i n ( 2 * p i * 4 0 * ( 1 : 1 0 0 0 ) / 1 0 0 0 ) ; x 2=s i n ( 2 * p i * 8 0 * ( 1 : 1 0 0 0 ) / 1 0 0 0 ) ; x=[ x 1x 2 ] ; x N o i s y=n o i s e + x ;%N o i s yv e r s i o no fx

You can play or plot x and

xN oisy

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

y=c o n v ( x N o i s y , h , ' v a l i d ' ) ;%i nF r e e M a tu s ey=c o n v 2 ( x N o i s y , h , ' v a l i d ' ) ;


Once again you can play or plot the output of the signal y 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
y

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 xN oisy )

As you can see, the output is "smoothed", that is, denoised. You can also remark the different effect on the two tones: The tone x2 = sin(2 pi 80 (1 : 1000)/1000) has been attenuated with respect to the tone x1 = sin(2 pi 40 (1 : 1000)/1000). 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 file jingle.mat into Matlab or FreeMat using the command

l o a d ( ' j i n g l e . m a t ' )

By typing

w h o s

you can see that the vector (signal samples) j i n g l e is of size 1x255000. F s is the variable containing the frequency at which the song j i n g l e has been sampled. By playing it you will probably recognize part of a famous jingle, but with a swiss yodeling touch! In Matlab use

s o u n d ( j i n g l e , F s )

while in FreeMat use

w a v p l a y ( j i n g l e , F s )

We now create a white noise signal

s i g m a 2=0 . 0 1 ; n o i s e=s i g m a 2 * r a n d n ( 1 , 2 5 5 0 0 0 ) ;

to be added to the signal

j i n g l e N o i s y=j i n g l e + n o i s e ;

Play it so hear the effect of the noise! We now apply the Leaky Integrator filter on jingleNoisy

y=c o n v ( j i n g l e N o i s y , h , ' v a l i d ' ) ;%i nF r e e M a tu s ey=c o n v 2 ( j i n g l e N o i s y , h , ' v a l i d ' ) ;

Play the denoised signal y ... 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 effect


Let us also filter a random process with a moving average filter seen in class. The random process x[n] is obtained by sampling independently at random a normal random variable with zero mean and variance 1.

The impulse response of the filter is given by


1 0 if 0 n M 1 otherwise

h[n] = {

The following code computes the moving average for various values of M by convolving the input signal with the filter impulse response using the function conv(...).

c l e a ra l l c l o s ea l l c l c %P a r a m e t e r s M=1 0 : 1 0 : 5 0 ; x=r a n d n ( 1 0 0 ,1 ) ; f i g u r e p l o t ( x ) ; f o ri = 1 : l e n g t h ( M ) c u r r e n t _ M=M ( i ) ; h=1 / c u r r e n t _ M * o n e s ( c u r r e n t _ M ,1 ) ; y=c o n v 2 ( x ( : ) ,h ( : ) ,' v a l i d ' ) ;

f i g u r e p l o t ( y ) e n d

First, note that the output y is of size length(x) + length(h) 1 . Indeed, remember that when convolving a signal with its impulse response, we flip the impulse response in time and shift it progressively on the right until there is no more overlap between the two signals. There is first length(h) 1 points that are formed by a partial overlap of h and x, than
length(x) length(h) + 1 length(h) 1

points that are formed by a complete overlap of h and x and, again, points that are formed by a partial overlap of h and x. Let us now display the result

for various values of M , the length of the filter.

M = 10

M = 20

M = 40

M = 50

We observe that, as M 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 M or when applying the filter repeatedly.

The convolution theorem & frequency response of filters


The 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

transform. We will use here the command for a K point fft

f f t ( X , K )

When convoluting a sequence

of length

with a sequence

of length $$L

y = c o n v ( x , h , ' v a l i d ' ) ;%i nF r e e M a tu s ey = c o n v 2 ( x , h , ' v a l i d ' ) ;

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 xN oisy (length N=2000), the Leaky Integrator filter and plot their spectral content, that is, the absolute value of their Fourier transform
h

(length L=100),

%C o n s t r u c tt h ei m p u l s er e s p o n s eo ft h es y s t e mo fl e n g t h1 0 0 M = 1 0 ; l a m b d a = ( M 1 ) / M ; h = ( 1 l a m b d a ) * l a m b d a . ^ ( 0 : 9 9 ) ; %G e n e r a t et w ot o n e s x 1 = s i n ( 2 * p i * 4 0 * ( 1 : 1 0 0 0 ) / 1 0 0 0 ) ; x 2 = s i n ( 2 * p i * 8 0 * ( 1 : 1 0 0 0 ) / 1 0 0 0 ) ; x = [ x 1x 2 ] ; %G e n e r a t et h en o i s e s i g m a 2 = 0 . 1 ; n o i s e = s i g m a 2 * r a n d n ( 1 , 2 0 0 0 ) ; %A d dn o i s et os i g n a l x N o i s y = n o i s e + x ; N = l e n g t h ( x N o i s y ) ; L = l e n g t h ( h ) ; %D F T / D F So fn o i s ys i g n a la n di m p u l s er e s p o n s e X N o i s y = f f t ( x N o i s y , N L + 1 ) ; H = f f t ( h , N L + 1 ) ; f i g u r e ; n o r m F r e q = ( 1 : ( N L + 1 ) ) / ( N L + 1 ) ; s u b p l o t ( 2 , 1 , 1 ) ; p l o t ( n o r m F r e q , a b s ( X N o i s y ) ) ; s u b p l o t ( 2 , 1 , 2 ) ; p l o t ( n o r m F r e q , a b s ( H ) ) ;

The plot of the absolute value of the product (samplewise) of the two Fourier transforms

Y = H . * X N o i s y ; p l o t ( n o r m F r e q , a b s ( Y ) ) ;

reads

As expected from the shape of |H | , 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 Y give us the denoised two tones in the time domain

y = i f f t ( Y ) ;

Low passing the Jingle!


Let's now see what happens in the frequency domain with the jingle signal

l o a d ( ' j i n g l e . m a t ' ) N = l e n g t h ( j i n g l e ) ; %G e n e r a t et h en o i s e s i g m a 2 = 0 . 0 1 ; n o i s e = s i g m a 2 * r a n d n ( 1 , N ) ; %A d dn o i s et os i g n a l j i n g l e N o i s y = j i n g l e + n o i s e ; %C o n s t r u c tt h ei m p u l s er e s p o n s eo ft h es y s t e mo fl e n g t h1 0 0 M = 1 0 ; l a m b d a = ( M 1 ) / M ;

h = ( 1 l a m b d a ) * l a m b d a . ^ ( 0 : 9 9 ) ; L = l e n g t h ( h ) ; %D F T / D F So fn o i s ys i g n a la n di m p u l s er e s p o n s e J i n g l e N o i s y = f f t ( j i n g l e N o i s y , N L + 1 ) ; H = f f t ( h , N L + 1 ) ; f i g u r e ; n o r m F r e q = ( 1 : ( N L + 1 ) ) / ( N L + 1 ) ; s u b p l o t ( 2 , 1 , 1 ) ; p l o t ( n o r m F r e q , a b s ( J i n g l e N o i s y ) ) ; s u b p l o t ( 2 , 1 , 2 ) ; p l o t ( n o r m F r e q , a b s ( H ) ) ;

The plot of the absolute value of the product (samplewise) of the absolute value of two Fourier transforms |H ||J ingleN oisy| compared to the absolute value of the Fourier transform of the original signal (around the origin for visualization ease)

Y = H . * J i n g l e N o i s y ; J i n g l e = f f t ( j i n g l e , N L + 1 ) ; f i g u r e ; n o r m F r e q = ( 1 : 5 0 0 0 0 ) / ( N L + 1 ) ; s u b p l o t ( 2 , 1 , 1 ) ; p l o t ( n o r m F r e q , a b s ( J i n g l e ( 1 : 5 0 0 0 0 ) ) ) ; s u b p l o t ( 2 , 1 , 2 ) ; p l o t ( n o r m F r e q , a b s ( Y ( 1 : 5 0 0 0 0 ) ) ) ;

reads

One can see that in the absolute value of the spectrum of the denoised signal |Y | the content of higher freuquencies is severely reduced.

Measuring risk in finance


Notice: 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
p[t]

over 5 years sampled at daily frequency is

obtained using the following code and depicted in the figure below.

c l e a ra l l c l o s ea l l c l c %P a r a m e t e r s F I L E N A M E=' d a t a . m a t ' ; M=1 0 0 ; L A M B D A=0 . 9 4 ; %L o a dd a t a %-d a t e s _ t s :v e c t o ro fd a t e s %-p r i c e _ t s :v e c t o ro fp r i c e l o a d ( F I L E N A M E ) ; %P l o tp r i c e f i g u r e s e m i l o g y ( p r i c e _ t s ) y l a b e l ( ' L o g p r i c e ' ) d a t e t i c k ( g c a ,[ 1l e n g t h ( p r i c e _ t s ) ] ,d a t e s _ t s ) g r i do n

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
p [ t ]p [ t 1]

r[t]

of his investment, defined as

r[t] =

p [t]

obtained using the following code and depicted in the figure below.

%C o m p u t es i m p l er e t u r nt i m e s e r i e s r e t u r n _ t s=( p r i c e _ t s ( 2 : e n d )-p r i c e _ t s ( 1 : ( e n d-1 ) ) ) . / p r i c e _ t s ( 2 : e n d ) ; d a t e s _ t s=d a t e s _ t s ( 2 : e n d ) ; l e n g t h _ t s=l e n g t h ( r e t u r n _ t s ) ; %P l o tr e t u r n f i g u r e p l o t ( r e t u r n _ t s ,' x ' ) ; y l a b e l ( ' R e t u r n ' ) d a t e t i c k ( g c a ,[ 1l e n g t h _ t s ] ,d a t e s _ t s ) ; g r i do n

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 M observations of returns. Let us first define the average return last M observations
[t] = r
1 M

[t] r

over the

M 1 k =0

r(t k)

In other terms, this is the convolution of the return signal with the moving average filter. The volatility is then given by

[t] =

1 M

M 1 k =0

[t]) (r[t k] r

2 [t] = [t]

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
M

point moving average return.

%C o m p u t et h eM p o i n tv o l a t i l i t y v o l a t i l i t y=z e r o s ( l e n g t h _ t s ,1 ) ;

h=1 / M * o n e s ( M ,1 ) ; f o ri=M : l e n g t h _ t s t e m p=r e t u r n _ t s ( ( i-M+1 ) : i ) ; m o v i n g _ a v e r a g e=h ' * t e m p ; v o l a t i l i t y ( i )=h ' * ( t e m p-m o v i n g _ a v e r a g e ) . ^ 2 ; e n d v o l a t i l i t y=s q r t ( v o l a t i l i t y ) ; %P l o t f i g u r e p l o t ( v o l a t i l i t y ) ; y l a b e l ( ' Mp o i n t sv o l a t i l i t y ' ) d a t e t i c k ( g c a ,[ Ml e n g t h _ t s ] ,d a t e s _ t s ) g r i do n

We obtain the following figure.

The leaky integrator


In the previous formula, when computing the average return or volatility, the same weight is given to each observation (1/M ). Since more recent observations are more relevant to predict the future, we can consider instead the exponentially weighted volatility, given by the formula
[t] = r [t 1] + (1 )r[t] r

[t] =

[t 1] + (1 )(r[t] r [t] )

2 [t] = [t]

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.

%C o m p u t et h ee x p o n e n t i a l l yw e i g h t e dv o l a t i l i t y e x p _ a v e r a g e=0 ; e x p _ v o l a t i l i t y=z e r o s ( l e n g t h _ t s+1 ,1 ) ; f o ri=1 : l e n g t h _ t s e x p _ a v e r a g e=L A M B D A * e x p _ a v e r a g e+( 1-L A M B D A ) * r e t u r n _ t s ( i ) ; e x p _ v o l a t i l i t y ( i + 1 )=L A M B D A * e x p _ v o l a t i l i t y ( i )+( 1-L A M B D A ) * ( r e t u r n _ t s ( i )-e x p _ a v e r a g e ) ^ 2 ; e n d e x p _ v o l a t i l i t y=s q r t ( e x p _ v o l a t i l i t y ( 2 : e n d ) ) ;

f i g u r e p l o t ( e x p _ v o l a t i l i t y ) y l a b e l ( ' E x p o n e n t i a l l yw e i g h t e dv o l a t i l i t y ' ) d a t e t i c k ( g c a ,[ 1l e n g t h _ t s ] ,d a t e s _ t s ) g r i do n

We obtain the following image

Dual-tone multi-frequency (DTMF) signaling


Notice: 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 phone were ubiquitous, as some of you may recall. When the user presses a button, the phone generates a twotones 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 770 Hz4 852 Hz7 941 Hz* 2 5 8 0 3 6 9 #

For example, upon pressing a 1, the signal generated contains frequencies 1209 Hz and 697 Hz. We obtain the following continuous time signal
x(t) = sin(2418t) + sin(1394t).

Furthermore, the various frequencies were chosen such that none can be obtained as a result of

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 x(t) is first sampled at a frequency f s so as to obtain the digital signal x[n]. The later is filtered by a series of 7 passband filtered placed in parallel. Each of them attenuates all frequencies except the one corresponding to one of the frequencies of DTMF f 0 . On the one hand, the frequency f 0 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 0 . The two are related by the relation
f0 fs

0 = 2

This link between continuous frequency f 0 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

One possibility consists of designing an IIR filter. In order to let only a certain frequency 0 pass, the filter has a pole at the desired
0 .

The filter also has a pole at 0 , so as to obtain a real impulse


f0

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 ejw and
0

jw0

with

0 < < 1.

The poles-zeros diagram looks like

The z-transform of the filter is then given by


H (z) = H (z) =
1 (1 e
jw 0

1)(1 e 1

jw 0

1)

12 cos(w0 )z

1+ z

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 x[n] the output of the filter formula
y [n] = x[n] 2cos(w0 )y [n 1] + y [n 2]
2

y [n]

is then given by the following iterative

The filter is implemented using the following code.

c l e a ra l l c l o s ea l l c l c F 0=6 9 7 ; F S=2 0 0 0 ; L A M B D A=0 . 9 8 ; %G e n e r a t ed i s c r e t e t i m es i g n a lx n=1 / F S : 1 / F S : 0 . 1 ; x=s i n ( 2 * p i * 6 9 7 * n )+s i n ( 2 * p i * 1 2 0 9 * n ) ; f i g u r e p l o t ( x )

g r i do n %P l o tp o l ed i a g r a mo ft h ef i l t e r o m e g a 0=2 * p i * F 0 / F S ; f i g u r e p o l e s=[ e x p ( j * o m e g a 0 )e x p ( j * o m e g a 0 ) ] ; p l o t ( r e a l ( p o l e s ( 1 ) ) ,i m a g ( p o l e s ( 1 ) ) ,' x r ' ) ; h o l do n p l o t ( r e a l ( p o l e s ( 2 ) ) ,i m a g ( p o l e s ( 2 ) ) ,' x r ' ) ; p l o t ( e x p ( j * ( 0 : 0 . 0 1 : 1 ) * 2 * p i ) ,' b ' ) a x i ss q u a r e g r i do n %P l o tp o l ed i a g r a mo ft h ef i l t e r o m e g a 0=2 * p i * F 0 / F S ; f i g u r e p o l e s=L A M B D A * [ e x p ( j * o m e g a 0 )e x p ( j * o m e g a 0 ) ] ; p l o t ( r e a l ( p o l e s ( 1 ) ) ,i m a g ( p o l e s ( 1 ) ) ,' x r ' ) ; h o l do n p l o t ( r e a l ( p o l e s ( 2 ) ) ,i m a g ( p o l e s ( 2 ) ) ,' x r ' ) ; p l o t ( e x p ( j * ( 0 : 0 . 0 1 : 1 ) * 2 * p i ) ,' b ' ) a x i ss q u a r e g r i do n %F i l t e rs i g n a l o m e g a 0=2 * p i * F 0 / F S ; b=1 ; a=[ 1 ;2 * L A M B D A * c o s ( o m e g a 0 ) ;L A M B D A ^ 2 ] ; y 1=m y _ f i l t e r ( x ,b ,a ) ; %P l o tt h ef i l t e r e ds i g n a l f i g u r e p l o t ( y 1 ) g r i do n %P l o tt h em a g n i t u d eo ft h eD F T / D F So ft h ef i l t e r e ds i g n a l f i g u r e Y 1=f f t ( y 1 ) ; p l o t ( ( 1 : l e n g t h ( Y 1 ) ) / l e n g t h ( Y 1 ) , a b s ( Y 1 ) )

We have used the function m y _ f i l t e r ( . . . ), which implements a filter written in direct form
b (1)+b (2)z

H (z) =

1++b (n b )z

n 1 b

1+a(2)z

1++a(n a )z

na 1

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 f s = 2000 Hz

we obtain the following output

Let us look at the magnitude of the DFT/DFS of this signal

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 2N multiplications and 2N 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 f i r p m ( . . . ) 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
dp ds

and

in the stop band and pass band, respectively.

c l e a ra l l c l o s ea l l c l c %P a r a m e t e r s F 0=6 9 7 ; F S=2 0 0 0 ; %G e n e r a t ed i s c r e t e t i m es i g n a lx n=1 / F S : 1 / F S : 0 . 1 ; x=s i n ( 2 * p i * 6 9 7 * n )+s i n ( 2 * p i * 1 2 0 9 * n ) ; %f i g u r e

%p l o t ( x ) %g r i do n %P a r k s M c C l e l l a n n u m b a n d s=3 ; b a n d s=0 . 5 * ( 1 / F S ) * [ 0F 0 2 0 0F 0 5 0F 0 + 5 0F 0 + 2 0 0F S ] ; d f=b a n d s ( 3 )-b a n d s ( 2 ) ; d e s=[ 001100 ] ; d p=0 . 1 ; d s=0 . 0 5 ; k=d s / d p ; w e i g h t=[ 1k1 ] ; t y p e=' b a n d p a s s ' ; g r i d d e n s i t y=1 6 ; n u m t a p s=c e i l ( 1 ( 1 0 * l o g 1 0 ( d p * d s ) + 1 3 ) / ( 2 . 3 2 4 * 2 * p i * d f ) )+1 ; h=m y _ r e m e z ( n u m t a p s ,n u m b a n d s ,b a n d s ,d e s ,w e i g h t ,t y p e ,g r i d d e n s i t y ) ;

%F i l t e r y 1=m y _ f i l t e r ( x ,h ,1 ) ; f i g u r e p l o t ( y 1 ) ; g r i do n f i g u r e Y 1=f f t ( y 1 ) ; p l o t ( ( 1 : l e n g t h ( Y 1 ) ) / l e n g t h ( Y 1 ) , a b s ( Y 1 ) ) g r i do n

Applying the filter to the same input signal x, we obtain the following output

where one tones has been attenuated, as illustrated by the norm of its Fourier transform

$$

Created Wed 30 Jan 2013 1:16 PM PET Last Modified Tue 19 Nov 2013 5:03 AM PET

Вам также может понравиться