Escolar Documentos
Profissional Documentos
Cultura Documentos
Solutions
Academ ia
Support
Events
Com pany
Documentation Center
Search R2013b Documentation
Signal Processing Toolbox Signal Processing Toolbox Examples
Product Updates
Share
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
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
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
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
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
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
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
Site Help
Patents
Trademarks
Privacy Policy
www.mathworks.in/help/signal/examples/measuring-signal-similarities.html
7/7