In this chapter, you will be introduced to the use of wavelets and wavelet transforms as an alternative method of spectral analysis. We will discuss a number of common wavelets and introduce the Wavelet Toolbox of the MATLAB® software.
wavelet; continuous wavelet transform; Morlet wavelet; Mexican hat wavelet; scalogram; discrete wavelet transform
In this chapter, you will be introduced to the use of wavelets and wavelet transforms as an alternative method of spectral analysis. We will discuss a number of common wavelets and introduce the Wavelet Toolbox of the MATLAB® software.
In Chapter 12, “Frequency Analysis Part II: Nonstationary Signals and Spectrograms,” introduced the short-time Fourier transform (STFT) to decompose the frequency composition of nonstationary signals. Under certain situations, though, the STFT results in a less-than-optimal breakdown of frequency as a function of time. With increased precision in frequency distribution, localization in time becomes less precise. In other words, there is a time–frequency precision tradeoff. The reverse is also true: better temporal localization reduces the precision of the frequency distribution. This may bring to mind the well-known relationship of position and momentum of the Heisenberg uncertainty principle.
One of the benefits of the STFT is that the transform window can be chosen to optimize the resolution of frequency or localization of that frequency in time. A larger window allows for better frequency resolution, and a smaller window allows for better temporal resolution. However, for the STFT, the window size is constant throughout the algorithm. So, while the STFT can optimize for frequency or time in a given signal, the choice in the time-frequency tradeoff holds for the entire signal. This can pose a problem for some nonstationary signals. The wavelet transform provides an alternative to the STFT that often provides a better frequency/time representation of the signal.
A wavelet is a function that satisfies at least the following two criteria:
1. The integral of the function ψ(x) over all x is 0.
(13.1)
2. The square of ψ(x) has integral 1. A function adhering to this property is called square-integrable.
(13.2)
Fulfilling the first criterion mandates that the wavelet function has an equal area above and below zero. Fulfilling the second criterion mandates that the function approach zero at positive and negative infinity. Because of this second criterion, the function decays away from the origin, unlike sinusoidal or other infinite waves (thus, wavelet).
The continuous wavelet transform (CWT) is analogous to the continuous Fourier transform:
(13.3)
Here, the parameter t is the typical t in the time series x(t). The parameter s is called scale and is analogous to frequency for Fourier transforms. The wavelet function itself varies with both s and t:
(13.4)
The inclusion of t and s allows the function to be scaled and translated (shifted) for different values of s and t. The original wavelet function (untranslated and unscaled) is often termed the mother wavelet, since the set of wavelet functions is generated from that initial function.
The scaling provides a significant benefit over the short-time Fourier transform. The multiple scales of the wavelet transform permit the equivalent of large- or small-scale transform windows in the same time series. The preceding transform can be approximated for a discrete time series.
A number of wavelet functions are commonly used in data analysis. Here are two used primarily for spectral analysis.
Morlet wavelet (for large ω0):
(13.5)
The Morlet wavelet was originally developed to analyze signals with short, high-frequency transients and long, low-frequency transients (see Figure 13.1).
Mexican hat wavelet:
(13.6)
The Mexican hat wavelet has poorer frequency resolution than the Morlet wavelet, but often better temporal resolution.
The scalogram depicts the strength of a particular wavelet transform coefficient at a point in time. As such, it is the wavelet analog of the spectrogram.
The scalogram in Figure 13.2 shows the continuous wavelet transform of the following signal with a Morlet wavelet (sigma=10). This code generates a time series with three long blocks of time at 100, 500, and 1000 Hz. At every half second, a 0.05 transient at 1000 Hz is inserted.
t = (1/Fs):(1/Fs):(total_time/3);
x = [cos(f(1)*2*pi*t) cos(f(2)*2*pi*t) cos(f(3)*2*pi*t)];
trans = cos(trans_f*2*pi*trans_time);
Be aware that the relationship between scale and frequency is an inverse one and that frequency increases with decreasing scale. Also, note how the frequency resolution improves for the higher frequency band in the later third of the series. This corresponds to the 1000 Hz section of the time series.
The code to generate and plot the CWT follows.
function coefs = simple_cwt(t, x, mother_wavelet, max_wavelet, scales, params)
% Generates coefs for a continuous wavelet transform
% t, x are time and data points for time series data
% mother_wavelet is a function, taking parameters (t, params),
% where the value of params depends on the specific function used
% max_wavelet is the maximum range of the wavelet function (beyond which
% the wavelet is essentially zero)
% scales is a vector of desired scales
% params is the parameter for the mother wavelet function
full_t = −(max_t/2):dt:(max_t/2);
coefs = zeros(length(scales), length(x));
t_scale = linspace(−max_wavelet, max_wavelet, points);
dt = (max_wavelet*2)/(points−1);
mom_wavelet = feval(mother_wavelet, t_scale, params);
time_scale = [1+floor([0:scale*max_wavelet*2]/(scale*dt))];
wavelet = mom_wavelet(time_scale);
w = conv(x,wavelet)/sqrt(scale);
w = w(((−mid_x:mid_x)+mid_w));
Here, imagesc generates an imagemap from two vectors of data. Given parameters x, y, and c, imagesc generates a colored area of color(n,m) centered at x(n) and y(m). So, here in plot_cwt, at values of t and coefs, the corresponding scales value is used to assign a color.
To generate the scalogram, type:
In addition to the continuous wavelet transform, there is a transformation termed the discrete wavelet transform (DWT). However, the DWT is not merely a discretized continuous wavelet transform. Instead, the discrete wavelet transform calculates only a subset of the possible scales, usually dyadic values (successive values in 2n, i.e., 1, 2, 4, 8, 16, 32, etc.). Moreover, the DWT is usually calculated using an algorithm called the pyramid algorithm, in which the data series is recursively split in two and reprocessed.
An exploration of the pyramid algorithm is beyond the scope of this chapter. For a thorough discussion, see Percival and Walden (2000). The DWT has been used to denoise signals and to cluster neural spikes for sorting (Quiroga, Nadasdy, and Ben-Shaul, 2004).
Wavelet Toolbox provides an implementation of the DWT and a number of appropriate wavelets. Analyses using the discrete wavelet transform use different wavelets than analyses with the continuous transform. The Haar wavelet and the Daubechies wavelet are among the most widely used.
In the following commands, ‘wname’ corresponds to the name of a specific wavelet included in Wavelet Toolbox. Possible choices are ‘dbN’ for Daubechies N, ‘haar’ for Haar, ‘morl’ for Morlet, and ‘mexh’ for the Mexican hat. To view all supported wavelets, use help waveform.
The function cwt performs a continuous wavelet transform on the dataset S. The scales given as SCALES are used, and the wavelet is given by ‘wname’. The function cwt will also automatically plot the scalogram if given the parameter ‘plot’ at the end:
The functions dwt and idwt perform a single level decomposition and synthesis given the wavelet name.
The functions wavedec and waverec perform multilevel decomposition and synthesis given wavelet name and level N. Note that N cannot be greater than the exponent of the largest power of 2 less than the size of X. The C vector contains the transform, and the L vector contains bookkeeping information used by wavedec and waverec to find the position of the parts of the transform in C.
Here is an example plotting scales 2 through 7 for a Debauches 4 wavelet:
[C, L] = wavedec(s, 7, ‘db4’);
c_sub = (2^(scale−1)):(2^scale);
The wavedemo function opens an automated tour of Wavelet Toolbox, showing various transforms and functions provided by the toolbox.
In Chapter 12, “Frequency Analysis Part II: Nonstationary Signals and Spectrograms,” you used the short-time Fourier transform to look for sleep state transitions. Here, you will be asked to examine the same data files using the continuous wavelet transform and Morlet and Mexican hat wavelets. Compare and contrast your findings with what you found using only the STFT.