2013-11-01 28 views
0

我想自動關聯一個隨機噪聲矢量與任何內置的MATLAB函數。是給予如何創建沒有任何內置函數的自相關函數,如xcorr

我的自相關方程爲:

Rxx[L] = ∑ from n = 1 to N-1 [x(n)*x(n+L)] 

L = [0:200] 

我已經寫了下面的代碼,但plotRxx VS Lplot是不是我期待的。 我期待我的陰謀從或L = 1開始在一定的最大值,因爲MATLAB開始在1的指數。然後指數下降和飽和在零分鐘。


clc 
clear all 

randn('seed',2496132); 
n = randn(1,1024); 

upperbound = numel(n)-1; 

for L = 1:200 

    for j = 1 : upperbound 

      n1(j) = n(j)+L; 
      Rxx(j) = (n(j)*n1(j));    

    end 

    Rxx_sum(L) = sum(Rxx); 
    Rxx = 0; 

end 

plot([1:200], Rxx_sum) 

回答

1

您在內環錯誤:你需要使用n1(j) = n(j+L);代替n1(j) = n(j)+L;。例如。您需要添加L來索引值。

第二個錯誤如下:如果你想使用upperbound = numel(n)-1比你應該使用L等於0或1只。例如。你外環將

for L = 0:1 
    ... 
    Rxx_sum(L+1) = sum(Rxx); 
    ... 

取而代之的是你也是正確的上界值可以:

upperbound = numel(n) - maxL; 

maxL是將在下一循環使用升最大值。

另一個提示:如果用標量產品替換內環,可以提高計算速度,例如,

for L = 1:200 
    Rxx_sum(L) = n(1:upperbound) * n(1+L:upperbound+L)';  
end 
+0

這不正是我一直在尋找但它有助於引導我走上正確的道路。非常感謝Danil –

0

我最終在上面的代碼的幫助下修復了我的腳本。

CLC

清楚所有

randn( '種子',2496132);

z = randn(1,1024);

n = [z零(1,200)];

upperbound = numel(z)-1;

爲L = 0:200

for j = 1 : upperbound 

    Rxx(j) = (n(j)*n(j+L));    

>end 

Rxx_sum(L+1) = sum(Rxx); 
Rxx = 0; 

圖([0:200],Rxx_sum)

相關問題