wave let mat lab

8
CHAPTER 9 Wavelets 9.1. GOALS OF THIS CHAPTER 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 Wavelet Toolbox of the MATLABW software. 9.2. BACKGROUND In Chapter 8, "Frequency Analysis Part II: Nonstationary Signals and Spectrograms," you used the short-time Fourier transform (STFT) to decompose the frequency composi- tion 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 preci- sion 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. When using the STFT, you can adjust the transform window to enhance the desired char- acteristic. 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. This can pose a problem for some nonstationary signals. The wavelet transform provides an alternative to the STFT that often provides a better fre- quency/time representation of the signal. 133

Upload: kartikiyer94886

Post on 18-Feb-2016

214 views

Category:

Documents


1 download

DESCRIPTION

Matlab code

TRANSCRIPT

Page 1: Wave Let Mat Lab

C H A P T E R

9

Wavelets

9.1. GOALS OF THIS CHAPTER

In this chapter, you will be introduced to the use of wavelets and wavelet transforms asan alternative method of spectral analysis. We will discuss a number of common waveletsand introduce Wavelet Toolbox of the MATLABW software.

9.2. BACKGROUND

In Chapter 8, "Frequency Analysis Part II: Nonstationary Signals and Spectrograms,"you used the short-time Fourier transform (STFT) to decompose the frequency composi-tion of nonstationary signals. Under certain situations, though, the STFT results in aless-than optimal breakdown of frequency as a function of time. With increased preci-sion 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 temporallocalization reduces the precision of the frequency distribution. This may bring to mindthe well-known relationship of position and momentum of the Heisenberg uncertaintyprinciple.

When using the STFT, you can adjust the transform window to enhance the desired char-acteristic. A larger window allows for better frequency resolution, and a smaller windowallows for better temporal resolution. However, for the STFT, the window size is constantthroughout the algorithm. This can pose a problem for some nonstationary signals. Thewavelet transform provides an alternative to the STFT that often provides a better fre-quency/time representation of the signal.

133

Page 2: Wave Let Mat Lab

134 9. WAVELETS

9.2.1. What Is a Wavelet?

A wavelet is a function that satisfies at least the following two criteria:

1. The integral of the function cðxÞ over all x is 0.

Z1

�1cðxÞdx ¼ 0 ð9:1Þ

2. The square of c(x) has integral 1. A function adhering to this property is called square-integrable.

Z1

�1c2ðxÞdx ¼ 1 ð9:2Þ

Fulfilling the first criterion mandates that the wavelet function have an equal areaabove and below zero. Fulfilling the second criterion mandates that the functionapproach zero at positive and negative infinity. Because of this second criterion, thefunction decays away from the origin, unlike sinusoidal or other infinite waves (thus,wavelet).

9.2.2. The Continuous Wavelet Transform

The continuous wavelet transform (CWT) is analogous to the continuous Fouriertransform:

Wðs; tÞ �Z1

�1xðuÞcs;tðuÞdu ð9:3Þ

Here, the parameter t is the typical t in the time series x(t). The parameter s is called scaleand is analogous to frequency for Fourier transforms. The wavelet function itself varieswith both s and t:

cs;tðxÞ �1ffiffis

p c� x� t

s

� �ð9:4Þ

The inclusion of t and s allows the function to be scaled and translated (shifted) for dif-ferent values of s and t. The original wavelet function (untranslated and unscaled) is oftentermed the mother wavelet, since the set of wavelet functions is generated from that initialfunction.

The scaling provides a significant benefit over the short-time Fourier transform. The mul-tiple scales of the wavelet transform permit the equivalent of large- or small-scale transformwindows in the same time series. The preceding transform can be approximated for a dis-crete time series.

Page 3: Wave Let Mat Lab

1359.2. BACKGROUND

9.2.3. Choosing a Wavelet

A number of wavelet functions are commonly used in data analysis. Here are two usedprimarily for spectral analysis.

Morlet wavelet (for large o0):

cðtÞ ¼ p�14 e�

12 t

2

e�io0t ð9:5ÞThe Morlet wavelet was originally developed to analyze signals with short, high-

frequency transients and long, low-frequency transients (see Figure 9.1).Mexican hat wavelet:

cðtÞ ¼ 1ffiffiffiffiffiffi2p

ps3

1� t2

s2

� �e�t2

2s2 ð9:6Þ

The Mexican hat wavelet has poorer frequency resolution than the Morlet wavelet, butoften better temporal resolution.

−20 −15 −10 −5 0 5 10 15 20

−20 −15 −10 −5 0 5 10 15 20

−20 −15 −10 −5 0 5 10 15 20

−1

−0.5

0

0.5

1Scale = 1

−1

−0.5

0

0.5

1Scale = 5

−1

−0.5

0

0.5

1Scale = 10

FIGURE 9.1 Morlet wavelet at various scales.

Page 4: Wave Let Mat Lab

136 9. WAVELETS

9.2.4. Scalograms

The scalogram depicts the strength of a particular wavelet transform coefficient at a pointin time. As such, it is the wavelet analog of the spectrogram.

The scalogram in Figure 9.2 shows the continuous wavelet transform of the following signalwith a Morlet wavelet (sigma ¼ 10). This code generates a time series with three long blocks oftime at 100, 500, and 1000 Hz. At every half second, a 0.05 transient at 1000 Hz is inserted.

Fs ¼ 5000;total_time ¼ 5;t ¼ (1/Fs):(1/Fs):(total_time/3);f ¼ [100 500 1000];x ¼ [cos(f(1)*2*pi*t) cos(f(2)*2*pi*t) cos(f(3)*2*pi*t)];t ¼ (1/Fs):(1/Fs):total_time;

%add short transientstrans_time ¼ 0:(1/Fs):0.05;trans_f ¼ 1000;for secs ¼ 0.5:0.5:4

trans ¼ cos(trans_f*2*pi*trans_time);x((secs*Fs):(secs*Fsþlength(trans)-1)) ¼ trans;

end

Be aware that the relationship between scale and frequency is an inverse one and thatfrequency increases with decreasing scale. Also, note how the frequency resolution improvesfor the higher frequency band in the later third of the series. This corresponds to the1000 Hz section of the time series.

The code to generate and plot the CWT follows.

0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

20

40

60

80

100

120

140

160

180

200 FIGURE 9.2 Scalogram for sinu-soid þ transient signal in text.

Page 5: Wave Let Mat Lab

1379.2. BACKGROUND

In my_cwt.m:

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

max_t ¼ max(t);dt ¼ t(2)-t(1);full_t ¼ -(max_t/2):dt:(max_t/2);coefs ¼ zeros(length(scales), length(x));points ¼ length(x);t_scale ¼ linspace(-max_wavelet, max_wavelet, points);dt ¼ (max_wavelet*2)/(points-1);

mom_wavelet ¼ feval(mother_wavelet, t_scale, params);row ¼ 1;

for scale ¼ scalestime_scale ¼ [1þfloor([0:scale*max_wavelet*2]/(scale*dt))];wavelet ¼ mom_wavelet(time_scale);w ¼ conv(x,wavelet)/sqrt(scale);mid_w ¼ floor(length(w)/2);mid_x ¼ floor(length(x)/2);w ¼ w(((-mid_x:mid_x)þmid_w));scale % print scale to show progresscoefs(row,:) ¼ abs(w);row ¼ row þ 1;

end

In my_morlet.m:

function m¼morlet(t, params)sigma ¼ params(1);m ¼ pi∧-0.25*exp(-i*sigma.*t-0.5*t.∧2);

In plot_cwt.m:

function plot_cwt(t, coefs, scales)imagesc(t, scales, coefs);colormap(hot);axis xy;

end

Page 6: Wave Let Mat Lab

138 9. WAVELETS

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 inplot_cwt, at values of t and coefs, the corresponding scales value is used to assign a color.

To generate the scalogram, type:

scales ¼ 1:200;coefs ¼ my_cwt(t, x, @my_morlet, 10, scales, [10]);plot_cwt(t, coefs, scales);

9.2.5. The Discrete Wavelet Transform

In addition to the continuous wavelet transform, there is a transformation termed the dis-crete wavelet transform (DWT). However, the DWT is not merely a discretized continuouswavelet transform. Instead, the discrete wavelet transform calculates only a subset of thepossible 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 thor-ough discussion, see Percival and Walden (2000). The DWT has been used to denoise sig-nals and to cluster neural spikes for sorting (Quiroga, Nadasdy, and Ben-Shaul, 2004).

9.2.6. Wavelet Toolbox

Wavelet Toolbox provides an implementation of the DWT and a number of appropriatewavelets. Analyses using the discrete wavelet transform use different wavelets than anal-yses with the continuous transform. The Haar wavelet and the Daubechies wavelet areamong the most widely used.

In the following commands, ‘wname’ corresponds to the name of a specific wavelet includedinWavelet Toolbox. Possible choices are ‘dbN’ for Daubechies N, ‘haar’ for Haar, ‘morl’ forMor-let, and ‘mexh’ for the Mexican hat. To view all supported wavelets, use help waveform.

coefs = cwt(S, SCALES, 'wname')

The function cwt performs a continuous wavelet transform on the dataset S. The scalesgiven as SCALES are used, and the wavelet is given by ‘wname’. The function cwt will alsoautomatically plot the scalogram if given the parameter ‘plot’ at the end:

coefs = cwt(x, 1:200, 'morl', 'plot')

[cA. cD] = dwt(X, 'wname')X = idwt(cA, cD, 'wname')

The functions dwt and idwt perform a single level decomposition and synthesis given thewavelet name.

[C, L] = wavedec(X, N, 'wname')X = waverec(C, L, 'wname')

The functions wavedec and waverec perform multilevel decomposition and synthesis givenwavelet name and level N. Note that N cannot be greater than the exponent of the largestpower of 2 less than the size of X. The C vector contains the transform, and the L vector

Page 7: Wave Let Mat Lab

1399.3. EXERCISES

contains bookkeeping information used by wavedec and waverec to find the position of theparts of the transform in C.

Here is an example plotting scales 2 through 7 for a Debauches 4 wavelet:

% here size(s) = 128[C, L] = wavedec(s, 7, 'db4');for scale = 2:7

subplot(7,1,scale)c_sub = (2∧(scale-1)):(2∧scale);t_sub = linspace(1, time, time/size(c_sub));plot(t_sub, C(c_sub))

end

wavedemo

The wavedemo function opens an automated tour of Wavelet Toolbox, showing varioustransforms and functions provided by the toolbox.

9.3. EXERCISES

Exercise 9.1: Which of the following MATLAB functions can be wavelet functions? Why or

why not?

function x = f_one(t)x = cos(t);

end

function x = f_two(t)if (x < 0 or x > pi/2)

x = 0;else

x = cos(t);end

end

function x = f_three(t)x = sqrt(2) * t * exp(-t^2/2) / pi^4;

end

function x = f_four(t)x = sqrt(2) * t^2 * exp(-t^2/2) / pi^4;

end

function x = f_five(t)x = (x > -1 && x < 0) * -1 + (x > 0 && x < 1);

end

Page 8: Wave Let Mat Lab

140 9. WAVELETS

Exercise 9.2: Generate the scalogram in Figure 9.2. Generate a spectrogram and compare.

How clearly does each render the transients? The primary frequencies?

Exercise 9.3: Write a Mexican hat mother wavelet function compatible with the previous con-

tinuous wavelet transform code. Generate a scalogram of the sinusoidþtransient signal used in

Figure 9.2. Compare Mexican hat transform to the Morlet transform.

Exercise 9.4: Download the EEG signal wavelet, eeg, from the companion website. Generate

scalograms using the Mexican hat and Morlet wavelet transforms. Compare to a spectrogram

generated with spectrogram().

9.4. PROJECT

In Chapter 8, “Frequency Analysis Part II: Nonstationary Signals and Spectrograms,”you used the short-time Fourier transform to look for sleep state transitions. Here, you willbe asked to examine the same data files using the continuous wavelet transform and Morletand Mexican hat wavelets. Compare and contrast your findings with what you found usingonly the STFT.

MATLAB FUNCTIONS, COMMANDS, AND OPERATORSCOVERED IN THIS CHAPTER

cwtdwtidwtwavedecwaverec