Você está na página 1de 7

9/30/13

Measuring Signal Similarities - MATLAB & Simulink Example - MathWorks India


India Accelerating the pace of engineering and science Create Account Log In Contact Us Store

Products & Services

Solutions

Academ ia

Support

User Com m unity

Events

Com pany

Documentation Center
Search R2013b Documentation
Signal Processing Toolbox Signal Processing Toolbox Examples

Trial Softw are

Product Updates

Share

Measuring Signal Similarities


This example shows how to measure signal similarities. It will help you answer questions such as: How do I compare signals with different lengths or different sampling rates? How do I find if there is a signal or just noise in a measurement? Are two signals related? How to measure a delay between two signals (and how do I align them)? How do I compare the frequency content of two signals? Similarities can also be found in different sections of a signal to determine if a signal is periodic. Comparing Signals with Different Sampling Rates Consider a database of audio signals and a pattern matching application where you need to identify a song as it is playing. Data is commonly stored at a low sampling rate to occupy less memory. %L o a dd a t a l o a dr e l a t e d s i g . m a t ; f i g u r e a x ( 1 )=s u b p l o t ( 3 1 1 ) ; p l o t ( ( 0 : n u m e l ( T 1 ) 1 ) / F s 1 , T 1 , ' k ' ) ;y l a b e l ( ' T e m p l a t e1 ' ) ;g r i do n a x ( 2 )=s u b p l o t ( 3 1 2 ) ; p l o t ( ( 0 : n u m e l ( T 2 ) 1 ) / F s 2 , T 2 , ' r ' ) ;y l a b e l ( ' T e m p l a t e2 ' ) ;g r i do n a x ( 3 )=s u b p l o t ( 3 1 3 ) ; p l o t ( ( 0 : n u m e l ( S ) 1 ) / F s , S , ' b ' ) ;y l a b e l ( ' S i g n a l ' ) ;g r i do n x l a b e l ( ' T i m e( s e c s ) ' ) ; l i n k a x e s ( a x ( 1 : 3 ) , ' x ' ) a x i s ( [ 01 . 6 144 ] )

The first and the second subplot show the template signals from the database. The third subplot shows the signal which we want to search for in our database. Just by looking at the time series, the signal does not seem to match to any of the two templates. A closer inspection reveals that the signals actually have different lengths and sampling rates. [ F s 1F s 2F s ] a n s= 4 0 9 6 4 0 9 6 8 1 9 2

Different lengths prevent you from calculating the difference between two signals but this can easily be remedied by extracting the common part of signals. Furthermore, it is not always necessary to equalize lengths. Cross-correlation can be performed between signals with

www.mathworks.in/help/signal/examples/measuring-signal-similarities.html

1/7

9/30/13

Measuring Signal Similarities - MATLAB & Simulink Example - MathWorks India


different lengths, but it is essential to ensure that they have identical sampling rates. The safest way to do this is to resample the signal with a lower sampling rate. The r e s a m p l efunction applies an anti-aliasing(low-pass) FIR filter to the signal during the resampling process. [ P 1 , Q 1 ]=r a t ( F s / F s 1 ) ; [ P 2 , Q 2 ]=r a t ( F s / F s 2 ) ; T 1=r e s a m p l e ( T 1 , P 1 , Q 1 ) ; T 2=r e s a m p l e ( T 2 , P 2 , Q 2 ) ; Finding a Signal in a Measurement We can now cross-correlate signal S to templates T1 and T2 with the x c o r rfunction to determine if there is a match. [ C 1 , l a g 1 ]=x c o r r ( T 1 , S ) ; [ C 2 , l a g 2 ]=x c o r r ( T 2 , S ) ; f i g u r e a x ( 1 )=s u b p l o t ( 2 1 1 ) ; p l o t ( l a g 1 / F s , C 1 , ' k ' ) ;y l a b e l ( ' A m p l i t u d e ' ) ;g r i do n t i t l e ( ' C r o s s c o r r e l a t i o nb e t w e e nT e m p l a t e1a n dS i g n a l ' ) a x ( 2 )=s u b p l o t ( 2 1 2 ) ; p l o t ( l a g 2 / F s , C 2 , ' r ' ) ;y l a b e l ( ' A m p l i t u d e ' ) ;g r i do n t i t l e ( ' C r o s s c o r r e l a t i o nb e t w e e nT e m p l a t e2a n dS i g n a l ' ) x l a b e l ( ' T i m e ( s e c s ) ' ) ; a x i s ( a x ( 1 : 2 ) , [ 1 . 51 . 57 0 07 0 0] ) %R a t i o n a lf r a c t i o na p p r o x i m a t i o n %R a t i o n a lf r a c t i o na p p r o x i m a t i o n %C h a n g es a m p l i n gr a t eb yr a t i o n a lf a c t o r %C h a n g es a m p l i n gr a t eb yr a t i o n a lf a c t o r

The first subplot indicates that the signal and template 1 are less correlated while the high peak in the second subplot indicates that signal is present in the second template. [ ~ , I ]=m a x ( a b s ( C 2 ) ) ; t i m e D i f f=l a g 2 ( I ) / F s t i m e D i f f= 0 . 0 6 0 9

The peak of the cross correlation implies that the signal is present in template T2 starting after 61 ms. Measuring Delay Between Signals and Aligning Them Consider a situation where you are collecting data from different sensors, recording vibrations caused by cars on both sides of a bridge. When you analyze the signals, you may need to align them. Assume you have 3 sensors working at same sampling rates and they are measuring signals caused by the same event. f i g u r e , a x ( 1 )=s u b p l o t ( 3 1 1 ) ;p l o t ( s 1 , ' b ' ) ;y l a b e l ( ' s 1 ' ) ;g r i do n a x ( 2 )=s u b p l o t ( 3 1 2 ) ;p l o t ( s 2 , ' k ' ) ;y l a b e l ( ' s 2 ' ) ;g r i do n a x ( 3 )=s u b p l o t ( 3 1 3 ) ;p l o t ( s 3 , ' r ' ) ;y l a b e l ( ' s 3 ' ) ;g r i do n x l a b e l ( ' S a m p l e s ' ) l i n k a x e s ( a x , ' x y ' )

www.mathworks.in/help/signal/examples/measuring-signal-similarities.html

2/7

9/30/13

Measuring Signal Similarities - MATLAB & Simulink Example - MathWorks India

The maximum value of the cross-correlations between s1 and s2 and s1 and s3 indicate time leads/lags. [ C 2 1 , l a g 1 ]=x c o r r ( s 2 , s 1 ) ; [ C 3 1 , l a g 2 ]=x c o r r ( s 3 , s 1 ) ; f i g u r e s u b p l o t ( 2 1 1 ) ;p l o t ( l a g 1 , C 2 1 / m a x ( C 2 1 ) ) ;y l a b e l ( ' C 2 1 ' ) ; g r i do n t i t l e ( ' C r o s s C o r r e l a t i o n s ' ) s u b p l o t ( 2 1 2 ) ;p l o t ( l a g 2 , C 3 1 / m a x ( C 3 1 ) ) ;y l a b e l ( ' C 3 1 ' ) ; g r i do n x l a b e l ( ' S a m p l e s ' ) [ ~ , I 1 ]=m a x ( a b s ( C 2 1 ) ) ; [ ~ , I 2 ]=m a x ( a b s ( C 3 1 ) ) ; t 2 1=l a g 1 ( I 1 ) t 3 1=l a g 2 ( I 2 ) t 2 1= 3 5 0 %F i n dt h ei n d e xo ft h eh i g h e s tp e a k %F i n dt h ei n d e xo ft h eh i g h e s tp e a k %T i m ed i f f e r e n c eb e t w e e nt h es i g n a l ss 2 , s 1 %T i m ed i f f e r e n c eb e t w e e nt h es i g n a l ss 3 , s 1

t 3 1= 1 5 0

t21 indicates that s2 lags s1 by 350 samples, and t31 indicates that s3 leads s1 by 150 samples. This information can now used to align the 3 signals. s 2=[ z e r o s ( a b s ( t 2 1 ) , 1 ) ; s 2 ] ; s 3=s 3 ( t 3 1 : e n d ) ; f i g u r e a x ( 1 )=s u b p l o t ( 3 1 1 ) ;p l o t ( s 1 ) ;g r i do n ;t i t l e ( ' s 1 ' ) ;a x i st i g h t

www.mathworks.in/help/signal/examples/measuring-signal-similarities.html

3/7

9/30/13

Measuring Signal Similarities - MATLAB & Simulink Example - MathWorks India


a x ( 2 )=s u b p l o t ( 3 1 2 ) ;p l o t ( s 2 ) ;g r i do n ;t i t l e ( ' s 2 ' ) ;a x i st i g h t a x ( 3 )=s u b p l o t ( 3 1 3 ) ;p l o t ( s 3 ) ;g r i do n ;t i t l e ( ' s 3 ' ) ;a x i st i g h t l i n k a x e s ( a x , ' x y ' )

Comparing the Frequency Content of Signals A power spectrum displays the power present in each frequency. Spectral coherence identifies frequency-domain correlation between signals. Coherence values tending towards 0 indicate that the corresponding frequency components are uncorrelated while values tending towards 1 indicate that the corresponding frequency components are correlated. Consider two signals and their respective power spectra. F s=F s S i g ; %S a m p l i n gR a t e

[ P 1 , f 1 ]=p e r i o d o g r a m ( s i g 1 , [ ] , [ ] , F s , ' p o w e r ' ) ; [ P 2 , f 2 ]=p e r i o d o g r a m ( s i g 2 , [ ] , [ ] , F s , ' p o w e r ' ) ; f i g u r e t=( 0 : n u m e l ( s i g 1 ) 1 ) / F s ; s u b p l o t ( 2 2 1 ) ;p l o t ( t , s i g 1 , ' k ' ) ;y l a b e l ( ' s 1 ' ) ; g r i do n t i t l e ( ' T i m eS e r i e s ' ) s u b p l o t ( 2 2 3 ) ;p l o t ( t , s i g 2 ) ;y l a b e l ( ' s 2 ' ) ; g r i do n x l a b e l ( ' T i m e( s e c s ) ' ) s u b p l o t ( 2 2 2 ) ;p l o t ( f 1 , P 1 , ' k ' ) ;y l a b e l ( ' P 1 ' ) ;g r i do n ;a x i st i g h t t i t l e ( ' P o w e rS p e c t r u m ' ) s u b p l o t ( 2 2 4 ) ;p l o t ( f 2 , P 2 ) ;y l a b e l ( ' P 2 ' ) ;g r i do n ;a x i st i g h t x l a b e l ( ' F r e q u e n c y( H z ) ' )

The m s c o h e r efunction calculates the spectral coherence between the two signals. It confirms that sig1 and sig2 have two correlated components around 35 Hz and 165 Hz. In frequencies where spectral coherence is high, the relative phase between the correlated components can be estimated with the cross spectrum phase. [ C x y , f ]=m s c o h e r e ( s i g 1 , s i g 2 , [ ] , [ ] , [ ] , F s ) ; P x y =c p s d ( s i g 1 , s i g 2 , [ ] , [ ] , [ ] , F s ) ; p h a s e =a n g l e ( P x y ) / p i * 1 8 0 ;

www.mathworks.in/help/signal/examples/measuring-signal-similarities.html

4/7

9/30/13

Measuring Signal Similarities - MATLAB & Simulink Example - MathWorks India


[ p k s , l o c s ]=f i n d p e a k s ( C x y , ' M i n P e a k H e i g h t ' , 0 . 7 5 ) ; f i g u r e s u b p l o t ( 2 1 1 ) ; p l o t ( f , C x y ) ;t i t l e ( ' C o h e r e n c eE s t i m a t e ' ) ; g r i do n ; s e t ( g c a , ' x t i c k ' , f ( l o c s ) , ' y t i c k ' , . 7 5 ) ; a x i s ( [ 02 0 001 ] ) s u b p l o t ( 2 1 2 ) ; p l o t ( f , p h a s e ) ;t i t l e ( ' C r o s sS p e c t r u mP h a s e( d e g ) ' ) ; g r i do n ; s e t ( g c a , ' x t i c k ' , f ( l o c s ) , ' y t i c k ' , r o u n d ( p h a s e ( l o c s ) ) ) ; x l a b e l ( ' F r e q u e n c y( H z ) ' ) ; a x i s ( [ 02 0 01 8 01 8 0 ] )

The phase lag between the 35 Hz components is close to -90 degrees, and the phase lag between the 165 Hz components is close to -60 degrees. Finding Periodicities in a Signal Consider a set of temperature measurements in an office building during the winter season. Measurements were taken every 30 minutes for about 16.5 weeks. l o a do f f i c e t e m p . m a t %L o a dT e m p e r a t u r eD a t a F s=1 / ( 6 0 * 3 0 ) ; %S a m p l er a t ei s1s a m p l ee v e r y3 0m i n u t e s d a y s=( 0 : l e n g t h ( t e m p ) 1 ) / ( F s * 6 0 * 6 0 * 2 4 ) ; f i g u r e p l o t ( d a y s , t e m p ) t i t l e ( ' T e m p e r a t u r eD a t a ' ) x l a b e l ( ' T i m e( d a y s ) ' ) ;y l a b e l ( ' T e m p e r a t u r e( F a h r e n h e i t ) ' ) g r i do n

With the temperatures in the low 70s, you need to remove the mean to analyze small fluctuations in the signal. The x c o vfunction removes the mean of the signal before computing the cross-correlation. It returns the cross-covariance. Limit the maximum lag to 50% of the signal to get a good estimate of the cross-covariance.

www.mathworks.in/help/signal/examples/measuring-signal-similarities.html

5/7

9/30/13

Measuring Signal Similarities - MATLAB & Simulink Example - MathWorks India


m a x l a g s=n u m e l ( t e m p ) * 0 . 5 ; [ x c , l a g ]=x c o v ( t e m p , m a x l a g s ) ; [ ~ , d f ]=f i n d p e a k s ( x c , ' M i n P e a k D i s t a n c e ' , 5 * 2 * 2 4 ) ; [ ~ , m f ]=f i n d p e a k s ( x c ) ; f i g u r e p l o t ( l a g / ( 2 * 2 4 ) , x c , ' k ' , . . . l a g ( d f ) / ( 2 * 2 4 ) , x c ( d f ) , ' k v ' , ' M a r k e r F a c e C o l o r ' , ' r ' ) g r i do n s e t ( g c a , ' X l i m ' , [ 1 51 5 ] ) x l a b e l ( ' T i m e( d a y s ) ' ) t i t l e ( ' A u t o c o v a r i a n c e ' )

Observe dominant and minor fluctuations in the auto-covariance. Dominant and minor peaks appear equidistant. To verify if they are, compute and plot the difference between the locations of subsequent peaks. c y c l e 1=d i f f ( d f ) / ( 2 * 2 4 ) ; c y c l e 2=d i f f ( m f ) / ( 2 * 2 4 ) ; s u b p l o t ( 2 1 1 ) ;p l o t ( c y c l e 1 ) ;y l a b e l ( ' D a y s ' ) ;g r i do n t i t l e ( ' D o m i n a n tp e a kd i s t a n c e ' ) s u b p l o t ( 2 1 2 ) ;p l o t ( c y c l e 2 , ' r ' ) ;y l a b e l ( ' D a y s ' ) ;g r i do n t i t l e ( ' M i n o rp e a kd i s t a n c e ' ) m e a n ( c y c l e 1 ) m e a n ( c y c l e 2 ) a n s= 7

a n s= 1 . 0 0 0 0

www.mathworks.in/help/signal/examples/measuring-signal-similarities.html

6/7

9/30/13

Measuring Signal Similarities - MATLAB & Simulink Example - MathWorks India

The minor peaks indicate 7 cycle/week and the dominant peaks indicate 1 cycles per week. This makes sense given that the data comes from a temperature-controlled building on a 7 day calendar. The first 7-day cycle indicates that there is a weekly cyclic behavior of the building temperature where temperatures lower during the weekends and go back to normal during the week days. The 1-day cycle behavior indicates that there is also a daily cyclic behavior - temperatures lower during the night and increase during the day. Was this topic helpful?

Yes

No

Try MATLAB, Simulink, and Other Products


Get trial now

1994-2013 The MathWorks, Inc.

Site Help

Patents

Trademarks

Privacy Policy

Preventing Piracy Join the conversation

www.mathworks.in/help/signal/examples/measuring-signal-similarities.html

7/7

Você também pode gostar