Академический Документы
Профессиональный Документы
Культура Документы
Help
in Matlab or
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
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
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 ) ;
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
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
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
Given the time invariance of the system, y1I nputDelayed must be equal to 100 samples, i.e., y1I nputDelayed
= y 1(100 : 901) .
y1
Indeed
By simply typing
m a x E r r o r
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)
that we add to the sequence of two pulses x1 and tones" of the Fourier module, that is
x2
xN oisy
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
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 )
w a v p l a y ( j i n g l e , F s )
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 ) ;
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
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.
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(...).
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
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.
f f t ( X , K )
of length
with a sequence
of length $$L
the result is of length N-L+1. Therefore, we will consider N-L+1 point fft.
(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 ) ;
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.
obtained using the following code and depicted in the figure below.
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]
r[t] =
p [t]
obtained using the following code and depicted in the figure below.
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.
[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
%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
[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
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 .
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.
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]
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
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
%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