2013-07-16 20 views
0

我是Modelica的新手,並且無法嘗試對數組採樣連續的實數輸入信號。我嘗試過使用'樣本',但無法使其工作。下面的代碼的問題是每個x [i]是一個相同的採樣版本pdt秒。我想要的是x [1]成爲第一個同樣,x [2]成爲第二個樣本等等。將信號採樣到Modelica中的陣列

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; 

任何幫助將不勝感激!

在此先感謝!

回答

0

我不是100%確定你要做什麼。你是否試圖將最後10個樣本保存在數組中?如果是這樣,它是下面的代碼(x[1]總是最後一個樣本)。也可以使用sample(j*dt/10, dt)或類似的東西在不同的時間點對所有樣品進行採樣(如果需要n個樣品,但不希望第一個樣品始終爲最新樣品)。

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;
0

感謝您的回覆。你的代碼並不是我想要的,但它幫助我更多地瞭解Modelica和我想要的內容。這是下面的代碼。基本上,x [i] = p((i-1)* dt)。假設模擬時間爲1秒,您需要11個樣本。

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;