我正在尝试将测量噪声添加到模拟中。例如,在Simulink中可以做到这一点,但在Modelica和SystemModeler中似乎更难。
你有什么建议吗?
发布于 2017-02-23 18:09:49
另一种选择是使用Modelica.Blocks.Noise来避免自己编写外部代码(添加到2016年4月3日发布的Modelica标准库3.2.2中;即当最初的问题被问到时,它不会有帮助)。
Modelica.Blocks.Noise的一个好处是解决了采样、多种子等棘手问题。
发布于 2013-02-19 17:51:33
您可以通过外部C代码在Wolfram SystemModeler中添加白噪声。
Modelica代码(我已经从代码中删除了图注释,以便更容易阅读):
package WhiteNoise "Package for generating white noise"
extends Modelica.Icons.Library;
block NoiseNormal "Normally distributed random noise"
parameter Real mean=0 "Mean value of random noise";
parameter Real stdev=1 "Standard deviation of random noise";
parameter Real tSample=0.01 "Noise sample time";
Modelica.Blocks.Interfaces.RealOutput y;
equation
when initial() then
WhiteNoise.initRandomNormal();
end when;
when sample(0, tSample) then
y=mean + stdev*WhiteNoise.RandomNormal(time);
end when;
end NoiseNormal;
function initRandomNormal
external "C" ext_initRandomNormal() annotation(Include="#include \"ext_initRandNormal.c\"");
end initRandomNormal;
function RandomNormal
output Real y;
input Real u;
external "C" y=ext_RandomNormal(u) annotation(Include="#include \"ext_RandNormal.c\"");
end RandomNormal;
end WhiteNoise;外部代码:
ext_intRandNormal.c
#include <math.h>
#include <limits.h>
void ext_initRandomNormal()
{
srand(time(NULL));
}ext_RandNormal.c
#include <math.h>
#include <limits.h>
double ext_RandomNormal(double timein)
{
unsigned int seed = 0;
double v1, v2, r;
timein /= 100;
seed = (timein - floor(timein)) * UINT_MAX;
do
{
v1 = 2 * ((double) rand()) /((double) RAND_MAX) - 1;
v2 = 2 * ((double) rand()) /((double) RAND_MAX) - 1;
r = v1 * v1 + v2 * v2;
} while((r >= 1.0) || (r == 0.0));
return v1 * sqrt( - 2.0 * log(r) / r );
}https://stackoverflow.com/questions/14943950
复制相似问题