Chapter 5. SciPy for Signal Processing
We define a signal as data that measures either time-varying or spatially varying phenomena. Sound or electrocardiograms are excellent examples of time-varying quantities, while images embody the quintessential spatially varying cases. Moving images (movies or videos) are treated with the techniques of both types of signals, obviously.
The field of signal processing treats four aspects of this kind of data – its acquisition, quality improvement, compression, and feature extraction. SciPy has many routines to treat tasks effectively in any of the four fields. All these are included in two low-level modules (scipy.signal
being the main one, with an emphasis in time-varying data, and scipy.ndimage
, for images). Many of the routines in these two modules are based on Discrete Fourier Transform of the data.
In this chapter, we will cover the following things:
Discrete Fourier Transforms
Discrete Fourier Transform (DFT) transforms any signal from its time/space domain into a related signal in frequency domain. This allows us not only to analyze the different frequencies of the data, but also enables faster filtering operations, when used properly. It is possible to turn a signal in frequency domain back to its time/spatial domain, thanks to the Inverse Fourier Transform (IFT). We will not go into details of the mathematics behind these operators, since we assume familiarity at some level with this theory. We will focus on syntax and applications instead.
The basic routines in the scipy.fftpack
module compute the DFT and its inverse, for discrete signals in any dimension – fft
, ifft
(one dimension); fft2
, ifft2
(two dimensions); fftn
, ifftn
(any number of dimensions). All of these routines assume that the data is complex valued. If we know beforehand that a particular dataset is actually real valued, and should offer real-valued frequencies, we...
To aid the construction of signals with predetermined properties, the scipy.signal
module has a nice collection of the most frequent one-dimensional waveforms in the literature – chirp
and sweep_poly
(for the frequency-swept cosine generator), gausspulse
(a Gaussian modulated sinusoid), sawtooth
and square
(for the waveforms with those names). They all take as their main parameter a one-dimensional ndarray
representing the times at which the signal is to be evaluated. Other parameters control the design of the signal according to frequency or time constraints. Let's take a look into the following code snippet which illustrates the use of these one dimensional waveforms that we just discussed:
A filter is an operation on signals that either removes features or extracts some component. SciPy has a complete set of known filters as well as the tools to allow construction of new ones. The complete list of filters in SciPy is long, and we encourage the reader to explore the help documents of the scipy.signal
and scipy.ndimage
modules for the complete picture. We will introduce in these pages, as an exposition, some of the most used filters in the treatment of audio or image processing.
We start by creating a signal worth filtering:
First, we test the classical smoothing filter of Wiener and Kolmogorov, wiener
. We present in a plot
the original signal (in black) and the corresponding filtered data, with a choice of Wiener window of size 55 samples (in blue)...
In this chapter, we explored signal processing (any dimensional), including the treatment of signals in frequency space, by means of their Discrete Fourier Transforms. These correspond to the fftpack
, signal
, and ndimage
modules.
The Chapter 6, SciPy for Data Mining, will explore the tools included in SciPy to approach Statistical and Data Mining problems. In addition to standard statistical quantities, special topics like kernel estimation, statistical distances, and the clustering of big data sets will be presented.