Question

What is the best way to calculate the mean value (mean) and standard deviation (StdDev) for a continuous signal in Modelica. The mean and StdDev should be calculated for fixed period of time T; i.e., from t-T until t.

Was it helpful?

Solution

Below is a discrete solution to the problem. It is coded as a block in Modelica with 1 continuous input and 2 continuous output signals. Discretization is accomplished using the Modelica built-in function sample:

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top