How can I use cross-correlation as a tool to align two signals in MATLAB?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
How can I use cross-correlation as a tool to align two signals in MATLAB?
1 comentario
dpb
el 16 de Mzo. de 2014
I answered w/ an example precisely the same question within the last week or so. Here...
Respuestas (1)
Image Analyst
el 16 de Mzo. de 2014
And I hope you know (though you probably don't because it's not well known) that the max of the cross correlation does not guarantee the best alignment. Just imagine a signal that's a Gaussian hump on the left and a tall box (taller than the Gaussian) on the right. And you're correlating it with a template that's the same as the Gaussian. The max will not be where the two Gaussians would align. Is that surprising to you? Run this demo to find out why:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
sigma = 10
x = 1:200;
longSignal = exp(-(x-50).^2/sigma^2);
longSignal(120:160) = 2;
subplot(3,1,1);
plot(x, longSignal, 'b-', 'LineWidth', 3);
grid on;
title('Long Signal', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Make template to correlate with.
template = exp(-(x-50).^2/sigma^2);
subplot(3,1, 2);
plot(x, template, 'b-', 'LineWidth', 3);
grid on;
title('Template Signal', 'FontSize', fontSize);
ylim([0 2]);
xc = xcorr(longSignal, template);
subplot(3,1, 3);
plot(xc, 'b-', 'LineWidth', 3);
grid on;
title('Cross Correlation', 'FontSize', fontSize);
![](https://www.mathworks.com/matlabcentral/images/broken_image.png)
See, the max is where the Gaussian aligns with the tall box, not where it aligns with "itself".
2 comentarios
Image Analyst
el 16 de Mzo. de 2014
For comparison, here's what you get if the box is not in the signal:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178145/image.jpeg)
And here's what you get if the template is centered at 90.
![](https://www.mathworks.com/matlabcentral/images/broken_image.png)
I imagine from looking at those you can figure out something.
Ver también
Categorías
Más información sobre Bartlett en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!