Convert first order transfer function to c code [closed]

谁说我不能喝 提交于 2020-01-14 06:08:25

问题


I have a simple first order transfer such as "3/s+3" or "tf(3,[1 3])" function and I would like to implement in c code. I have a C function that is called with the delta time since the last iteration:

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

How do implement the transfer function 3/s+3 in C?


回答1:


It's not just a matter of implementing 3/(s+3) directly. You need to discretize it to the z-domain using an appropriate technique (forward euler, backward euler, tustin, zero-order hold) then implement the discrete version of the filter.

The following would be a simple version for the Tustin transformation. As written, the state needs to be initialized and stored somewhere externally to this function.

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


来源:https://stackoverflow.com/questions/15351315/convert-first-order-transfer-function-to-c-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!