# Add baseline drift and noise drift = 0.05 * np.sin(2 * np.pi * 0.2 * t) noise = 0.02 * np.random.randn(len(t)) return t, ecg + drift + noise t, signal = generate_ecg(bpm=85) plt.plot(t, signal) plt.title("Simulated ECG for Heartbeat Simulator") plt.xlabel("Time (s)") plt.ylabel("Amplitude (mV)") plt.grid(True) plt.show()
# P wave def p_wave(t_center, t): return 0.25 * np.exp(-((t - t_center)**2) / (0.04))
# QRS complex model (sum of Gaussians) def qrs(t_center, t): return 1.2 * np.exp(-((t - t_center)**2) / (0.01)) heartbeat simulator
A filtered analog voltage (0–5V) that mimics an ECG signal. Add a 0.1µF capacitor between output and GND to smooth PWM ripple. Part 3: Software Heartbeat Simulator (Python for Testing) For testing heart rate algorithms or generating synthetic data, use Python with numpy and matplotlib . Python Code: import numpy as np import matplotlib.pyplot as plt def generate_ecg(sample_rate=500, duration=10, bpm=75): t = np.linspace(0, duration, int(sample_rate*duration)) ecg = np.zeros_like(t)
beat_interval = 60 / bpm for i in range(int(duration / beat_interval)): center = i * beat_interval + 0.2 # offset to place QRS ecg += qrs(center, t) ecg += p_wave(center - 0.15, t) ecg += t_wave(center + 0.3, t) # Add baseline drift and noise drift = 0
void loop() // Output one heartbeat cycle for (int i = 0; i < sizeof(ecgWaveform); i++) analogWrite(9, ecgWaveform[i]); delay(10); // 10ms steps = 100Hz update
// Pause for rest of beat interval int waveformDuration = sizeof(ecgWaveform) * 10; // in ms int remainingTime = beatInterval - waveformDuration; if (remainingTime > 0) analogWrite(9, 128); // baseline delay(remainingTime); Python Code: import numpy as np import matplotlib
void setup() pinMode(9, OUTPUT);