Reality Bending LabNews › Generate an artificial ECG signal in Python

Generate an artificial ECG signal in Python

2019-05-17 · Methods · Dominique Makowski

Create a natural ECG signal

Generating artificial physiological signals can be very useful to build, test your analysis pipeline or develop and validate a new algorithm.

Generating a synthetic, yet realistic, ECG signal in Python can be easily achieved with the ecg_simulate() function available in the NeuroKit2 package.

In the example below, we will generate 8 seconds of ECG, sampled at 200 Hz (i.e., 200 points per second) - hence the length of the signal will be 8 * 200 = 1600 data points. We can also specify the average heart rate, although note that there will be some natural variability (which is a good thing, because it makes it realistic).

import neurokit2 as nk  # Load the package

simulated_ecg = nk.ecg_simulate(duration=8, sampling_rate=200, heart_rate=80)

nk.signal_plot(simulated_ecg, sampling_rate=200)  # Visualize the signal

The simulation is based on the ECGSYN algorithm (McSharry et al., 2003).

However, for fast and stable results (as the realistic algorithm naturally generates some variability), one can approximate the QRS complex by a Daubechies wavelet. An ECG based on this method can also be obtained in NeuroKit by changing the method as follows:

simulated_ecg = nk.ecg_simulate(duration=8, sampling_rate=200, method="daubechies")

nk.signal_plot(simulated_ecg, sampling_rate=200)

While faster and stable, the generated ECG is far from being realistic.

👉 Discover more about NeuroKit here 👈

References

McSharry, P. E., Clifford, G. D., Tarassenko, L., & Smith, L. A. (2003). A dynamical model for generating synthetic electrocardiogram signals. IEEE Transactions on Biomedical Engineering, 50(3), 289-294.


Thanks for reading! 🐦 Don't forget to join me on X (@Dom_Makowski)

Keep reading

Click to Open
Reality Bending Lab

Exploring the neuropsychology of reality and its distortions.

The Reality Bending Lab, led by Dominique Makowski at the University of Sussex in Brighton, UK, conducts world-leading research on reality perception, illusions, fake news, AI-beliefs, deception and its links with emotions, cognitive control and the Self. Using recording signals from the body (ECG, EDA…) and the brain (EEG), we analyse data using advanced modelling (Bayesian statistics, chaos theory, computational models) and develop open-source tools to improve neuropsychological science.

“Do not try to bend the spoon — that’s impossible. Instead, only try to realise the truth: there is no spoon. Then you'll see that it is not the spoon that bends, it is only yourself.”

People

Information

Where to find us

The Reality Bending Lab is based in the School of Psychology, at the University of Sussex in Brighton, UK.

Pevensey 1 - room 2B7, School of Psychology, University of Sussex, BN1 9QH

The University of Sussex campus in the South Downs University of Sussex crest

University of Sussex

Brighton is the sunniest city in the UK and, by some margin, its most cheerfully strange — on the sea, an hour from London. The University of Sussex sits just above it in the South Downs, and its School of Psychology is one of the largest and strongest in the country.

It is famous for its research on consciousness and interoception, with scientists like Anil Seth, Andy Clark, Zoltan Dienes and Hugo Critchley, its teaching of cutting-edge psychological methods (with stats rockstar Andy Field), its history of multidisciplinary research (arts and science), and its radical engagement in open and disruptive science.

Join this community

Hire us Join the Lab Like this website? See how to get yours