autocorr.m

来自「calculate the correlation and convolutio」· M 代码 · 共 39 行

M
39
字号
%genertate the autocorrelation with two approaches..
%Programmed by Benjamin.Frank, 19-Feb-2009

function y = autocorr(x)

x = randn(1,20);
%the first method
N = length(x);
M = 10;
Rx = zeros(1,M+1);
for m=1:M+1,
    for n=1:N-M+1
        Rx(m) = Rx(m)+x(n)*x(n+m-1);
        Rx(m) = Rx(m)/(N-m+1);
    end
end

%the second method..
N=length(x);
L=M+1;
x1=zeros(N+L-1,1);
x2=x1;
x=x-mean(x);
x1(1:N,1)=x;
for k=1:L
	x2=zeros(N+L-1,1);
	x2(k:N+k-1,1)=x;
	r(k)=x1'*x2;
end
r=r/N;

if 1,
    stem(Rx);
    hold on
    stem(r);
end


⌨️ 快捷键说明

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