What can I add to this function to allow me to normalize the cross-correlation of 2 signals in the frequency domain?

조회 수: 14 (최근 30일)
I am in the process of refining the "xcorrFD" function to include the option to normalize the cross-correlation of 2 signals in the frequency domain, mirroring the process undertaken by MATLAB's built-in "xcorr" function. I've come across answers on how to do so in the time domain but am facing some difficulties integrating them into "xcorrFD". I'm hoping someone can determine what I need to add to the current content of the function to achieve this. For a more detailed description for how the function works, I've included the author's DL link just above what is written. Thank you for your help.
xcorrFD DL Link: https://www.mathworks.com/matlabcentral/fileexchange/63353-xcorrfd-x-y
function [lags,ck,td] = xcorrFD(x,y)
a = y'; b = x';
len = length(a);
c = [ zeros(1,len-1) a ];
d = [ b zeros(1,len-1) ];
% Compute FFTs
X1 = fft(c);
X2 = fft(d);
% Compute cross correlation
X = X1.*conj(X2);
ck = ifft((X));
[~,i] = max(ck);
td = i - len;
lags = [-(len-1):len-1];

채택된 답변

Sudarsanan A K
Sudarsanan A K 2023년 12월 11일
Hello,
I understand that you are trying to modify the given implementation of "xcorrFD" function to include the normalization option in the frequency domain, and expecting the result obtained from MATLAB's built-in "xcorr" function.
The following example demonstrates how you can include the normalization (where the cross-correlations at zero lag are identically 1.0):
% Generate input signals
x = [1, 1, 1, 1];
y = [1, 1, 1, 1];
% Calculate the cross-correlation with normalization
[lags, ck_custom, td_custom] = xcorrFD(x, y, true); % see the function definition at the bottom of the script
% Calculate the cross-correlation using MATLAB's "xcorr" function with normalization
[ck_matlab, lags_matlab] = xcorr(x, y, 'normalized');
disp('Cross-correlation result:');
Cross-correlation result:
disp(ck_custom);
0.2500 0.5000 0.7500 1.0000 0.7500 0.5000 0.2500
disp('Lags:');
Lags:
disp(lags);
-3 -2 -1 0 1 2 3
% Plot the input signals and the cross-correlation result
figure
subplot(3,1,1);
stem(x);
xlabel('Sample index');
ylabel('Amplitude');
title('Signal x');
subplot(3,1,2);
stem(y);
xlabel('Sample index');
ylabel('Amplitude');
title('Signal y');
subplot(3,1,3);
stem(lags_matlab, ck_matlab, 'bo');
hold on
stem(lags, ck_custom, 'r*');
hold off
legend("xcorr - MATLAB built-in function", "xcorrFD - Custom function")
xlabel('Lag');
ylabel('Cross-correlation');
title('Cross-correlation of x and y');
%% xcorrFD function definition
function [lags, ck, td] = xcorrFD(x, y, normalize)
a = y(:); % Ensure y is a column vector
b = x(:); % Ensure x is a column vector
len = length(a);
c = [a; zeros(len-1, 1)]; % Zero-padding and flipping a
d = [b; zeros(len-1, 1)]; % Zero-padding b
% Compute FFTs
X1 = fft(c);
X2 = fft(d);
% Compute cross correlation
X = X1.*conj(X2);
ck = fftshift(ifft((X)));
if normalize
ck = ck / abs(ck(len)); % Normalize to ensure magnitude at zero lag is 1
end
[~, i] = max(ck);
td = i - len;
lags = -(len-1):len-1;
end
To know more about different scaling options in "xcorr" function, you can refer to the following MathWorks documentation:
I hope this helps you to achieve the normalization in the cross-correlation performed in the frequency domain.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by