计算Modelica中的平均值和标准差

时间:2013-07-29 19:01:06

标签: modelica

在Modelica中计算连续信号的平均值(平均值)和标准偏差(StdDev)的最佳方法是什么。应该在固定的时间段T内计算平均值和StdDev;即,从t-T到t。

1 个答案:

答案 0 :(得分:3)

以下是该问题的离散解决方案。它被编码为Modelica中的一个块,具有1个连续输入和2个连续输出信号。使用Modelica内置函数样本完成离散化:

block MeanStdDevDiscr 
"Determines the mean value and standard deviation of a signal for a fixed time interval T."
  extends Modelica.Blocks.Interfaces.BlockIcon;
  import SI = Modelica.SIunits;

  parameter SI.Time T = 0.1 
    "Time interval used for calculating mean value and standard deviation";
  parameter Integer n = 10 "number of increments in T";

  Modelica.Blocks.Interfaces.RealInput u "signal input"
    annotation (Placement(transformation(extent={{-140,-20},{-100,20}})));
  Modelica.Blocks.Interfaces.RealOutput[2] y 
    "y[1] = average value; y[2] = standard deviation"
    annotation (Placement(transformation(extent={{100,-10},{120,10}})));

protected 
  parameter SI.Time dt = T/n "Precision of monitor";
  Real[n] uArray;

initial equation 
  uArray = ones(n)*u;

equation 
  when sample(0, dt) then
    uArray[1] = u;
    for j in 2:n loop
      uArray[j] = pre(uArray[j-1]);
    end for;
  end when;
  y[1] = sum(uArray)/n; // mean value
  y[2] = sqrt(sum((uArray .- y[1]).^2)/n); // standard deviation

  annotation (Diagram(graphics));
end MeanStdDevDiscr;