Cross-correlation signal alignment
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Albert Zurita
am 15 Dez. 2022
Kommentiert: William Rose
am 22 Dez. 2022
I would like to align two pulses through the use of cross-correlation. I create two pulses located at different parts within a sequence of 100 samples. For instance:
sig1 = zeros(1,100);
sig1(71:90) = 1;
sig2 = zeros(1,100);
sig2([21:41]) = 1;
I then do the cross-correlation between both signals
[xc1,l1] = xcorr(sig1,sig2);
[mx1,ix1] = max(xc1);
Usign the lag information from cross-correlation I can align both sequences where the maximum cross-correlation is found.
plot(1:100,sig1,'.-','linewidth',2);hold on;grid on;
plot((1:100)+l1(ix1),sig2,'.-','linewidth',2);hold on;grid on;
But this only seem to work when sig2 is not wrapped around the begin and the end. For the case where sig2 is:
sig2 = zeros(1,100);
sig2([1:10 91:100]) = 1;
Then the procedure doesn't work. Probably I should wrap the lags and the signal to align the sig2 pulse to the master sig2? Perhaps I should do circular cross-correlation in frequency domain or convolution? Thanks
0 Kommentare
Akzeptierte Antwort
William Rose
am 15 Dez. 2022
Bearbeitet: William Rose
am 16 Dez. 2022
[edit: Add comment about circular correlation; correct typos.]
You need to use a circular correlation, because that treats each signal as if it wraps around on itself. Unfortunately, xcorr() does not have a 'circular' option. Therefore try the following to get the circular correlation function:
Use the inverse FFT of the point-by-point product of the FFT and the conjugate FFT of the signals.
circcorr_ab = ifft(fft(a).*conj(fft(b)));
Here is your code using the function above.
sig1 = zeros(1,100);
sig1(71:90) = 1;
sig2 = zeros(1,100);
sig2([21:40]) = 1;
[xc1,l1] = xcorr(sig1,sig2);
[mx1,ix1] = max(xc1);
subplot(311), plot(1:100,sig1,'.r-');hold on;grid on;
plot((1:100)+l1(ix1),sig2,'xb-');hold on;grid on;
% Make a new sig2
sig2 = zeros(1,100);
sig2([1:10 91:100]) = 1;
% Compute circular cross correlation with inverse FFT
xc2 = ifft(fft(sig1).*conj(fft(sig2)));
l2=-length(sig1):length(sig1);
% Find max of circulation cross correlation
[mx2,ix2] = max(xc2);
% Plot shifted signal
subplot(312)
plot(1:100,sig1,'.r-');hold on;grid on;
plot((1:100)+l2(ix2),sig2,'xb-');hold on;grid on;
% Middle plot looks incomplete
% Extend sig2, then plot sig1 and the extended sig2
sig2ext=[sig2,sig2(1:abs(l2(ix2)))];
% Plot sig1 and extended sig2
subplot(313)
plot(1:100,sig1,'.r-');hold on;grid on;
plot((1:(100+abs(l2(ix2))))+l2(ix2),sig2ext,'xb-');hold on;grid on;
The middle plot looks incomplete, so I extended sig2, then plotted sig1 and extended sig2.
2 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!