Question

I am new to Modelica and am having trouble trying to sample a continuous, real, input signal to an array. I have tried using 'when sample' but can't get it to work. The problem with the code below is that every x[i] is an identical sampled version of p every dt seconds. What I would like, is x[1] to be the first sameple, x[2] to be the second sample and so on.

model test_sample
  parameter Real dt = 0.1 "Precision of monitor";
  Real p;
  Real[10] x;
  Modelica.Blocks.Sources.Sine sine(freqHz=1);

equation 
  p = sine.y;

  for j in 1:10 loop
    when sample(0, dt) then
      x[j] = p;
    end when;
  end for;

end test_sample;

Any help would be greatly appreciated!

Thanks in advance!

Was it helpful?

Solution

I am not 100% sure what you are trying to do. Are you trying to keep the 10 last samples in the array? If so, it is the code below (x[1] is always the last sample). It is also possible to use sample(j*dt/10, dt) or something similar to sample them all at different points in time (if you want n number of samples, but do not want the first one to always be the latest sample).

model test_sample
  parameter Real dt = 0.1 "Precision of monitor";
  Real p;
  Real[10] x;
  Modelica.Blocks.Sources.Sine sine(freqHz=1);

equation 
  p = sine.y;
  when sample(0, dt) then
    x[1] = p;
    for j in 2:10 loop
      x[j] = pre(x[j-1]);
    end for;
  end when;

end test_sample;

OTHER TIPS

Thanks for the response. Your code wasn't exactly what I wanted, but it helped me understand more about Modelica and exactly what I wanted. Here is the code below. Basically, x[i] = p((i-1)*dt). This assumes the simulation is 1 second long and you want 11 samples.

model test_sample
  parameter Real dt = 0.1 "Precision of monitor";
  Real p;
  Real[11] x;
  Modelica.Blocks.Sources.Sine sine(freqHz=1);

algorithm 
  for j in 0:10 loop
    when time > (j-1)*dt and time <= j*dt then
      x[j] := p;
    end when;
  end for;

equation 
  p = sine.y;

end test_sample;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top