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…

  1. Create a new Python file, and import the following packages:
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation 
  2. 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 
  3. Define an initializer function to initialize parameters of the plot:
    # Initializer function def initializer(): peak_val ...

Get Python Machine Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.