cross corelation in matlab

29 次查看(过去 30 天)
Mohamed Jamall
Mohamed Jamall 2020-6-7
编辑: Adam Danz 2020-6-12
Hi guys, I have two vectors , one called v1 and the other v2 , the size of v1 is 10k , and the other v2's size is 32 , I want to search inside the vector v1 if there's v2 , this means when I find that the vector v1 has the same data as v2 then there would be a high amplitude in the plot ..
so Im trying to do do cross corelation between those two vectors in matlab, how can I do that?
another question, also Im trying to understand the meaning of cross corelation, so I build one for loop and inside it I did shift indexes by 32 (size of v2 vector) to v2 (v(i:i+32)*v1) -product of sum, and then Im trying to see if the output gies me the required plot to see if there's a high amplituide if v2 found inside v1 .. but the plot is suck and couldn't see anything .. any help how can I do the cross corelation by for loop? thanks alot
  2 个评论
Mohamed Jamall
Mohamed Jamall 2020-6-7
Im using matlab platform , Im trying to do like this: I have two vectors one called v1 and the other v2 with difference size, trying to find out if v2 is found in v1 and once there's v2 inside v1 then at the plot of cross corelation there would be a high amplitude , this means when I do cross corelation v2 with v1 , and I plot the cross corelation of those two vectors, then the plot would be like there's a high amplitude once v1 figured in v2 and otherwise would be zero .
like something _______|_____|_____| , here for instance v1 appeared in v2 three times because there's three highs with high amplitudes in the plot of cross corelation .. so I want to do something like that any help please?
Adam Danz
Adam Danz 2020-6-10
It sounds like you were on the right track. What does it mean "the plot is stuck"?
The animation below illustrates the concept of cross correlation between two vectors of different lengths.

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2020-6-8
编辑:Adam Danz 2020-6-12
If you have two signals, S1 and S2 where S2 is a segment of S1, you can find the location of S2 in S1 by incrementally shifting S2 along S1 and computing the correlation or sum of squared error (SSE) between S2 and the windowed S1.
In this demo, fullCurve is the full S1 signal and segment is the S2 segment of S1. The correlation and SSE are shown in the third subplot. Correlation is maximized and SSE is minimized when S2 aligns with S1. You can find the optimal shift of S2 by locating the minimized SSE or maximized correlation.
Avoid using Matlab's xcorr() function when the lengths of S1 and S2 are not equal. xcorr will equate their lengths by padding 0s to the end of S2 which will affect the correlation values.
Code to recreate this demo. See inline comments for details.
%% Create a curve
x = 1:360;
r = linspace(-pi,pi,numel(x));
fullCurve = exp(3 *(cos(r+pi/4)-1)); % Von Mises
% Isolate a segment of the curve
idx = 160:200;
segment = fullCurve(idx);
%% Compute cross correlation & SSE
% Define moving windows
windowStart = 1:numel(fullCurve)-numel(segment)+1;
windowStop = windowStart + numel(segment)-1;
% loop through each window and compute correlation and sum of squared error
rho = nan(size(windowStart));
lags = 1:numel(windowStart);
sse = rho;
for i = lags
rho(i) = corr(segment', fullCurve(windowStart(i):windowStop(i))');
sse(i) = sum((segment - fullCurve(windowStart(i):windowStop(i))).^2);
end
% Find max correlation
[~, maxIdx] = max(rho);
%% Plot the data
% Show maximized fit
figure('Name', 'CrossCorrDemo')
sh(1) = subplot(3,1,1);
title('Full curve and segment')
hold on
plot(x, fullCurve, 'k-','LineWidth', 3)
plot(x(idx), fullCurve(idx), 'r:','LineWidth', 3)
xlim([min(x),max(x)])
grid on
% Show animated cross corr
sh(2) = subplot(3,1,2);
title('Animated cross correlation')
hold on
plot(x, fullCurve, 'k-','LineWidth', 3)
segmentLine = plot(nan(size(segment)), segment, 'r:', 'LineWidth', 4);
xlim([min(x),max(x)])
grid on
xl(1) = xline(min(x), 'k--');
xl(2) = xline(min(x), 'k--');
% Show animated correlation & SSE
sh(3) = subplot(3,1,3);
title('Correlation')
hold on
xlabel('Lag (shift index value)')
yyaxis left
ylabel('rho')
cor = plot(nan(size(lags)),nan(size(lags)), 'o','MarkerSize',3, 'Color', sh(3).YAxis(1).Color);
xlim([min(x),max(x)])
ylim([min(rho), max(rho)])
yyaxis right
ylabel('SSE')
sseLine = plot(nan(size(lags)), nan(size(lags)), 'o', 'MarkerSize', 3, 'Color', sh(3).YAxis(2).Color); %ADDED
ylim([min(sse), max(sse)])
grid on
yyaxis left
% Loop through iterations
for i = 1:numel(rho)
% Shift segment
segmentLine.XData = lags(i) + (0:numel(segment)-1);
% Update correlation plot
cor.XData(i) = lags(i);
cor.YData(i) = rho(i);
% Update SSE
sseLine.XData(i) = lags(i);
sseLine.YData(i) = sse(i);
% Update window bound lines
xl(1).Value = min(segmentLine.XData);
xl(2).Value = max(segmentLine.XData);
% Pause at max corr / min sse
if i == maxIdx
plot(sh(2),segmentLine.XData,segment,'m:','LineWidth',3)
xline(sh(3), lags(i), 'k-', lags(i), 'LabelVerticalAlignment', 'middle');
xline(sh(2),min(segmentLine.XData), 'k:','LineWidth',2);
xline(sh(2),max(segmentLine.XData), 'k:','LineWidth',2);
drawnow()
pause(.4)
end
% Update plot every 3 frames
if mod(i,3)==0
drawnow
end
end

Community Treasure Hunt

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

Start Hunting!

Translated by