f_corr.asv

来自「digital signal processing常用工具箱」· ASV 代码 · 共 69 行

ASV
69
字号
function r = f_corr (x,y,circ,norm)
% F_CORR: Fast cross-correlation of two discrete-time signals
%
% Usage:  r = f_corr (x,y,circ,norm)
%
% Entry:  x    = vector of length L containing first signal
%         y    = vector of length M <= L containing second signal
%         circ = correlation type code:
%
%                0 = linear correlation
%                1 = circular correlation
%
%         norm = normalization code:
%
%                0 = no normalization 
%                1 = normalized cross-correlation
%
% Exit:  r = vector of length L contained selected cross- 
%            correlation of x with y.
%
% Notes: To compute auto-correlation use y = x.
%
% See also: f_conv

% Initialize

L = length(x);
M = length(y);

if M > L
   fprintf ('\nThe length of argument 2 of fcorr exceeds the length')
   fprintf ('\nof argument 1.\n')
   f_wait
   return;
end

if circ

% Compute circular cross-correlation

if M ~= L
   fprintf ('\nThe length of argument 2 of fcorr must equal the length')
   fprintf ('\nof argument 1 for a circular cross-correlation.\n')
   f_wait
   return;
else
X = fft(f_tocol(x),L);
Y = fft(f_tocol(y),L);
s = ifft(X .* conj(Y))/L;
r = real(s);

else
    
% Compute linear cross-correlation

N = 2^ceil(log(L+M-1)/log(2));
X = fft(f_tocol(x),N);
Y = fft(f_tocol(y),N);
s = ifft(X .* conj(Y))/L;
r = real(s(1:L));

% Normalize 

if norm 
   P_x = sum(x .^ 2)/L;
   P_y = sum(y .^ 2)/M;
   r = r/sqrt((M/L)*P_x*P_y);
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?