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
dpb
2014년 3월 16일
I answered w/ an example precisely the same question within the last week or so. Here...
답변 (1개)
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
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!