使用 sample() 函数时无法为数组赋值

Can not assign values to an array when using sample() function

我正在尝试自己实现 DFT,因为 OpenModelica 中的 FFT 示例对我不起作用,我也不知道为什么。 但是我已经卡住了对正弦函数进行采样并将采样值分配给缓冲区数组。 这就是为什么我试图让它更简单,只是将一个计数器变量 "iTick" 分配给仍然不起作用的数组。 请参阅基本示例。

谁能告诉我为什么这不起作用,以及我如何在使用 sample() 函数时实际为数组赋值??

block DFT
  import Modelica.Constants.pi;

  parameter Integer N = 360 "Total number of samples";

  Integer iTick;
  Real y_buf[N];


algorithm

when sample(0, 0.1) then     
  iTick :=iTick + 1;

  if iTick >= 1 and iTick <= N then
    y_buf[iTick] := iTick;
  end if;
end when;

end DFT;
[358] 14:56:15 Symbolisch Warnung
The linear system: 
1 : $PRE.y_buf[2] = y_buf[2]
2 : y_buf[2] = $PRE.y_buf[2]
[
  -1.0 , 1.0 ;
  1.0 , -1.0
]
  *
[
  y_buf[2] ;
  $PRE.y_buf[2]
]
  =
[
  0.0 ;
  0.0
]
 might be structurally or numerically singular for variable $PRE.y_buf[2] since U(2,2) = 0.0. It might be hard to solve. Compilation continues anyway.

[359] 14:56:15 Symbolisch Warnung
The linear system: 
1 : $PRE.y_buf[1] = y_buf[1]
2 : y_buf[1] = $PRE.y_buf[1]
[
  -1.0 , 1.0 ;
  1.0 , -1.0
]
  *
[
  y_buf[1] ;
  $PRE.y_buf[1]
]
  =
[
  0.0 ;
  0.0
]
 might be structurally or numerically singular for variable $PRE.y_buf[1] since U(2,2) = 0.0. It might be hard to solve. Compilation continues anyway.

[360] 14:56:15 Übersetzung Warnung
Assuming fixed start value for the following 2 variables:
         y_buf[360]:DISCRETE(fixed = false )  type: Real  [360]
         iTick:DISCRETE(fixed = false )  type: Integer 

一旦您初始化 iTicky_buf,您的 "Symbolisch Warnung" 就会消失。但是,代码仍然不起作用。 OpenModelica 模拟了它,但是 y_buf 的项目永远不会更新。

此问题可能与 this 问题有关,其中 delay 运算符在算法部分不起作用。因此,我建议采用类似的解决方法:尽量避开算法部分。使用等式部分和适当的初始化,您的最小示例可能如下所示:

block DFT
  import Modelica.Constants.pi;

  parameter Integer N = 360 "Total number of samples";

  Integer iTick(start=0, fixed=true);
  Real y_buf[N](start=fill(0, N), fixed=fill(true, N));

equation 

  when sample(0, 0.1) then
    iTick = pre(iTick) + 1;
  end when;

  for i in 1:N loop
    when iTick >= i then
      y_buf[i] =  iTick;
    end when;
  end for;

end DFT;

经过长时间的搜索和尝试,我发现魔术词 "discrete" 解决了我的问题!我还不能解释为什么,但请看下面的工作示例:

model Test
  import Modelica.Constants.pi;

  parameter Integer N = 360 "Total number of samples";

  Integer iTick(start=0, fixed=true);
  discrete Real y_buf[N](start=fill(0,N), fixed=fill(true, N));


algorithm

when sample(0, 0.1) then     
  iTick :=iTick + 1;

  if iTick >= 1 and iTick <= N then
    y_buf[iTick] := iTick;
  end if;
end when;
end Test;

希望这对某人有所帮助!