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

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

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

where the new function, Ax(θ,τ), is termed the ambiguity function. In this case, the convolution operation in Eq. (13) becomes multiplication, and the desired distribution is just the double Fourier transform of the product of the ambiguity function times the instantaneous autocorrelation function:

ρ(t,f) = FFTt{FFTf [Ax(θ,τ)Rx(t,τ)]}

(15)

One popular distribution is the Choi-Williams, which is also referred to as an exponential distribution (ED) since it has an exponential-type kernel. Specifically, the kernel and determining function of the Choi-Williams distribution are:

g(v,τ) = ev2τ 2

(16)

After integrating the equation above as in Eq. (11), G(t,τ) becomes:

G(t,τ) =

 

e−σ t2/4τ 2

 

σ/π

(17)

 

The Choi-Williams distribution can also be used in a modified form that incorporates a window function and in this form is considered one of a class of reduced interference distributions (RID) (Williams, 1992). In addition to having reduced cross products, the Choi-Williams distribution also has better noise characteristics than the Wigner-Ville. These two distributions will be compared with other popular distributions in the section on implementation.

Analytic Signal

All of the transformations in Cohen’s class of distributions produce better results when applied to a modified version of the waveform termed the Analytic signal, a complex version of the real signal. While the real signal can be used, the analytic signal has several advantages. The most important advantage is due to the fact that the analytic signal does not contain negative frequencies, so its use will reduce the number of cross products. If the real signal is used, then both the positive and negative spectral terms produce cross products. Another benefit is that if the analytic signal is used the sampling rate can be reduced. This is because the instantaneous autocorrelation function is calculated using evenly spaced values, so it is, in fact, undersampled by a factor of 2 (compare the discrete and continuous versions of Eq. (9)). Thus, if the analytic function is not used, the data must be sampled at twice the normal minimum; i.e., twice the Nyquist frequency or four times fMAX.* Finally, if the instantaneous frequency

*If the waveform has already been sampled, the number of data points should be doubled with intervening points added using interpolation.

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

is desired, it can be determined from the first moment (i.e., mean) of the distribution only if the analytic signal is used.

Several approaches can be used to construct the analytic signal. Essentially one takes the real signal and adds an imaginary component. One method for establishing the imaginary component is to argue that the negative frequencies that are generated from the Fourier transform are not physical and, hence, should be eliminated. (Negative frequencies are equivalent to the redundant frequencies above fs/2. Following this logic, the Fourier transform of the real signal is taken, the negative frequencies are set to zero, or equivalently, the redundant frequencies above fs/2, and the (now complex) signal is reconstructed using the inverse Fourier transform. This approach also multiplies the positive frequencies, those below fs/2, by 2 to keep the overall energy the same. This results in a new signal that has a real part identical to the real signal and an imaginary part that is the Hilbert Transform of the real signal (Cohen, 1989). This is the approach used by the MATLAB routine hilbert and the routine hilber on the disk, and the approach used in the examples below.

Another method is to perform the Hilbert transform directly using the Hilbert transform filter to produce the complex component:

z(n) = x(n) + j H[x(n)]

(18)

where H denotes the Hilbert transform, which can be implemented as an FIR filter (Chapter 4) with coefficients of:

h(n) =

 

2 sin2n/2)

for n ≠ 0

(19)

 

πn

 

 

 

 

0

for n = 0

 

Although the Hilbert transform filter should have an infinite impulse response length (i.e., an infinite number of coefficients), in practice an FIR filter length of approximately 79 samples has been shown to provide an adequate approximation (Bobashash and Black, 1987).

MATLAB IMPLEMENTATION

The Short-Term Fourier Transform

The implementation of the time–frequency algorithms described above is straightforward and is illustrated in the examples below. The spectrogram can be generated using the standard fft function described in Chapter 3, or using a special function of the Signal Processing Toolbox, specgram. The arguments for specgram (given on the next page) are similar to those use for pwelch described in Chapter 3, although the order is different.

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

[B,f,t] = specgram(x,nfft,fs,window,noverlap)

where the output, B, is a complex matrix containing the magnitude and phase of the STFT time–frequency spectrum with the rows encoding the time axis and the columns representing the frequency axis. The optional output arguments, f and t, are time and frequency vectors that can be helpful in plotting. The input arguments include the data vector, x, and the size of the Fourier transform window, nfft. Three optional input arguments include the sampling frequency, fs, used to calculate the plotting vectors, the window function desired, and the number of overlapping points between the windows. The window function is specified as in pwelch: if a scalar is given, then a Hanning window of that length is used.

The output of all MATLAB-based time–frequency methods is a function of two variables, time and frequency, and requires either a three-dimensional plot or a two-dimensional contour plot. Both plotting approaches are available through MATLAB standard graphics and are illustrated in the example below.

Example 6.1 Construct a time series consisting of two sequential sinusoids of 10 and 40 Hz, each active for 0.5 sec (see Figure 6.2). The sinusoids should be preceded and followed by 0.5 sec of no signal (i.e., zeros). Determine the magnitude of the STFT and plot as both a three-dimensional grid plot and as a contour plot. Do not use the Signal Processing Toolbox routine, but develop code for the STFT. Use a Hanning window to isolate data segments.

Example 6.1 uses a function similar to MATLAB’s specgram, except that the window is fixed (Hanning) and all of the input arguments must be specified. This function, spectog, has arguments similar to those in specgram. The code for this routine is given below the main program.

FIGURE 6.2 Waveform used in Example 6.1 consisting of two sequential sinusoids of 10 and 40 Hz. Only a portion of the 0.5 sec endpoints are shown.

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

%Example 6.1 and Figures 6.2, 6.3, and 6.4

%Example of the use of the spectrogram

%Uses function spectog given below

%

 

 

 

clear all; close all;

 

% Set up constants

 

fs =

500;

% Sample frequency in Hz

N =

 

1024;

% Signal length

f1

=

10;

% First frequency in Hz

f2

=

40;

% Second frequency in Hz

nfft = 64;

% Window size

noverlap = 32;

% Number of overlapping points (50%)

%

% Construct a step change in frequency

tn =

(1:N/4)/fs;

% Time vector used to create sinusoids

x

=

[zeros(N/4,1); sin(2*pi*f1*tn)’; sin(2*pi*f2*tn)’...

 

zeros(N/4,1)];

 

t

=

(1:N)/fs;

% Time vector used to plot

plot(t,x,’k’);

....labels....

%Could use the routine specgram from the MATLAB Signal Processing

%Toolbox: [B,f,t] = specgram(x,nfft,fs,window,noverlap),

%but in this example, use the “spectog” function shown below.

FIGURE 6.3 Contour plot of the STFT of two sequential sinusoids. Note the broad time and frequency range produced by this time–frequency approach. The appearance of energy at times and frequencies where no energy exists in the original signal is evident.

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

FIGURE 6.4 Time–frequency magnitude plot of the waveform in Figure 6.3 using the three-dimensional grid technique.

%

[B,f,t] = spectog(x,nfft,fs,noverlap);

B = abs(B);

% Get spectrum magnitude

figure;

 

mesh(t,f,B);

% Plot Spectrogram as 3-D mesh

view(160,40);

% Change 3-D plot view

axis([0 2 0 100 0 20]);

% Example of axis and

xlabel(’Time (sec)’);

% labels for 3-D plots

ylabel(’Frequency (Hz)’);

figure

 

contour(t,f,B);

% Plot spectrogram as contour plot

....labels and axis....

The function spectog is coded as:

function [sp,f,t] = spectog(x,nfft,fs,noverlap);

% Function to calculate spectrogram

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

%Output arguments

%sp spectrogram

%t time vector for plotting

%f frequency vector for plotting

%Input arguments

%x data

%nfft window size

%fs sample frequency

%noverlap number of overlapping points in adjacent segments

%Uses Hanning window

%

 

 

 

[N xcol] = size(x);

 

if N < xcol

 

x =

x’;

% Insure that the input is a row

N =

xcol;

% vector (if not already)

end

 

 

 

incr =

nfft—noverlap;

% Calculate window increment

hwin =

fix(nfft/2);

% Half window size

f = (1:hwin)*(fs/nfft);

% Calculate frequency vector

% Zero pad data array to handle edge effects

x_mod =

[zeros(hwin,1); x; zeros(hwin,1)];

%

 

 

 

j = 1;

 

% Used to index time vector

%Calculate spectra for each window position

%Apply Hanning window

for i = 1:incr:N

data = x_mod(i:i nfft-1) .* hanning(nfft);

ft = abs(fft(data));

% Magnitude data

sp(:,j) = ft(1:hwin);

% Limit spectrum to meaningful

 

% points

t(j) = i/fs;

% Calculate time vector

j = j 1;

% Increment index

end

 

Figures 6.3 and 6.4 show that the STFT produces a time–frequency plot with the step change in frequency at approximately the correct time, although neither the step change nor the frequencies are very precisely defined. The lack of finite support in either time or frequency is evidenced by the appearance of energy slightly before 0.5 sec and slightly after 1.5 sec, and energies at frequencies other than 10 and 40 Hz. In this example, the time resolution is better than the frequency resolution. By changing the time window, the compromise between time and frequency resolution could be altered. Exploration of this tradeoff is given as a problem at the end of this chapter.

A popular signal used to explore the behavior of time–frequency methods is a sinusoid that increases in frequency over time. This signal is called a chirp

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

signal because of the sound it makes if treated as an audio signal. A sample of such a signal is shown in Figure 6.5. This signal can be generated by multiplying the argument of a sine function by a linearly increasing term, as shown in Example 6.2 below. Alternatively, the Signal Processing Toolbox contains a special function to generate a chip that provides some extra features such as logarithmic or quadratic changes in frequency. The MATLAB chirp routine is used in a latter example. The output of the STFT to a chirp signal is demonstrated in Figure 6.6.

Example 6.2 Generate a linearly increasing sine wave that varies between 10 and 200 Hz over a 1sec period. Analyze this chirp signal using the STFT program used in Example 6.1. Plot the resulting spectrogram as both a 3- D grid and as a contour plot. Assume a sample frequency of 500 Hz.

%Example 6.2 and Figure 6.6

%Example to generate a sine wave with a linear change in frequency

%Evaluate the time–frequency characteristic using the STFT

%Sine wave should vary between 10 and 200 Hz over a 1.0 sec period

%Assume a sample rate of 500 Hz

%

 

clear all; close all;

 

% Constants

 

N = 512;

% Number of points

FIGURE 6.5 Segment of a chirp signal, a signal that contains a single sinusoid that changes frequency over time. In this case, signal frequency increases linearly with time.

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

% Plot 3-D and contour % side-by-side
% 3-D plot
% Contour plot

FIGURE 6.6 The STFT of a chirp signal, a signal linearly increasing in frequency from 10 to 200 Hz, shown as both a 3-D grid and a contour plot.

fs =

500;

% Sample freq;

f1

=

10;

% Minimum frequency

f2

=

200;

% Maximum frequency

nfft = 32;

% Window size

t =

 

(1:N)/fs;

% Generate a time

 

 

 

% vector for chirp

% Generate chirp signal (use a linear change in freq)

fc = ((1:N)*((f2-f1)/N)) f1;

 

x =

 

sin(pi*t.*fc);

 

%

% Compute spectrogram using the Hanning window and 50% overlap [B,f,t] = spectog(x,nfft,fs,nfft/2); % Code shown above

%

subplot(1,2,1);

mesh(t,f,abs(B));

....labels, axis, and title....

subplot(1,2,2);

contour(t,f,abs(B));

....labels, axis, and title....

The Wigner-Ville Distribution

The Wigner-Ville distribution will provide a much more definitive picture of the time–frequency characteristics, but will also produce cross products: time–

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

frequency energy that is not in the original signal, although it does fall within the time and frequency boundaries of the signal. Example 6.3 demonstrates these properties on a signal that changes frequency abruptly, the same signal used in Example 6.1with the STFT. This will allow a direct comparison of the two methods.

Example 6.3 Apply the Wigner-Ville distribution to the signal of Example 6.1. Use the analytic signal and provide plots similar to those of Example 6.1.

%Example 6.3 and Figures 6.7 and 6.8

%Example of the use of the Wigner-Ville distribution

%Applies the Wigner-Ville to data similar to that of Example

%6.1, except that the data has been shortened from 1024 to 512

%to improve run time.

%

clear all; close all;

% Set up constants (same as Example 6–1)

fs =

500;

% Sample frequency

N =

 

512;

% Signal length

f1

=

10;

% First frequency in Hz

f2

=

40;

% Second frequency in Hz

FIGURE 6.7 Wigner-Ville distribution for the two sequential sinusoids shown in Figure 6.3. Note that while both the frequency ranges are better defined than in Figure 6.2 produced by the STFT, there are large cross products generated in the region between the two actual signals (central peak). In addition, the distributions are sloped inward along the time axis so that onset time is not as precisely defined as the frequency range.

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

FIGURE 6.8 Contour plot of the Wigner-Ville distribution of two sequential sinusoids. The large cross products are clearly seen in the region between the actual signal energy. Again, the slope of the distributions in the time domain make it difficult to identify onset times.

%

% Construct a step change in frequency as in Ex. 6–1

tn =

(1:N/4)/fs;

 

x =

[zeros(N/4,1); sin(2*pi*f1*tn)’; sin(2*pi*f2*tn)’;

zeros(N/4,1)];

 

%

 

 

% Wigner-Ville analysis

 

x =

hilbert(x);

% Construct analytic function

[WD,f,t] = wvd(x,fs);

% Wigner-Ville transformation

WD =

abs(WD);

% Take magnitude

mesh(t,f,WD);

% Plot distribution

view(100,40);

% Use different view

....Labels and axis....

 

figure

 

contour(t,f,WD);

% Plot as contour plot

....Labels and axis....

 

The function wwd computes the Wigner-Ville distribution.

function [WD,f,t] = wvd(x,fs)

%Function to compute Wigner-Ville time–frequency distribution

%Outputs

%WD Wigner-Ville distribution

%f Frequency vector for plotting

%t Time vector for plotting

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