This page describes a simple circuit which produces ECGlike waveform. The waveform is not very detailed, but it contains a sharp depolarizing (rising) component, a slower hyperpolarizing (falling) component, and a repetition rate of approximately one beat per second making it potentially useful for testing heartbeat detection circuitry.
In 2019 I released a YouTube video and blog post showing how to build an ECG machine using an AD8232 interfaced to a computer’s sound card. At the end of the video I discussed how to use a 555 timer to create a waveform roughly like an ECG signal, but I didn’t post the circuit at the end of that video. I get questions about it from time to time, so I’ll share my best guess at what that circuit was here using LTSpice to simulate it.

The 555 timer generates pulses about once per second.

The diode (D1) causes the 555 to produce very short pulses. The duty of the pulses is controlled by the resistance in series with the diode (R3), with higher resistances resulting in larger duty.

The main purpose of the first opamp is to invert polarity of the signal emitted by the 555. The signal is a square wave at about 1Hz, but it is mostly high with brief low pulses.

The second opamp serves as a voltage buffer to stabilize the output, and the final series capacitor shifts the voltage so it’s centered around zero.

Unity gain opamps should have some feedback resistance to improve smallsignal stability in production applications, but for messing around here I felt fine omitting them.
This page shows how to use the LM741 opamp model file in LTSpice. This is surprisingly unintuitive, but is a good thing to know how to do. Model files can often be downloaded by vendor sites, but LTSpice only comes preloaded with models of common LT components.
I found LM741.MOD
available on the TI’s LM741 product page.
Save it wherever you want, but you will need to know the full path to this file later.
Open the model file in a text editor and look for the line starting with .SUBCKT
. The top of LM741.MOD looks like this:
* connections: noninverting input
*  inverting input
*   positive power supply
*    negative power supply
*     output
*     
*     
.SUBCKT LM741/NS 1 2 99 50 28
The last line tells us the name of this model’s subcircuit is LM741/NS
Click the “.op” button on the toolbar, then add .include
followed by the full path to the model file. After clicking OK place the text somewhere on your LTSpice circuit diagram.
We know the part we are including is a 5pin opamp, so we can start by placing a generic component. Notice the description says you must give the value a name and include this file. We will do this in the next step.
Rightclick the opamp and update its Value
to match the name of the subcircuit we read from the model file earlier.
Your new component will run using the properties of the model you downloaded.
Fitting an exponential curve to data is a common task and in this example we’ll use Python and SciPy to determine parameters for a curve fitted to arbitrary X/Y points. You can follow along using the fit.ipynb Jupyter notebook.
import numpy as np
import scipy.optimize
import matplotlib.pyplot as plt
xs = np.arange(12) + 7
ys = np.array([304.08994, 229.13878, 173.71886, 135.75499,
111.096794, 94.25109, 81.55578, 71.30187,
62.146603, 54.212032, 49.20715, 46.765743])
plt.plot(xs, ys, '.')
plt.title("Original Data")
To fit an arbitrary curve we must first define it as a function. We can then call scipy.optimize.curve_fit
which will tweak the arguments (using arguments we provide as the starting parameters) to best fit the data. In this example we will use a single exponential decay function.
def monoExp(x, m, t, b):
return m * np.exp(t * x) + b
In biology / electrophysiology biexponential functions are often used to separate fast and slow components of exponential decay which may be caused by different mechanisms and occur at different rates. In this example we will only fit the data to a method with a exponential component (a monoexponential function), but the idea is the same.
# perform the fit
p0 = (2000, .1, 50) # start with values near those we expect
params, cv = scipy.optimize.curve_fit(monoExp, xs, ys, p0)
m, t, b = params
sampleRate = 20_000 # Hz
tauSec = (1 / t) / sampleRate
# determine quality of the fit
squaredDiffs = np.square(ys  monoExp(xs, m, t, b))
squaredDiffsFromMean = np.square(ys  np.mean(ys))
rSquared = 1  np.sum(squaredDiffs) / np.sum(squaredDiffsFromMean)
print(f"R² = {rSquared}")
# plot the results
plt.plot(xs, ys, '.', label="data")
plt.plot(xs, monoExp(xs, m, t, b), '', label="fitted")
plt.title("Fitted Exponential Curve")
# inspect the parameters
print(f"Y = {m} * e^({t} * x) + {b}")
print(f"Tau = {tauSec * 1e6} µs")
Y = 2666.499 * e^(0.332 * x) + 42.494
Tau = 150.422 µs
R² = 0.999107330342064
We can use the calculated parameters to extend this curve to any position by passing X values of interest into the function we used during the fit.
The value at time 0 is simply m + b
because the exponential component becomes e^(0) which is 1.
xs2 = np.arange(25)
ys2 = monoExp(xs2, m, t, b)
plt.plot(xs, ys, '.', label="data")
plt.plot(xs2, ys2, '', label="fitted")
plt.title("Extrapolated Exponential Curve")
What if we know our data decays to 0? It’s not best to fit to an exponential decay function that lets the b
component be whatever it wants. Indeed, our fit from earlier calculated the ideal b
to be 42.494
but what if we know it should be 0
? The solution is to fit using an exponential function where b
is constrained to 0 (or whatever value you know it to be).
def monoExpZeroB(x, m, t):
return m * np.exp(t * x)
# perform the fit using the function where B is 0
p0 = (2000, .1) # start with values near those we expect
paramsB, cv = scipy.optimize.curve_fit(monoExpZeroB, xs, ys, p0)
mB, tB = paramsB
sampleRate = 20_000 # Hz
tauSec = (1 / tB) / sampleRate
# inspect the results
print(f"Y = {mB} * e^({tB} * x)")
print(f"Tau = {tauSec * 1e6} µs")
# compare this curve to the original
ys2B = monoExpZeroB(xs2, mB, tB)
plt.plot(xs, ys, '.', label="data")
plt.plot(xs2, ys2, '', label="fitted")
plt.plot(xs2, ys2B, '', label="zero B")
Y = 1245.580 * e^(0.210 * x)
Tau = 237.711 µs
The curves produced are very different at the extremes (especially when time is 0), even though they appear to both fit the data points nicely. Which curve is more accurate? That depends on your application. A hint can be gained by inspecting the time constants of these two curves.
Parameter 
Fitted B 
Fixed B 
m 
2666.499 
1245.580 
t 
0.332 
0.210 
Tau 
150.422 µs 
237.711 µs 
b 
42.494 
0 
By inspecting Tau I can gain insight into which method may be better for me to use in my application. I expect Tau to be near 250 µs, leading me to trust the fixedB method over the fitted B method. Choosing the correct method has great implications on the value of m
(which is also the value of the curve when time is 0).
How to apply lowpass, highpass, and bandpass filters with Python
This page describes how to perform lowpass, highpass, and bandpass filtering in Python. I favor SciPy’s filtfilt
function because the filtered data it produces is the same length as the source data and it has no phase offset, so the output always aligns nicely with the input. The sosfiltfilt
function is even more convenient because it consumes filter parameters as a single object which makes them easier work with.
import numpy as np
import scipy.signal
import scipy.io.wavfile
import matplotlib.pyplot as plt
def lowpass(data: np.ndarray, cutoff: float, sample_rate: float, poles: int = 5):
sos = scipy.signal.butter(poles, cutoff, 'lowpass', fs=sample_rate, output='sos')
filtered_data = scipy.signal.sosfiltfilt(sos, data)
return filtered_data
# Load sample data from a WAV file
sample_rate, data = scipy.io.wavfile.read('ecg.wav')
times = np.arange(len(data))/sample_rate
# Apply a 50 Hz lowpass filter to the original data
filtered = lowpass(data, 50, sample_rate)
# Code used to display the result
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 3), sharex=True, sharey=True)
ax1.plot(times, data)
ax1.set_title("Original Signal")
ax1.margins(0, .1)
ax1.grid(alpha=.5, ls='')
ax2.plot(times, filtered)
ax2.set_title("LowPass Filter (50 Hz)")
ax2.grid(alpha=.5, ls='')
plt.tight_layout()
plt.show()
import numpy as np
import scipy.signal
import scipy.io.wavfile
import matplotlib.pyplot as plt
def highpass(data: np.ndarray, cutoff: float, sample_rate: float, poles: int = 5):
sos = scipy.signal.butter(poles, cutoff, 'highpass', fs=sample_rate, output='sos')
filtered_data = scipy.signal.sosfiltfilt(sos, data)
return filtered_data
# Load sample data from a WAV file
sample_rate, data = scipy.io.wavfile.read('ecg.wav')
times = np.arange(len(data))/sample_rate
# Apply a 20 Hz highpass filter to the original data
filtered = highpass(data, 20, sample_rate)
# Code used to display the result
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 3), sharex=True, sharey=True)
ax1.plot(times, data)
ax1.set_title("Original Signal")
ax1.margins(0, .1)
ax1.grid(alpha=.5, ls='')
ax2.plot(times, filtered)
ax2.set_title("HighPass Filter (20 Hz)")
ax2.grid(alpha=.5, ls='')
plt.tight_layout()
plt.show()
import numpy as np
import scipy.signal
import scipy.io.wavfile
import matplotlib.pyplot as plt
def bandpass(data: np.ndarray, edges: list[float], sample_rate: float, poles: int = 5):
sos = scipy.signal.butter(poles, edges, 'bandpass', fs=sample_rate, output='sos')
filtered_data = scipy.signal.sosfiltfilt(sos, data)
return filtered_data
# Load sample data from a WAV file
sample_rate, data = scipy.io.wavfile.read('ecg.wav')
times = np.arange(len(data))/sample_rate
# Apply a 1050 Hz highpass filter to the original data
filtered = bandpass(data, [10, 50], sample_rate)
# Code used to display the result
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 3), sharex=True, sharey=True)
ax1.plot(times, data)
ax1.set_title("Original Signal")
ax1.margins(0, .1)
ax1.grid(alpha=.5, ls='')
ax2.plot(times, filtered)
ax2.set_title("BandPass Filter (1050 Hz)")
ax2.grid(alpha=.5, ls='')
plt.tight_layout()
plt.show()
This code evaluates the same signal lowpass filtered using different cutoff frequencies:
import numpy as np
import scipy.signal
import scipy.io.wavfile
import matplotlib.pyplot as plt
# Load sample data from a WAV file
sample_rate, data = scipy.io.wavfile.read('ecg.wav')
times = np.arange(len(data))/sample_rate
# Plot the original signal
plt.plot(times, data, '.', alpha=.5, label="original signal")
# Plot the signal lowpass filtered using different cutoffs
for cutoff in [10, 20, 30, 50]:
sos = scipy.signal.butter(5, cutoff, 'lowpass', fs=sample_rate, output='sos')
filtered = scipy.signal.sosfiltfilt(sos, data)
plt.plot(times, filtered, label=f"lowpass {cutoff} Hz")
plt.legend()
plt.grid(alpha=.5, ls='')
plt.axis([0.35, 0.5, None, None])
plt.show()
Artifacts may appear in the smooth signal if the first or last data point differs greatly from their adjacent points. This is because, in an effort to ensure the filtered signal length is the same as the input signal, the input signal is “padded” with data on each side prior to filtering. The default behavior is to pad the data by duplicating the first and last data points, but this causes artifacts in the smoothed signal if the first or last points contain an extreme value. An alternative strategy is Gustafsson’s Method, described in a 1996 paper by Fredrik Gustafsson in which “initial conditions are chosen for the forward and backward passes so that the forwardbackward filter gives the same result as the backwardforward filter.” Interestingly, the original publication demonstrates the method by filtering noise out of an ECG recording.
import numpy as np
import scipy.signal
import scipy.io.wavfile
import matplotlib.pyplot as plt
# Load sample data from a WAV file
sample_rate, data = scipy.io.wavfile.read('ecg.wav')
times = np.arange(len(data))/sample_rate
# Isolate a small portion of data to inspect
segment = data[350:400]
# Create a 5pole lowpass filter with an 80 Hz cutoff
b, a = scipy.signal.butter(5, 80, fs=sample_rate)
# Apply the filter using the default edge method (padding)
filtered_pad = scipy.signal.filtfilt(b, a, segment)
# Apply the filter using Gustafsson's method
filtered_gust = scipy.signal.filtfilt(b, a, segment, method="gust")
# Display the Results
plt.plot(segment, '.', alpha=.5, label="data")
plt.plot(filtered_pad, 'k', label="Default (Padding)")
plt.plot(filtered_gust, 'k', label="Gustafsson's Method")
plt.legend()
plt.grid(alpha=.5, ls='')
plt.title("Padded Data vs. Gustafsson’s Method")
plt.show()
An alternative strategy to lowpass a signal is to use convolution. In this method you create a kernel (typically a bellshaped curve) and convolve the kernel with the signal. The wider the window is the smoother the output signal will be. Also, the window must be normalized so its sum is 1 to preserve the amplitude of the input signal. Note that this method exclusively uses NumPy and does not require SciPy.
There are different for handling data at the edges of the signal, but setting mode
to valid
deletes insufficiently filtered points at the edges to produce an output signal that is fully filtered but slightly shorter than the input signal. See numpy.convolve
documentation for additional information.
The kernel shape affects the spectral properties of the filter. Commonly called window functions, these different shapes produce filtered signals with different frequency response characteristics. The Hanning window is preferred for most general purpose signal processing applications. See FftSharp for additional information about the pros and cons of common window functions.
import numpy as np
import scipy.io.wavfile
import matplotlib.pyplot as plt
# Load sample data from a WAV file
sample_rate, data = scipy.io.wavfile.read('ecg.wav')
times = np.arange(len(data))/sample_rate
# create a Hanning kernel 1/50th of a second wide
kernel_width_seconds = 1.0/50
kernel_size_points = int(kernel_width_seconds * sample_rate)
kernel = np.hanning(kernel_size_points)
# normalize the kernel
kernel = kernel / kernel.sum()
# Create a filtered signal by convolving the kernel with the original data
filtered = np.convolve(kernel, data, mode='valid')
# Display the result
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(10, 3))
ax1.plot(np.arange(len(kernel))/sample_rate, kernel, '.')
ax1.set_title("Kernel (1/50 sec wide)")
ax1.grid(alpha=.5, ls='')
ax2.plot(np.arange(len(data))/sample_rate, data)
ax2.set_title("Original Signal")
ax2.margins(0, .1)
ax2.grid(alpha=.5, ls='')
ax3.plot(np.arange(len(filtered))/sample_rate, filtered)
ax3.set_title("Convolved Signal")
ax3.margins(0, .1)
ax3.grid(alpha=.5, ls='')
plt.tight_layout()
plt.show()
Azure Pipelines makes it easy to run tests in the cloud, but I found that a new React projects made with createreactapp
fail to properly test in the cloud using the simple npm test
command. Attempting this would display No tests found related to files changed since last commit
but hang forever.
I solved this problem and got my React app to test properly in the cloud by adding  watchAll=false
after npm test
. This is my final azurepipelines.yml
file:
trigger:
 master
pool:
vmImage: "ubuntulatest"
steps:
 task: NodeTool@0
inputs:
versionSpec: "10.x"
displayName: "Install Node.js"
 script: npm install
displayName: "Install NPM"
 script: npm run build
displayName: "Build"
 script: npm test  watchAll=false
displayName: "Test"
A working React app that tests properly with Azure Pipelines is GitHub.com/swharden/AliCalc