Animating dynamic signals
When we visualize real-time signals, it's nice to look at how the waveform builds up. In this recipe, we will see how to animate dynamic signals and visualize them as they are encountered in real time.
How to do it…
- Create a new Python file, and import the following packages:
import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation
- Create a function to generate a damping sinusoid signal:
# Generate the signal def generate_data(length=2500, t=0, step_size=0.05): for count in range(length): t += step_size signal = np.sin(2*np.pi*t) damper = np.exp(-t/8.0) yield t, signal * damper
- Define an
initializer
function to initialize parameters of the plot:# Initializer function def initializer(): peak_val = 1.0 buffer_val = 0.1
- Set these parameters:
ax.set_ylim(-peak_val * (1 + buffer_val), peak_val * (1 + buffer_val)) ax.set_xlim(0, 10) del x_vals[:] del y_vals[:] line...