0

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?

gregoiregentil
  • 1,793
  • 1
  • 26
  • 56
  • This isn't clear. Is "5/s+3" in the Laplace domain? Are you asking how to perform the inverse Laplace transform programmatically? – Oliver Charlesworth Mar 12 '13 at 00:46
  • I suppose it's in Laplace domain. By the way, that should be `3/(s+3)`, no? – Eitan T Mar 12 '13 at 00:47
  • I edited the question. Yes, it's 3/(s+3) and it's in the Laplace domain. – gregoiregentil Mar 12 '13 at 00:51
  • 1
    @gregoiregentil: Ok, well [there are tables](http://en.wikipedia.org/wiki/Laplace_transform#Table_of_selected_Laplace_transforms) of standard Laplace identities. If you have a rational polynomial in `s`, then you can just use partial-fraction techniques, and then you're basically done! – Oliver Charlesworth Mar 12 '13 at 00:57
  • 2
    To say it explicitly, you don't implement transfer functions in the Laplace domain. That's good for analysis/design/etc, but when implementing we live in the real domain, so you need to convert that TF to an impulse response or difference equation to actually implement it. – lxop Mar 12 '13 at 01:01

1 Answers1

2

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;
}
Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • 1
    Why do you need to go via the z-domain? You can trivially obtain a symbolic representation of the continuous-time equivalent, and then evaluate it at whatever sample rate is required. – Oliver Charlesworth Mar 12 '13 at 01:10
  • 1
    Because your continuous time equivalent sampled at a given sample rate is equivalent to the z-transformation, so why not just go there rather than introduce an intermediate step. – Phil Goddard Mar 12 '13 at 01:34
  • Only because it seems simpler to use trivial tables of identities than to go via the z-transform! – Oliver Charlesworth Mar 12 '13 at 01:35
  • Sorry for the necromancy, just thought I'd clear it up: @OliverCharlesworth's reason is that deriving the exact inverse Laplace equation will give exact values, no matter the sampling, while with Z-transform it's only an approximation, as good as it may be -- which needs a reasonably low (depending on the transform) sampling rate, too. – a concerned citizen Jul 01 '18 at 16:44