Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Biosignal and Biomedical Image Processing MATLAB based Applications - John L. Semmlow

.pdf
Скачиваний:
429
Добавлен:
10.08.2013
Размер:
4.43 Mб
Скачать

clear all; close all;

 

 

N =

1000;

% Data length

fs =

1000;

% Assumed sample frequency

wo1 = pi * sqrt(2/log2(2));

% Const. for wavelet time

 

 

 

%

scale

a =

[.5 1.0 2.0 3.0];

% Values of a

xi =

((1:N/2)/fs)*10;

% Show ± 10 sec of the wavelet

t =

(1:N)/fs;

% Time scale

omaga = (1:N/2) * fs/N;

% Frequency scale

%

 

 

 

 

for i =

1:length(a)

 

 

t1 =

xi./a(i);

% Make time vector for

 

 

 

%

wavelet

mex =

exp(-t1.v2).* (1–2*t1.v2);%

Generate Mexican hat

 

 

 

%

wavelet

w = [fliplr(mex) mex];

% Make symmetrical about zero

wsq = abs(w).v2

% Square wavelet;

W = fft(w);

% Get frequency representa-

Wsq =

abs(W(1:N/2)).v2;

% tion and square. Use only

 

 

 

%

fs/2 range

t0 =

sum(t.* wsq)/sum(wsq);

% Calculate center time

d_t =

sqrt(sum((t—to).v2 .*wsq)/sum(wsq));

 

 

 

% Calculate time spread

w0 =

sum(omaga.*Wsq)/sum(Wsq);

% Calculate center frequency

d_w0 = sqrt(sum((omaga—w0).v2 .* Wsq)/sum(Wsq));

t1 =

t0*a(i);

% Adjust time position to

 

 

 

% compensate for symmetri-

 

 

 

%

cal waveform

hold on;

 

 

%Plot boundaries

plot([t1-d_t t1-d_t],[w0-d_w0 w0 d_w0],’k’); plot([t1 d_t t1 d_t],[w0-d_w0 w0 d_w0],’k’);

plot([t1-d_t t1 d_t],[w0-d_w0 w0-d_w0],’k’); plot([t1-d_t t1 d_t],[w0 d_w0 w0 d_w0],’k’);

end

% ***** lables*****

THE DISCRETE WAVELET TRANSFORM

The CWT has one serious problem: it is highly redundant.* The CWT provides an oversampling of the original waveform: many more coefficients are generated than are actually needed to uniquely specify the signal. This redundancy is

*In its continuous form, it is actually infinitely redundant!

Copyright 2004 by Marcel Dekker, Inc. All Rights Reserved.

usually not a problem in analysis applications such as described above, but will be costly if the application calls for recovery of the original signal. For recovery, all of the coefficients will be required and the computational effort could be excessive. In applications that require bilateral transformations, we would prefer a transform that produces the minimum number of coefficients required to recover accurately the original signal. The discrete wavelet transform (DWT) achieves this parsimony by restricting the variation in translation and scale, usually to powers of 2. When the scale is changed in powers of 2, the discrete wavelet transform is sometimes termed the dyadic wavelet transform which, unfortunately, carries the same abbreviation (DWT). The DWT may still require redundancy to produce a bilateral transform unless the wavelet is carefully chosen such that it leads to an orthogonal family (i.e., a orthogonal basis). In this case, the DWT will produce a nonredundant, bilateral transform.

The basic analytical expressions for the DWT will be presented here; however, the transform is easier to understand, and easier to implement using filter banks, as described in the next section. The theoretical link between filter banks and the equations will be presented just before the MATLAB Implementation section. The DWT is often introduced in terms of its recovery transform:

∞ ∞

 

x(t) = ∑ ∑d(k,R)2k/2ψ(2kt − R)

(13)

k= −∞ R= −∞

Here k is related to a as: a = 2k; b is related to R as b = 2k R; and d(k,R) is a sampling of W(a,b) at discrete points k and R.

In the DWT, a new concept is introduced termed the scaling function, a function that facilitates computation of the DWT. To implement the DWT efficiently, the finest resolution is computed first. The computation then proceeds to coarser resolutions, but rather than start over on the original waveform, the computation uses a smoothed version of the fine resolution waveform. This smoothed version is obtained with the help of the scaling function. In fact, the scaling function is sometimes referred to as the smoothing function. The definition of the scaling function uses a dilation or a two-scale difference equation:

 

φ(t) = 2c(n)φ(2t n)

(14)

n= −∞

where c(n) is a series of scalars that defines the specific scaling function. This equation involves two time scales (t and 2t) and can be quite difficult to solve.

In the DWT, the wavelet itself can be defined from the scaling function:

 

ψ(t) = 2d(n)φ(2t n)

(15)

n= −∞

Copyright 2004 by Marcel Dekker, Inc. All Rights Reserved.

where d(n) is a series of scalars that are related to the waveform x(t) (Eq. (13)) and that define the discrete wavelet in terms of the scaling function. While the DWT can be implemented using the above equations, it is usually implemented using filter bank techniques.

Filter Banks

For most signal and image processing applications, DWT-based analysis is best described in terms of filter banks. The use of a group of filters to divide up a signal into various spectral components is termed subband coding. The most basic implementation of the DWT uses only two filters as in the filter bank shown in Figure 7.4.

The waveform under analysis is divided into two components, ylp(n) and yhp(n), by the digital filters H0(ω) and H1(ω). The spectral characteristics of the two filters must be carefully chosen with H0(ω) having a lowpass spectral characteristic and H1(ω) a highpass spectral characteristic. The highpass filter is analogous to the application of the wavelet to the original signal, while the lowpass filter is analogous to the application of the scaling or smoothing function. If the filters are invertible filters, then it is possible, at least in theory, to construct complementary filters (filters that have a spectrum the inverse of H0(ω) or H1(ω)) that will recover the original waveform from either of the subband signals, ylp(n) or yhp(n). The original signal can often be recovered even if the filters are not invertible, but both subband signals will need to be used. Signal recovery is illustrated in Figure 7.5 where a second pair of filters, G0(ω) and G1(ω), operate on the high and lowpass subband signals and their sum is used

FIGURE 7.4 Simple filter bank consisting of only two filters applied to the same waveform. The filters have lowpass and highpass spectral characteristics. Filter outputs consist of a lowpass subband, ylp(n), and a highpass subband, yhp(n).

Copyright 2004 by Marcel Dekker, Inc. All Rights Reserved.

FIGURE 7.5 A typical wavelet application using filter banks containing only two filters. The input waveform is first decomposed into subbands using the analysis filter bank. Some process is applied to the filtered signals before reconstruction. Reconstruction is performed by the synthesis filter bank.

to reconstruct a close approximation of the original signal, x’(t). The Filter Bank that decomposes the original signal is usually termed the analysis filters while the filter bank that reconstructs the signal is termed the syntheses filters. FIR filters are used throughout because they are inherently stable and easier to implement.

Filtering the original signal, x(n), only to recover it with inverse filters would be a pointless operation, although this process may have some instructive value as shown in Example 7.3. In some analysis applications only the subband signals are of interest and reconstruction is not needed, but in many wavelet applications, some operation is performed on the subband signals, ylp(n) and yhp(n), before reconstruction of the output signal (see Figure 7.5). In such cases, the output will no longer be exactly the same as the input. If the output is essentially the same, as occurs in some data compression applications, the process is termed lossless, otherwise it is a lossy operation.

There is one major concern with the general approach schematized in Figure 7.5: it requires the generation of, and operation on, twice as many points as are in the original waveform x(n). This problem will only get worse if more filters are added to the filter bank. Clearly there must be redundant information contained in signals ylp(n) and yhp(n), since they are both required to represent x(n), but with twice the number of points. If the analysis filters are correctly chosen, then it is possible to reduce the length of ylp(n) and yhp(n) by one half and still be able to recover the original waveform. To reduce the signal samples by one half and still represent the same overall time period, we need to eliminate every other point, say every odd point. This operation is known as downsampling and is illustrated schematically by the symbol ↓ 2. The downsampled version of y(n) would then include only the samples with even indices [y(2), y(4), y(6), . . . ] of the filtered signal.

Copyright 2004 by Marcel Dekker, Inc. All Rights Reserved.

If downsampling is used, then there must be some method for recovering the missing data samples (those with odd indices) in order to reconstruct the original signal. An operation termed upsampling (indicated by the symbol ↑ 2) accomplishes this operation by replacing the missing points with zeros. The recovered signal (x’(n) in Figure 7.5) will not contain zeros for these data samples as the synthesis filters, G0(ω) or G1(ω), ‘fill in the blanks.’ Figure 7.6 shows a wavelet application that uses three filter banks and includes the downsampling and upsampling operations. Downsampled amplitudes are sometimes scaled by 2, a normalization that can simplify the filter calculations when matrix methods are used.

Designing the filters in a wavelet filter bank can be quite challenging because the filters must meet a number of criteria. A prime concern is the ability to recover the original signal after passing through the analysis and synthesis filter banks. Accurate recovery is complicated by the downsampling process. Note that downsampling, removing every other point, is equivalent to sampling the original signal at half the sampling frequency. For some signals, this would lead to aliasing, since the highest frequency component in the signal may no

FIGURE 7.6 A typical wavelet application using three filters. The downsampling ( ↓ 2) and upsampling ( ↑ 2) processes are shown. As in Figure 7.5, some process would be applied to the filtered signals, ylp(n) and yhp(n), before reconstruction.

Copyright 2004 by Marcel Dekker, Inc. All Rights Reserved.

longer be twice the now reduced sampling frequency. Appropriately chosen filter banks can essentially cancel potential aliasing. If the filter bank contains only two filter types (highpass and lowpass filters) as in Figure 7.5, the criterion for aliasing cancellation is (Strang and Nguyen, 1997):

G0(z)H0(−z) + G1(z)H1(−z) = 0

(16)

where H0(z) is the transfer function of the analysis lowpass filter, H1(z) is the transfer function of the analysis highpass filter, G0(z) is the transfer function of the synthesis lowpass filter, and G1(z) is the transfer function of the synthesis highpass filter.

The requirement to be able to recover the original waveform from the subband waveforms places another important requirement on the filters which is satisfied when:

G0(z)H0(z) + G1(z)H1(z) = 2zN

(17)

where the transfer functions are the same as those in Eq. (16). N is the number of filter coefficients (i.e., the filter order); hence z-N is just the delay of the filter.

In many analyses, it is desirable to have subband signals that are orthogonal, placing added constraints on the filters. Fortunately, a number of filters have been developed that have most of the desirable properties.* The examples below use filters developed by Daubechies, and named after her. This is a family of popular wavelet filters having 4 or more coefficients. The coefficients of the lowpass filter, h0(n), for the 4-coefficient Daubechies filter are given as:

h(n) =

[(1 +

3), (3 +

 

 

 

 

 

 

 

3), (3 −

3), (1 −

3)]

(18)

 

 

 

 

 

 

 

 

8

 

 

 

 

 

Other, higher order filters in this family are given in the MATLAB routine daub found in the routines associated with this chapter. It can be shown that orthogonal filters with more than two coefficients must have asymmetrical coefficients.† Unfortunately this precludes these filters from having linear phase characteristics; however, this is a compromise that is usually acceptable. More complicated biorthogonal filters (Strang and Nguyen, 1997) are required to produce minimum phase and orthogonality.

In order for the highpass filter output to be orthogonal to that of the lowpass output, the highpass filter frequency characteristics must have a specific relationship to those of the lowpass filter:

*Although no filter yet exists that has all of the desirable properties.

†The two-coefficient, orthogonal filter is: h(n) = [12; 12], and is known as the Haar filter. Essentially a two-point moving average, this filter does not have very strong filter characteristics. See Problem 3.

Copyright 2004 by Marcel Dekker, Inc. All Rights Reserved.

H1(z) = −zNH0(−z−1)

(19)

The criterion represented by Eq. (19) can be implemented by applying the alternating flip algorithm to the coefficients of h0(n):

h1(n) = [h0(N), −h0(N − 1), h0(N − 2), −h0(N − 3), . . .]

(20)

where N is the number of coefficients in h0(n). Implementation of this alternating flip algorithm is found in the analyze program of Example 7.3.

Once the analyze filters have been chosen, the synthesis filters used for reconstruction are fairly constrained by Eqs. (14) and (15). The conditions of Eq. (17) can be met by making G0(z) = H1(- z) and G1(z) = -H0(-z). Hence the synthesis filter transfer functions are related to the analysis transfer functions by the Eqs. (21) and (22):

G0(z) = H1(z) = zNH0(z−1)

(21)

G1(z) = −H0(−z) = zNH1(z−1)

(22)

where the second equality comes from the relationship expressed in Eq. (19). The operations of Eqs. (21) and (22) can be implemented several different ways, but the easiest way in MATLAB is to use the second equalities in Eqs. (21) and (22), which can be implemented using the order flip algorithm:

g0(n) = [h0(N), h0(N − 1),

h0(N − 2),

. . .]

(23)

g1(n) = [h1(N), h1(N − 1),

h1(N − 2),

. . .]

(24)

where, again, N is the number of filter coefficients. (It is assumed that all filters have the same order; i.e., they have the same number of coefficients.)

An example of constructing the syntheses filter coefficients from only the analysis filter lowpass filter coefficients, h0(n), is shown in Example 7.3. First the alternating flip algorithm is used to get the highpass analysis filter coefficients, h1(n), then the order flip algorithm is applied as in Eqs. (23) and (24) to produce both the synthesis filter coefficients, g0(n) and g1(n).

Note that if the filters shown in Figure 7.6 are causal, each would produce a delay that is dependent on the number of filter coefficients. Such delays are expected and natural, and may have to be taken into account in the reconstruction process. However, when the data are stored in the computer it is possible to implement FIR filters without a delay. An example of the use of periodic convolution to eliminate the delay is shown in Example 7.4 (see also Chapter 2).

The Relationship Between Analytical Expressions

and Filter Banks

The filter bank approach and the discrete wavelet transform represented by Eqs. (14) and (15) were actually developed separately, but have become linked both

Copyright 2004 by Marcel Dekker, Inc. All Rights Reserved.

theoretically and practically. It is possible, at least in theory, to go between the two approaches to develop the wavelet and scaling function from the filter coefficients and vice versa. In fact, the coefficients c(n) and d(n) in Eqs. (14) and (15) are simply scaled versions of the filter coefficients:

c(n) =

 

h0(n); d(n) =

 

h1(n)

 

2

2

(25)

With the substitution of c(n) in Eq. (14), the equation for the scaling

function (the dilation equation) becomes:

 

 

φ(t) = 2 h0(n)φ(2t n)

(26)

n= − ∞

Since this is an equation with two time scales (t and 2t), it is not easy to solve, but a number of approximation approaches have been worked out (Strang and Nguyen, 1997, pp. 186–204). A number of techniques exist for solving for φ(t) in Eq. (26) given the filter coefficients, h1(n). Perhaps the most straightforward method of solving for φ in Eq. (26) is to use the frequency domain representation. Taking the Fourier transform of both sides of Eq. (26) gives:

Φ(ω) = H0 ω2 Φ ω2

(27)

Note that 2t goes to ω/2 in the frequency domain. The second term in Eq. (27) can be broken down into H0(ω/4) Φ(ω/4), so it is possible to rewrite the equation as shown below.

Φ(ω) = H0 ω2 H0 ω4 Φ ω4

(28)

= H0 ω2 H0 ω4 H0 ω8 . . . H0 2ωN Φ 2ωN

(29)

In the limit as N → ∞ , Eq. (29) becomes:

 

ω

 

Φ(ω) = JH0

(30)

j=1

2j

 

The relationship between φ(t) and the lowpass filter coefficients can now be obtained by taking the inverse Fourier transform of Eq. (30). Once the scaling function is determined, the wavelet function can be obtained directly from Eq. (16) with 2h1(n) substituted for d(n):

 

ψ(t) = 2 h1(n)φ(2t n)

(31)

n= −∞

Copyright 2004 by Marcel Dekker, Inc. All Rights Reserved.

Eq. (30) also demonstrates another constraint on the lowpass filter coefficients, h0(n), not mentioned above. In order for the infinite product to converge (or any infinite product for that matter), H0(ω/2j) must approach 1 as j → ∞ . This implies that H0(0) = 1, a criterion that is easy to meet with a lowpass filter. While Eq. (31) provides an explicit formula for determining the scaling function from the filter coefficients, an analytical solution is challenging except for very simple filters such as the two-coefficient Haar filter. Solving this equation numerically also has problems due to the short data length (H0(ω) would be only 4 points for a 4-element filter). Nonetheless, the equation provides a theoretical link between the filter bank and DWT methodologies.

These issues described above, along with some applications of wavelet analysis, are presented in the next section on implementation.

MATLAB Implementation

The construction of a filter bank in MATLAB can be achieved using either routines from the Signal Processing Toolbox, filter or filtfilt, or simply convolution. All examples below use convolution. Convolution does not conserve the length of the original waveform: the MATLAB conv produces an output with a length equal to the data length plus the filter length minus one. Thus with a 4-element filter the output of the convolution process would be 3 samples longer than the input. In this example, the extra points are removed by simple truncation. In Example 7.4, circular or periodic convolution is used to eliminate phase shift. Removal of the extraneous points is followed by downsampling, although these two operations could be done in a single step, as shown in Example 7.4.

The main program shown below makes use of 3 important subfunctions. The routine daub is available on the disk and supplies the coefficients of a Daubechies filter using a simple list of coefficients. In this example, a 6-element filter is used, but the routine can also generate coefficients of 4-, 8-, and 10element Daubechies filters.

The waveform is made up of 4 sine waves of different frequencies with added noise. This waveform is decomposed into 4 subbands using the routine analysis. The subband signals are plotted and then used to reconstruct the original signal in the routine synthesize. Since no operation is performed on the subband signals, the reconstructed signal should match the original except for a phase shift.

Example 7.3 Construct an analysis filter bank containing L decompositions; that is, a lowpass filter and L highpass filters. Decompose a signal consisting of 4 sinusoids in noise and the recover this signal using an L-level syntheses filter bank.

Copyright 2004 by Marcel Dekker, Inc. All Rights Reserved.

% Sample frequency
% Number of points in % waveform
% Sinusoid frequencies % for mix
% Amplitude of sinusoid % Get filter coeffi-
% cients: Daubechies 6

%Example 7.3 and Figures 7.7 and 7.8

%Dyadic wavelet transform example

%Construct a waveform of 4 sinusoids plus noise

%Decompose the waveform in 4 levels, plot each level, then

%reconstruct

%Use a Daubechies 6-element filter

%

clear all; close all;

%

fs = 1000; N = 1024;

freqsin = [.63 1.1 2.7 5.6];

ampl = [1.2 1 1.2 .75 ]; h0 = daub(6);

FIGURE 7.7 Input (middle) waveform to the four-level analysis and synthesis filter banks used in Example 7.3. The lower waveform is the reconstructed output from the synthesis filters. Note the phase shift due to the causal filters. The upper waveform is the original signal before the noise was added.

Copyright 2004 by Marcel Dekker, Inc. All Rights Reserved.