The cross correlation function (xcorr) is not finding the correct phase lag between 2 signals

5 views (last 30 days)
I have two signals (they are the same variable but measured by two different pieces of equipment). I want to find the phase lag between the two signals so I can synchronize the data measured by the two pieces of equipment. The signals are sampled at the same frequency (100Hz). However when I run my code it finds zero phase lag which is incorrect as when I overlay the two signals they are not synchronised (figure 16 graph link below). Also it does not agree with the statistic programme (SPSS) which find the correct phase lag.
This is the relevant section of my code, what do I need to change to calculate the correct phase lag?
%Cross correlation of pedal angle from force pedals with pedal angle from Qualisys %Only consider the first 3 peaks section of both signals in cross correlation %assume no longer than 1000 data points (10seconds) %make signals the same length so can calculate cross correlation %coefficient (CCF)
[c,lags] = xcorr(Pedal_angle_qualisys(1:1000),Pedal_angle_sensix_rs(408:1407),'coeff');
%lags as column vector lags=lags';
%Find max CCF value [c_max,I] = max(c);
%Find lag at max CCF value lag_max=lags(I);
%plot CCF against lags figure(15) plot(lags,c) title({'Cross correlation between pedal angle','from Sensix and Qualisys'}) xlabel('Lag number') ylabel('Cross correlation') text(lags(I), c_max, sprintf('Lag = %6.0f',lag_max)) xlim([-1000 1000]);
%Move Sensix pedal angle by phase lag to sync Pedal_angle_sensix_lag=Pedal_angle_sensix_rs((408-lag_max):end); Pedal_angle_sensix_lag_SPSS=Pedal_angle_sensix_rs(413:end); % Lag caluclated in SPSS independently
%Plot synchronised Qualisys and Sensix pedal angle figure(16), plot(1:length(Pedal_angle_sensix_rs),Pedal_angle_sensix_rs,'r',1:length(Pedal_angle_sensix_lag),Pedal_angle_sensix_lag,'g',1:length(Pedal_angle_sensix_lag_SPSS),Pedal_angle_sensix_lag_SPSS,'b',1:length(Pedal_angle_qualisys),Pedal_angle_qualisys,'k') title('Pedal angle - Sensix convention - synchronised'); xlabel('data point'); ylabel('Pedal angle (degrees)'); legend('Pedal angle Sensix resampled','Pedal angle Sensix lag applied','Pedal angle Sensix SPSS lag applied','Pedal angle Qualisys','Location','northwest'); ylim([70 140]); xlim([0 750]);

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!