# laplace transform on simple low pass filter in python

Looking at the first picture in the link, showing a simple graph of an with- and without bypass filter circuit voltage difference, i wanted to recreate this picture.

I have coded the noise and created the graph of the noisy signal:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

v_in = np.random.uniform(-0.005,0.005,150)+5
v_out = 1/(1+1*0.1E-6*s)*v_in #this line is wrong, no such thing as '*s'

plt.plot(v_in)
plt.plot(v_out)
plt.axis([0, 150, 4.95, 5.05])
plt.title('Bypass capacitor')
plt.ylabel('Volts')
plt.xticks(v_in, " ")
plt.show()

## Problem

Now the difficult part is that i want to solve this by having the parallel capacitor be represented as an RC low pass circuit with R = 1, and the corosponding transfer function of $$V_{out} = \dfrac{1}{1+RCs} \cdot V_{in}$$

I am confused with what to do with the 's', i think a laplace transform of the input? please help, i can work with impedances and AC-frequency, but a complex signal is new. a bit of theory behind the laplace 's' variable followed by the simple demo partialy set up would be very much appriciated!

• If you're wondering what to do with 's', You need to take a few steps back and read about the Laplace transform. – Chu Jun 22 '17 at 21:51
• This is what i would use in matlab: mathworks.com/help/control/ref/lsim.html no idea about python – sstobbe Jun 23 '17 at 1:10
• Are you trying to design a simple filter that you can code? – Andy aka Aug 8 '17 at 9:08

You're trying to plot in the time domain (ie. the x-axis is in seconds) but your formula is in the frequency domain (s is a complex frequency variable). You would need to perform the inverse Laplace transform to get back to the time domain.

This cannot be done unless you have a s-domain expression for Vin, which is tricky given that you just created it with random time domain values. Generally when doing this sort of s-domain stuff the output is only calculated for very simple (but very useful) inputs like step functions, impulses or single frequency sine waves.

There are many ways to go about what you're doing, and many of them require years of study. As suggested by @sstobbe, I think time domain simulation is probably your best route. Fortunately Python (via the SciPy library) has an equivalent lsim function. You just have to construct a system transfer function using lti. Something like this:

import matplotlib.pyplot as plt
from scipy import signal

sys = signal.lti(1,[1*0.1E-6,1])
t = np.linspace(0, 5, 150)
v_in = np.random.uniform(-0.005,0.005,150)+5
tout, y, x = signal.lsim(sys, v_in, t)

plt.plot(t, y)

Your voltage is given in the time domain (voltage values as function of time $t$).

Your low pass filter definition is given in the s-domain (transfer function as function of complex frequency $s$).

Both defintions don't fit together well (I wonder anyhow where you got the transfer function definition of the filter from if you don't know what to do with it).

I propose to use a definition of your low pass filter in the time domain, too.

Such a definition is called an ordinary differential equation and there are well known methods (e.g. very simple Euler method or more sophisticated Runge-Kutta methods; also offered by SciPy; see below) to solve it numerically for your particular input signal.

The ODE for the low pass filter is:

$\frac{d}{dt}v_{out}(t) = \frac{1}{RC}(v_{in}(t) - v_{out}(t))$

(you get it by applying KVL, Ohms's law and the V-I-relationship of the capacitor $\frac{d}{dt}v=\frac{1}{C}i$)

And this is what the Python script would look like:

import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
#%matplotlib inline

v_in = np.random.uniform(-0.005, 0.005, 150) + 5
tPoints = np.linspace(0, 150 - 1, 150)

# define function that yields v_in(t) at arbitrary values of t:
def vIn(t):
global tPoint, v_in
return np.interp(t, tPoints, v_in)

# define constants used in the ODE:
R = 1.0;   C = 1.0

# define dvOut/dt of ODE:
def dvOut_dt(vOut, t):
global R, C
return 1 / (R * C) * (vIn(t) - vOut)

# define starting value:
v_out_start = v_in[0]

# solve ODE by numerical integration:
v_out = integrate.odeint(dvOut_dt, v_out_start, tPoints)

plt.plot(v_in)
plt.plot(v_out)
plt.axis([0, 150, 4.95, 5.05])
plt.title('Bypass capacitor')
plt.ylabel('Volts')
plt.show()

The advantage of this solution (compared to the one given by Heath Raftery) is that is more generic. I.e. it works also for non-linear systems (e.g. if you include a diode somewhere in the circuit) and also for time-variant systems (e.g. if you suddenly change a value of some component) whereas the Laplace approach works only for LTI systems.