Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 36 ยท Getting Started

A Python Toolkit for Signals

Set up the NumPy habits you need for signals and systems: time axes, sampling, vectorized math, text plots, and the SciPy tools used in this course.

Throughout this course you will build signals as NumPy arrays and systems as Python functions. A handful of habits makes that painless: how to build a time axis, how to evaluate a formula on it, how to measure the result and how to compare your own code with SciPy. This lesson sets those habits so that later lessons can focus on the theory.

Time axes

A discrete signal is an array of values; the time axis is a second array giving the instant of every value. For a sampling rate fs (samples per second) the spacing is T = 1/fs.

Python Building a time axis

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Prefer np.arange(N) / fs or linspace(..., endpoint=False) for periodic signals. With endpoint=True the last point would repeat the first when the signal is one period long.

Evaluating formulas on the axis

NumPy functions work on whole arrays at once (vectorization), so a formula for x(t) becomes one line.

Python A sum of two tones

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The factor 2 * np.pi * f * t converts a frequency in hertz to an angle in radians; get used to it, it appears in every chapter.

Measuring signals

Four numbers summarize most signals quickly.

  • Mean (x.mean()): the DC (constant) level.
  • Peak (np.abs(x).max()): the largest excursion.
  • Energy (np.sum(np.abs(x)**2) for samples; multiply by 1/fs to approximate the integral).
  • RMS (np.sqrt(np.mean(x**2))): the effective size, the number a voltmeter reports.

For a sine with amplitude A the RMS is A / sqrt(2), about 0.707 A.

Python RMS of a sine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Plotting without a plot

The in-browser runner can show Matplotlib figures, but a quick text plot is often enough to see a shape. This helper prints one line per sample with a star positioned by the value.

Python A tiny text plot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When you do want real figures, import matplotlib.pyplot as plt, call plt.plot(t, x) and plt.show(); the site shows the picture under the editor.

What SciPy adds

NumPy gives arrays and math. scipy.signal adds ready-made tools that we will rebuild by hand first and then compare with:

Task NumPy by hand SciPy
Convolution np.convolve scipy.signal.convolve
Frequency content np.fft.fft scipy.fft.fft
Filtering loops or convolution scipy.signal.lfilter
Transfer functions polynomial arithmetic scipy.signal.TransferFunction

Learning the idea first and the library second is deliberate: the library hides the details that exam and interview questions ask about.

Python By hand versus the library

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Habits that save time

  1. Keep fs, t and x together and name them consistently.
  2. Check shapes with .shape when a result looks odd.
  3. Avoid Python loops over samples when a vector expression exists.
  4. Compare your result with a second method (library, formula, or a special case with a known answer).
  5. Use np.allclose for floats instead of ==.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A signal is sampled at fs = 500 Hz. What is the spacing between samples?

    T = 1 / fs = 1 / 500 = 0.002 seconds.
  2. 2. What converts a frequency f in hertz to the angle inside np.sin?

    A sine of f hertz is sin(2 pi f t): 2 pi f is the angular frequency in radians per second.
  3. 3. What is the RMS value of 3 sin(2 pi 10 t)?

    RMS of a sine is amplitude divided by sqrt(2): 3 / 1.414 = 2.12.
  4. 4. Why compare floats with np.allclose instead of ==?

    Floating-point arithmetic rounds, so mathematically equal results often differ by tiny amounts.

Summary

  • Build time axes from fs: t = np.arange(N) / fs.
  • Evaluate formulas on whole arrays; use 2 * np.pi * f * t for sinusoids.
  • Mean, peak, energy and RMS are the quick measurements.
  • Implement ideas by hand first, then compare with scipy.signal.
  • Use np.allclose to compare floating-point results.

Next: Complex Numbers and Phasors