crosscor.m

来自「matlab算法集 matlab算法集」· M 代码 · 共 50 行

M
50
字号
function z = crosscor (x,y)
%-----------------------------------------------------------------------
% Usage:       z = crosscor (x,y)
%
% Description: Compute the normalized cross-correlation of two 
%              discrete-time signals.
%
% Inputs:      x = n by 1 vector containing samples of first signal
%              y = m by 1 vector containing samples of second signal
%                  where m <= n.
%
% Outputs:     z = n by 1 vector containing normalized cross-correlation
%                  of x with y. 
%
% Note:        The auto correlation of a signal x is just the cross
%              correlation of x with x.
%-----------------------------------------------------------------------
   
% Initialize

   chkvec (x,1,'crosscor');
   chkvec (y,2,'crosscor');
   n = length(x);
   m = length(y);
   if m > n
      disp ('Argument 2 of crosscor can not be longer than argument 1');
      return
   end;

% Compute DFTs of x(k) and y(k), pad y with (n-m) zeros     
   
   Y = zeros(n,1);
   Y(1:m) = y;
   X = dft (x,1);
   Y = dft (Y,1);

% Compute inveres DFT of  X(m)conj(Y(m)) */

   X = X .* conj(Y);
   z = real(dft (X,-1));

% Normalize 
   
   a = dot(x,x);
   b = dot(y,y);
   c = sqrt(a)*sqrt(b);
   z = z/c;
%-----------------------------------------------------------------------

⌨️ 快捷键说明

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