First Order Filters

This notebook goes through calculations of first order low- and high-pass filters.

To start, lets import the libraries that will be used during this tutorial.


In [ ]:
import numpy as np
import plotly.plotly as py
import plotly.graph_objs as go

Time Constant

The time constant of an RC circuit, $\tau$, can be describe by the following relation: $$ \tau = RC $$

The turnover or cutoff frequency can be determined with the time constant.

$$ f_c = \frac{1}{2 \pi \tau} $$

In [ ]:
# inputs
R = 1000  # resistace in ohms
C = 0.001  # capacitance in farads

tau = R*C
fc = 2*np.pi*tau
print('Time constant, tau =', tau, 's')
print('Cutoff frequency, f =', fc, 'Hz')


Time constant, tau = 1.0 s
Cutoff frequency, f = 6.283185307179586 Hz

Low-Pass (LP) Filter

A low pass filter uses a resistor and capacitor with the voltage read across the capacitor and the input voltage into the resistor. The reactance of the capacitor blocks low frequency signals.

The response of the low pass filter is,

$$ V_o = V_i \frac{1}{\sqrt{1+(\omega R C)^2}} $$

Where $ \omega $ is the frequency and $RC$ is $ \tau $.


In [ ]:
# inputs
Vi = 5  # volts

# define a linear space of omega that is significantly greatr that fc
omega = np.linspace(0.01*fc,fc*5,1000)
Vo_lp = Vi*1/np.sqrt(1+(omega*tau)**2)

Gdb_lp = 20*np.log10(Vo_lp/Vi)  # Where Gdb is the power 

# plot with plotly
# Create traces
legend = ['Low Pass Gain']
tracelp = go.Scatter(
    x=np.log10(omega),
    y=Gdb_lp,
    mode='lines',
    name=legend[0]
)

# Edit the layout
layout = dict(title='Output Voltage of First Order Low-Pass Filter vs. Time',
              xaxis=dict(title='Log[Frequency (Hz)]'),
              yaxis=dict(title='Power Gain (dB)'),
              )
data = [tracelp]  # put trace in array (plotly formatting)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename="FirstOrderLPFilter")

Now, we can find the $-3 dB$ frequency with this discrete data set if we interoplate, or if we just look at the graph and find the point where the Gain in dB is $-3$.


In [ ]:
freq3db = 0 # logarithmic frequency at -3db
freqcut = 10**freq3db
print('Therefore, the cutoff frequency is', freqcut, 'Hz')

High Pass Filter

A first order high-pass filter similarly uses a resistor and capacitor, however, the output voltage is measured across the resistor.

The response of the high-pass filter is,

$$ V_o = V_i \frac{\omega R C}{\sqrt{1+ (\omega R C)^2}}$$

In [ ]:
Vo_hp = Vi*(omega*tau)/np.sqrt(1+(omega*tau)**2)

Gdb_hp = 20*np.log10(Vo_hp/Vi)  # Where Gdb is the power 

# plot with plotly
# Create traces
legend = ['High Pass Gain']
tracehp = go.Scatter(
    x=np.log10(omega),
    y=Gdb_hp,
    mode='lines',
    name=legend[0]
)

# Edit the layout
layout = dict(title='Output Voltage of First Order High-Pass Filter vs. Time',
              xaxis=dict(title='Log[Frequency (Hz)]'),
              yaxis=dict(title='Power Gain (dB)'),
              )
data = [tracehp]  # put trace in array (plotly formatting)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename="FirstOrderHPFilter")

Now, overlaying the graphs


In [ ]:
# Edit the layout
layout = dict(title='Output Voltage of First Order High-Pass and Low-Pass Filter vs. Time',
              xaxis=dict(title='Log[Frequency (Hz)]'),
              yaxis=dict(title='Power Gain (dB)'),
              )
data = [tracehp, tracelp]  # put trace in array (plotly formatting)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename="FirstOrderFilters")