# Creating a digital filter, from Laplace to $\mathcal Z$-transform (zero order hold) to code?

I'm trying to create a digital filter in code(C) but any language is fine. Now I've got an analogue filter that I have represented by an equation in the Laplace domain and I want to try and implement it digitally.

So my filter has this form in the Laplace domain: $$\frac{as+b}{cs^2+ds}$$

I then use MATLAB's c2d command which uses the zero order hold transformation (I have a really poor grasp on this, so this might be wrong) and it gives me this formula:

$$\frac{\left(5\cdot 10^5\right)z-67}{z^2-z}$$

I tried following an example that I found that used the Tustin's method, though when I use the c2d function in MATLAB with Tustin it gives me an error.

My attempt has been

$$\frac{hz-i}{jz^2-kz}$$

$b_0=-i, b_1=h, b_2=0, a_0=0, a_1=-k, a_2=j$

Then from this I've tried (which is wrong) \begin{align} \text{output}&=z_0 b_0+z_1b_1+z_2b_2\\ z_2&=z_1\\ z_1&=z_0\\ z_0&=\text{input}-a_0z_0-a_1z_1-a_2z_2 \end{align}

• as far as i can tell, "Tustin method" is the same as "bilinear transform". so, first you need to be a little more clear in your notation. and then you need to apply the bilinear transform faithfully. to do that, you must be clear about what your sampling rate is. then, once you have a transfer function in the Z-domain, you can construct an algorithm similar to what you have done (i think you need to compute $z0$ first, not last) using the Direct Form I or Direct Form II (as you have done). – robert bristow-johnson Oct 10 '14 at 14:26
• @robertbristow-johnson Yeah thanks, after more reading i found out tustin and bilinear are the same thing, I was reading from different sources that used different names that got me a bit confused, computing $z0$ last is my problem is correct, though i may have the notation reversed from the standard form, which might make it look wrong. I've answered what i was trying to do bellow which isn't really an answer to the question, because it was quite a poor question. My problem was from not calculating my filter coeficients correctly – Darren Lanigan Oct 10 '14 at 14:43
• the bilinear transform is the subject of DSP textbooks, including on-line texts and wikipedia. are you having trouble with that? is the problem that you just don't know how to properly apply the bilinear transform? – robert bristow-johnson Oct 10 '14 at 14:50
• @robertbristow-johnson no sorry, ive explained myself quite poorly. My problem was a matlab problem, not a dsp problem(which i thought it was when i posted this question a few weeks ago). I couldnt get it to compute the bilinear transfer correctly which has since been resolved. Thanks – Darren Lanigan Oct 10 '14 at 14:56

The example I looked at used a tustin or bilinear conversion not a zero order hold(the default for matlabs "c2d" command). So this is more an answer to what i wanted to do rather than the question that i asked above.

I solved the following (converting the s domain function into code) by taking the s domain function. $$\frac{as+b}{cs^2+ds}$$

and putting this into matlab (matlab command "g=tf([a b],[c d 0])"). Then performed the bilenear conversion with the matlab command "c2d(g,Ts,'tustin')" where g was my transfer funtion and Ts my sampling rate. This produced the output

$$\frac{ez^2+fz+g}{iz^2+jz+k}$$

The a and b coeficients can then be taken from this equation such that(if $i!=1$ the equation needs to be multiplied through by the inverse of "i"): $b0=e$ $b1=f$ $b2=g$ $a0=i$ $a1=j$ $a2=k$

this can then be converted to code by setting the initial states for simplicity let $$z0=z1=z2=0$$

then set up a loop that repeats the following algorithm

$$output=z0*b0+z1*b1+z2*b2$$ $$z2=z1$$ $$z1=z0$$ $$z0=input-a1*z1-a2*z2$$

For anyone else that got lost like me, this is known as an IIR filter and googling IIR filter design helped sooo much.

• By using bilinear transform, make sure you understand the warping effect it applies to your analog frequency response. It warps the analog frequencies (0-Infiniti)to the nyquist frequency. Proper oversampling can resolve thus issue, but it's quiet computational heavy. – tiborsimon Jan 8 '15 at 17:15