将一阶转移函数转换为c代码

我有一个简单的一阶转移,如“3 / s + 3”或“tf(3,[1 3])”函数,我想用c代码实现。 我有一个C函数,自上次迭代后调用delta时间:

double output(double input, double t); //usually, t is around 0.01 second 

如何在C中实现传递函数3 / s + 3?

这不仅仅是直接实现3 /(s + 3)的问题。 您需要使用适当的技术(前向欧拉,后向欧拉,tustin,零阶保持)将其离散到z域,然后实现filter的离散版本。

以下是Tustin转型的简单版本。 如上所述,状态需要初始化并存储在此函数外部的某处。

 double firstOrderLag(double input, double coeff, double dT, double *state){ // Function to implement the discretization of a continuous time first // order lag sys = coeff/(s+coeff) using the Tustin (Bilinear) transformation. double num = (1/(1+2/coeff/dT)); // numerator double den = (1-2/coeff/dT)*num; // denominator double temp; double output; temp = input - den*(*state); output = num*(temp + (*state)); *state = temp; return output; }