Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 38 ยท Getting Started

Your DSP Toolkit in Python

Set up a practical Python toolkit for signal processing: generate signals with NumPy, plot them with Matplotlib and use scipy.signal helpers, with runnable examples.

Every lesson in this course runs code in your browser, and the same few tools appear again and again. This lesson introduces them so that later lessons can focus on the signal ideas. You need three libraries:

  • NumPy holds signals as arrays and does fast element-wise math.
  • SciPy (scipy.signal, scipy.fft) provides ready-made filters, windows and transforms. We use it to check our own implementations.
  • Matplotlib draws plots. On this site a figure appears under the code after you call plt.show().

Time axes and the sampling grid

Always build a signal from a sampling rate and a duration. Create the time axis with np.arange(N) / fs rather than np.linspace so the spacing is exactly 1/fs and the end point is not repeated.

Python Generating a signal from fs and a duration

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 signals

For discrete signals use a stem plot, because the signal has values only at the sample positions. For long signals a normal line plot is fine.

Python Stem plot and line 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.

Useful building blocks

Task NumPy / SciPy
Sine wave np.sin(2*np.pi*f*t)
Square, sawtooth scipy.signal.square, scipy.signal.sawtooth
Chirp (sweeping tone) scipy.signal.chirp
White noise rng.standard_normal(N) with rng = np.random.default_rng(seed)
Impulse scipy.signal.unit_impulse(N)
Convolution np.convolve, scipy.signal.convolve
FFT np.fft.fft, np.fft.rfft, scipy.fft.fft
Windows np.hanning, scipy.signal.get_window
Filter design scipy.signal.firwin, butter, remez
Apply a filter scipy.signal.lfilter, filtfilt

Seed the random generator whenever you want repeatable results, as the examples here do.

Python Test signals from scipy.signal

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 gap between zero crossings shrinks as the chirp speeds up: a quick check without drawing anything.

Measuring a signal

A few numbers describe almost any signal:

  • Mean (DC level): x.mean()
  • Peak: np.max(np.abs(x))
  • RMS (root mean square, the effective amplitude): np.sqrt(np.mean(x**2))
  • Power: np.mean(x**2); a sine of amplitude A has power A**2/2 and RMS A/sqrt(2).
  • Signal-to-noise ratio in decibels: 10*np.log10(P_signal / P_noise).
Python RMS, power and SNR

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.

Decibels

Ratios in DSP span many orders of magnitude, so we use the logarithmic decibel. For power, dB = 10*log10(P1/P0). For amplitude (voltage, sample value), dB = 20*log10(A1/A0), because power grows with the square of the amplitude. Memorize three anchors: halving the amplitude is about -6 dB, doubling is +6 dB, and a factor of 10 in amplitude is 20 dB.

Python Amplitude ratios in decibels

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 in one variable and compute time axes from it.
  2. Use rfft for real signals; it returns only the non-redundant half.
  3. Always check your hand-written code against SciPy on a small example.
  4. Plot before you trust a number, but print numbers to be exact.
  5. Do not convert to integers early; keep signals in floating point.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which expression builds a time axis with spacing exactly 1/fs for N samples?

    Sample n happens at time n/fs, so dividing the index array by fs gives the exact grid.
  2. 2. A sine of amplitude 4 has what power (mean of x squared)?

    Power is A^2/2 = 16/2 = 8.
  3. 3. Halving the amplitude of a signal changes its level by about:

    20*log10(0.5) = -6.02 dB.
  4. 4. Why use a stem plot for a discrete signal?

    Between samples there is no value, so connecting the points with a line would imply values that do not exist.

Summary

  • Build signals from fs, a duration and t = np.arange(N)/fs.
  • Use stem plots for discrete sequences and line plots for long ones.
  • scipy.signal supplies test signals, windows, filters and convolution.
  • Mean, peak, RMS, power and SNR summarize a signal; decibels express ratios (20*log10 for amplitude, 10*log10 for power).
  • Seed random generators so results can be repeated.

Next: Quantization and the ADC