简体   繁体   English

倍频程变量定义错误

[英]Octave variable definition error

I'm trying to generate Blackman-Harris window coefficients in Octave. 我正在尝试在Octave中生成Blackman-Harris窗口系数。 I have declared a function in a .m file like so: 我已经在.m文件中声明了一个函数,如下所示:

function result = BlackmanHarris(window_size)
  a0 = 0.35875
  a1 = 0.48829
  a2 = 0.14128
  a3 = 0.01168
  result = [0:window_size - 1];

  if(nargin != 1)
    print_usage("BlackmanHarris(int window_size)");
  endif

  if(isinteger(window_size))
    for n = 0:window_size - 1
      result(n) = a0 - (a1 * cos((2 * pi * n)/(window_size - 1))) + (a2 * cos((4 * pi * n)/(window_size - 1))) - (a3 * cos((6 * pi * n)/(window_size - 1)));
    endfor
  else
    error("BlackmanHarris: Expecting integer argument.");
  endif
endfunction

When I attempt to run this, I get the error: 当我尝试运行此命令时,出现错误:

>> window = BlackmanHarris(window_size);
error: 'a0' undefined near line 15 column 16
error: called from
    BlackmanHarris at line 15 column 14

I have tried declaring the variables as 'global' and 'persistent', nether of which solve this issue. 我尝试将变量声明为“全局”和“持久”,它们共同解决了这个问题。 I'm sure I'm just doing something slightly wrong, but Google has yielded little help on this. 我敢肯定我只是在做些错误的事情,但是Google在这方面没有什么帮助。

Thanks in advance. 提前致谢。

As @Sardar_Usama said, I was attempting to access element 0 of the result array, which of course will not work. 正如@Sardar_Usama所说,我试图访问结果数组的元素0,这当然是行不通的。

Changing that to n+1 fixed the issue. 将其更改为n + 1可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM