http://cn.mathworks.com/help/signal/examples/measuring-signal-similarities.html
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.
% Load data
load relatedsig.mat;
figure
ax(1) = subplot(311);
plot((0:numel(T1)-1)/Fs1,T1,'k');
ylabel('Template 1');
grid on
ax(2) = subplot(312);
plot((0:numel(T2)-1)/Fs2,T2,'r');
ylabel('Template 2');
grid on
ax(3) = subplot(313);
plot((0:numel(S)-1)/Fs,S);
ylabel('Signal');
grid on
xlabel('Time (secs)');
linkaxes(ax(1:3),'x')
axis([0 1.61 -4 4])
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.
[Fs1 Fs2 Fs]
ans = 4096 4096 8192
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 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 resample
function applies an anti-aliasing(low-pass) FIR filter to the signal during the resampling process.
[P1,Q1] = rat(Fs/Fs1); % Rational fraction approximation [P2,Q2] = rat(Fs/Fs2); % Rational fraction approximation T1 = resample(T1,P1,Q1); % Change sampling rate by rational factor T2 = resample(T2,P2,Q2); % Change sampling rate by rational factor
Finding a Signal in a Measurement
We can now cross-correlate signal S to templates T1 and T2 with the xcorr
function to determine if there is a match.
[C1,lag1] = xcorr(T1,S); [C2,lag2] = xcorr(T2,S); figure ax(1) = subplot(211); plot(lag1/Fs,C1,'k'); ylabel('Amplitude'); grid on title('Cross-correlation between Template 1 and Signal') ax(2) = subplot(212); plot(lag2/Fs,C2,'r'); ylabel('Amplitude'); grid on title('Cross-correlation between Template 2 and Signal') xlabel('Time(secs)'); axis(ax(1:2),[-1.5 1.5 -700 700 ])
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] = max(abs(C2)); SampleDiff = lag2(I) timeDiff = SampleDiff/Fs
SampleDiff = 499 timeDiff = 0.0609
The peak of the cross correlation implies that the signal is present in template T2 starting after 61 ms. In other words, signal T2 leads signal S by 499 samples as indicated by SampleDiff. This information can be used to align the signals.
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.
figure,
ax(1) = subplot(311);
plot(s1);
ylabel('s1');
grid on
ax(2) = subplot(312);
plot(s2,'k');
ylabel('s2');
grid on
ax(3) = subplot(313);
plot(s3,'r');
ylabel('s3');
grid on
xlabel('Samples')
linkaxes(ax,'xy')