【问题标题】:How to Create Autocorrelation Function without any buit-in functions like xcorr如何在没有任何内置函数(如 xcorr)的情况下创建自相关函数
【发布时间】:2013-11-01 00:22:42
【问题描述】:

我想在没有任何内置 MATLAB 函数的情况下自动关联随机噪声向量。

我给出的自相关方程是:

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

L = [0:200]

我已经编写了下面的代码,但 plot Rxx vs L plot 不是我所期望的。 我希望我的绘图以 L = 0L = 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)

【问题讨论】:

    标签: matlab correlation


    【解决方案1】:

    您在内部循环中有错误:您需要使用n1(j) = n(j+L); 而不是n1(j) = n(j)+L;。例如。您需要将 L 添加到索引而不是值。

    第二个错误如下:如果你想使用upperbound = numel(n)-1,那么你应该只使用等于0或1的L。例如。你的外循环将是

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

    除此之外,您还可以更正上限值:

    upperbound = numel(n) - maxL;
    

    maxL 是 L 的最大值,将在下一个循环中使用。

    另一个提示:如果将内部循环替换为标量积,例如,可以提高计算速度

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

    【讨论】:

    • 这不是我想要的,但它有助于引导我走上正确的道路。非常感谢丹尼尔
    【解决方案2】:

    我最终在上述代码的帮助下修复了我的脚本。

    clc

    全部清除

    randn('种子',2496132);

    z = randn(1,1024);

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

    上界 = 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)

    【讨论】:

      猜你喜欢
      • 2022-07-23
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      相关资源
      最近更新 更多