How can I use cross-correlation as a tool to align two signals in MATLAB?

2 次查看(过去 30 天)
How can I use cross-correlation as a tool to align two signals in MATLAB?

回答(1 个)

Image Analyst
Image Analyst 2014-3-16
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);
See, the max is where the Gaussian aligns with the tall box, not where it aligns with "itself".
  2 个评论
Image Analyst
Image Analyst 2014-3-16
For comparison, here's what you get if the box is not in the signal:
And here's what you get if the template is centered at 90.
I imagine from looking at those you can figure out something.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by