问题
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