Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/notebooks/filter.ipynb
Views: 531
Modeling and Simulation in Python
Copyright 2017 Allen Downey
Low pass filter
The following circuit diagram (from Wikipedia) shows a low-pass filter built with one resistor and one capacitor.
A "filter" is a circuit takes a signal, , as input and produces a signal, , as output. In this context, a "signal" is a voltage that changes over time.
A filter is "low-pass" if it allows low-frequency signals to pass from to unchanged, but it reduces the amplitude of high-frequency signals.
By applying the laws of circuit analysis, we can derive a differential equation that describes the behavior of this system. By solving the differential equation, we can predict the effect of this circuit on any input signal.
Suppose we are given and at a particular instant in time. By Ohm's law, which is a simple model of the behavior of resistors, the instantaneous current through the resistor is:
where is resistance in ohms.
Assuming that no current flows through the output of the circuit, Kirchhoff's current law implies that the current through the capacitor is:
According to a simple model of the behavior of capacitors, current through the capacitor causes a change in the voltage across the capacitor:
where is capacitance in farads (F).
Combining these equations yields a differential equation for :
Follow the instructions blow to simulate the low-pass filter for input signals like this:
where is the amplitude of the input signal, say 5 V, and is the frequency of the signal in Hz.
Params and System objects
Here's a Params
object to contain the quantities we need. I've chosen values for R1
and C1
that might be typical for a circuit that works with audio signal.
Now we can pass the Params
object make_system
which computes some additional parameters and defines init
.
omega
is the frequency of the input signal in radians/second.tau
is the time constant for this circuit, which is the time it takes to get from an initial startup phase tocutoff
is the cutoff frequency for this circuit (in Hz), which marks the transition from low frequency signals, which pass through the filter unchanged, to high frequency signals, which are attenuated.t_end
is chosen so we run the simulation for 4 cycles of the input signal.
Let's make a System
Exercise: Write a slope function that takes as an input a State
object that contains V_out
, and returns the derivative of V_out
.
Note: The ODE solver requires the return value from slope_func to be a sequence, even if there is only one element. The simplest way to do that is to return a list with a single element:
Test the slope function with the initial conditions.
And then run the simulation. I suggest using t_eval=ts
to make sure we have enough data points to plot and analyze the results.
Here's a function you can use to plot V_out
as a function of time.
If things have gone according to plan, the amplitude of the output signal should be about 0.8 V.
Also, you might notice that it takes a few cycles for the signal to get to the full amplitude.
Sweeping frequency
Plot V_out
looks like for a range of frequencies:
At low frequencies, notice that there is an initial "transient" before the output gets to a steady-state sinusoidal output. The duration of this transient is a small multiple of the time constant, tau
, which is 1 ms.
Estimating the output ratio
Let's compare the amplitudes of the input and output signals. Below the cutoff frequency, we expect them to be about the same. Above the cutoff, we expect the amplitude of the output signal to be smaller.
We'll start with a signal at the cutoff frequency, f=1000
Hz.
The following function computes V_in
as a TimeSeries
:
Here's what the input and output look like. Notice that the output is not just smaller; it is also "out of phase"; that is, the peaks of the output are shifted to the right, relative to the peaks of the input.
The following function estimates the amplitude of a signal by computing half the distance between the min and max.
The amplitude of V_in
should be near 5 (but not exact because we evaluated it at a finite number of points).
The amplitude of V_out
should be lower.
And here's the ratio between them.
Exercise: Encapsulate the code we have so far in a function that takes two TimeSeries objects and returns the ratio between their amplitudes.
And test your function.
Estimating phase offset
The delay between the peak of the input and the peak of the output is call a "phase shift" or "phase offset", usually measured in fractions of a cycle, degrees, or radians.
To estimate the phase offset between two signals, we can use cross-correlation. Here's what the cross-correlation looks like between V_out
and V_in
:
The location of the peak in the cross correlation is the estimated shift between the two signals, in seconds.
We can express the phase offset as a multiple of the period of the input signal:
We don't care about whole period offsets, only the fractional part, which we can get using modf
:
Finally, we can convert from a fraction of a cycle to degrees:
Exercise: Encapsulate this code in a function that takes two TimeSeries
objects and a System
object, and returns the phase offset in degrees.
Note: by convention, if the output is shifted to the right, the phase offset is negative.
Test your function.
Sweeping frequency again
Exercise: Write a function that takes as parameters an array of input frequencies and a Params
object.
For each input frequency it should run a simulation and use the results to estimate the output ratio (dimensionless) and phase offset (in degrees).
It should return two SweepSeries
objects, one for the ratios and one for the offsets.
Run your function with these frequencies.
We can plot output ratios like this:
But it is useful and conventional to plot ratios on a log-log scale. The vertical gray line shows the cutoff frequency.
This plot shows the cutoff behavior more clearly. Below the cutoff, the output ratio is close to 1. Above the cutoff, it drops off linearly, on a log scale, which indicates that output ratios for high frequencies are practically 0.
Here's the plot for phase offset, on a log-x scale:
For low frequencies, the phase offset is near 0. For high frequencies, it approaches 90 degrees.
Analysis
By analysis we can show that the output ratio for this signal is
where , and the phase offset is
Exercise: Write functions that take an array of input frequencies and returns and as SweepSeries
objects. Plot these objects and compare them with the results from the previous section.
Test your function:
Test your function:
Plot the theoretical results along with the simulation results and see if they agree.
For the phase offsets, there are small differences between the theoretical results and our estimates, but that is probably because it is not easy to estimate phase offsets precisely from numerical results.
Exercise: Consider modifying this notebook to model a first order high-pass filter, a two-stage second-order low-pass filter, or a passive band-pass filter.