• No se han encontrado resultados

Chapter 5: Simulations

3. AUTODESK CFD for Surface Micro-condensation

FIR filters have transfer functions that have only numerator coefficients, i.e., H(z)= B(z). This leads to an impulse response that is finite, hence the name.

They have the advantage of always being stable and having linear phase shifts.

In addition, they have initial transients that are of finite durations and their extension to 2-dimensional applications is straightforward. The downside of FIR filters is that they are less efficient in terms of computer time and memory than IIR filters. FIR filters are also referred to as nonrecursive because only the input (not the output) is used in the filter algorithm (i.e., only the first term of Eq. (6) is used).

A simple FIR filter was developed in the context of Problem 3 inChapter 2.This filter was achieved taking three consecutive points in the input array and averaging them together. The filter output was constructed by moving this three-point average along the input waveform. For this reason, FIR filtering has also been referred to as a moving average process. (This term is used for any process that uses a moving set of multiplier weights, even if the operation does not really produce an average.) In Problem 4 of Chapter 2, this filter was imple-mented using a three weight filter, [1/3 1/3 1/3], which was convolved with the input waveform to produce the filtered output. These three numbers are simply the b(n) coefficients of a third-order, or three-weight, FIR filter. All FIR filters are similar to this filter; the only difference between them is the number and value of the coefficients.

The general equation for an FIR filter is a simplification of Eq. (6) and, after changing the limits to conform with MATLAB notation, becomes:

y(k)=

L

n=1

b(n) x(k− n) (8)

where b(n) is the coefficient function (also referred to as the weighting function) of length L, x(n) is the input, and y(n) is the output. This is identical to the convolution equation in Chapter 2 (Eq. (15)) with the impulse response, h(n),

replaced by the filter coefficients, b(n). Hence, FIR filters can be implemented using either convolution or MATLAB’sfilter routine. Eq. (8) indicates that the filter coefficients (or weights) of an FIR filter are the same as the impulse response of the filter. Since the frequency response of a process having an im-pulse response h(n) is simply the Fourier transform of h(n), the frequency re-sponse of an FIR filter having coefficients b(n) is just the Fourier transform of b(n):

X(m)=

N−1

n=0

b(n) e(−j2π mn/N)

(9) Eq. (9) is a special case of Eq. (5) when the denominator equals one. If b(n) generally consists of a small number of elements, this equation can some-times be determined manually as well as by computer.

The inverse operation, going from a desired frequency response to the coefficient function, b(n), is known as filter design. Since the frequency re-sponse is the Fourier transform of the filter coefficients, the coefficients can be found from the inverse Fourier transform of the desired frequency response.

This design strategy is illustrated below in the design of a FIR lowpass filter based on the spectrum of an ideal filter. This filter is referred to as a rectangular window filter* since its spectrum is ideally a rectangular window.

FIR Filter Design

The ideal lowpass filter was first introduced inChapter 1 as a rectangular win-dow in the frequency domain (Figure 1.7). The inverse Fourier transform of a rectangular window function is given in Eq. (25) inChapter 2and repeated here with a minor variable change:

b(n)=sin[2πfcTs(n− L/2)]

π(n − L/2) (10)

where fcis the cutoff frequency; Ts is the sample interval in seconds; and L is the length of the filter. The argument, n − L/2, is used to make the coefficient function symmetrical giving the filter linear phase characteristics. Linear phase characteristics are a desirable feature not easily attainable with IIR filters. The coefficient function, b(n), produced by Eq. (10), is shown for two values of fc

inFigure 4.4.Again, this function is the same as the impulse response.

Unfortu-*This filter is sometimes called a window filter, but the term rectangular window filter will be used in this text so as not to confuse the filter with a window function as described in the last chapter.

This can be particularly confusing since, as we show later, rectangular window filters also use window functions!

FIGURE4.4 Symmetrical weighting function of a rectangular filter (Eq. (10) trun-cated at 64 coefficients. The cutoff frequencies are given relative to the sampling frequency, fs, as is often done in discussing digital filter frequencies. Left: Low-pass filter with a cutoff frequency of 0.1fs/2 Hz. Right: Lowpass cutoff frequency of 0.4fs/2 Hz.

nately this coefficient function must be infinitely long to produce the filter char-acteristics of an ideal filter; truncating it will result in a lowpass filter that is less than ideal.Figure 4.5shows the frequency response, obtained by taking the Fourier transform of the coefficients for two different lengths. This filter also shows a couple of artifacts associated with finite length: an oscillation in the frequency curve which increases in frequency when the coefficient function is longer, and a peak in the passband which becomes narrower and higher when the coefficient function is lengthened.

Since the artifacts seen in Figure 4.5 are due to truncation of an (ideally) infinite function, we might expect that some of the window functions described in Chapter 3 would help. In discussing window frequency characteristics in Chapter 3, we noted that it is desirable to have a narrow mainlobe and rapidly diminishing sidelobes, and that the various window functions were designed to make different compromises between these two features. When applied to an FIR weight function, the width of the mainlobe will influence the sharpness of the transition band, and the sidelobe energy will influence the oscillations seen

FIGURE4.5 Freuquency characteristics of an FIR filter based in a weighting func-tion derived from Eq. (10). The weighting funcfunc-tions were abruptly truncated at 17 and 65 coefficients. The artifacts associated with this truncation are clearly seen.

The lowpass cutoff frequency is 100 Hz.

in Figure 4.5. Figure 4.6shows the frequency characteristics that are produced by the same coefficient function used in Figure 4.4 except that a Hamming window has been applied to the filter weights. The artifacts are considerably diminished by the Hamming window: the overshoot in the passband has disap-peared and the oscillations are barely visible in the plot. As with the unwin-dowed filter, there is a significant improvement in the sharpness of the transition band for the filter when more coefficients are used.

The FIR filter coefficients for highpass, bandpass, and bandstop filters can be derived in the same manner from equations generated by applying an inverse FT to rectangular structures having the appropriate associated shape. These equations have the same general form as Eq. (10) except they include additional terms:

b(n)=sin[π(n − L/2)]

π(n − L/2) −sin[2πfcTs(n− L/2)]

π(n − L/2) Highpass (11)

b(n)=sin[2πfHT(n− L/2)]

π(n − L/2) −sin[2πfLTs(n− L/2)]

π(n − L/2) Bandpass (12)

FIGURE4.6 Frequency characteristics produced by an FIR filter identical to the one used inFigure 4.5except a Hamming function has been applied to the filter coefficients. (See Example 1 for the MATLAB code.)

b(n)=sin[2πfLT(n− L/2)]

π(n − L/2) +sin[π(n − L/2)]

π(n − L/2)

−sin[2πfHTs(n− L/2)]

π(n − L/2) Bandstop (13)

An FIR bandpass filter designed using Eq. (12) is shown inFigure 4.7for two different truncation lengths. Implementation of other FIR filter types is a part of the problem set at the end of this chapter. A variety of FIR filters exist that use strategies other than the rectangular window to construct the filter coef-ficients, and some of these are explored in the section on MATLAB implemen-tation. One FIR filter of particular interest is the filter used to construct the derivative of a waveform since the derivative is often of interest in the analysis of biosignals. The next section explores a popular filter for this operation.

Derivative Operation: The Two-Point Central Difference Algorithm

The derivative is a common operation in signal processing and is particularly useful in analyzing certain physiological signals. Digital differentiation is

de-FIGURE4.7 Frequency characteristics of an FIR Bandpass filter with a coefficient function described by Eq. (12) in conjuction with the Blackman window function.

The low and high cutoff frequencies were 50 and 150 Hz. The filter function was truncated at 33 and 129 coefficients. These figures were generated with code similar to that in Example 4.2 below, except modified according to Eq. (12)

fined as ∆x/∆t and can be implemented by taking the difference between two adjacent points, scaling by 1/ Ts, and repeating this operation along the entire waveform. In the context of the FIR filters described above, this is equiva-lent to a two coefficient filter, [−1, +1]/Ts, and this is the approach taken by MATLAB’sdervroutine. The frequency characteristic of the derivative opera-tion is a linear increase with frequency, Figure 4.8 (dashed line) so there is considerable gain at the higher frequencies. Since the higher frequencies fre-quently contain a greater percentage of noise, this operation tends to produce a noisy derivative curve.Figure 4.9Ashows a noisy physiological motor response (vergence eye movements) and the derivative obtained using thedervfunction.

Figure 4.9B shows the same response and derivative when the derivative was calculated using the two-point central difference algorithm. This algorithm acts

FIGURE4.8 The frequency response of the two-point central difference algorithm using two different values for the skip factor: (A) L= 1; (B) L = 4. The sample time was 1 msec.

as a differentiator for the lower frequencies and as an integrator (or lowpass filter) for higher frequencies.

The two-point central difference algorithm uses two coefficients of equal but opposite value spaced L points apart, as defined by the input–output equa-tion:

y(n)=x(n+ L) − x(n − L) 2LTs

(14) where L is the skip factor that influences the effective bandwidth as described below, and Ts is the sample interval. The filter coefficients for the two-point central difference algorithm would be:

FIGURE4.9 A physiological motor response to a step input is shown in the upper trace and its derivative is shown in the lower trace. (A) The derivative was calcu-lated by taking the difference in adjacent points and scaling by the sample fre-quency. (B) The derivative was computed using the two-point central difference algorithm with a skip factor of 4. Derivative functions were scaled by 1/2and re-sponses were offset to improve viewing.

h(n)=−0.5/L n = −L 0.5/L n= +L

0 n≠ L

(15)

The frequency response of this filter algorithm can be determined by tak-ing the Fourier transform of the filter coefficient function. Since this function contains only two coefficients, the Fourier transform can be done either

analyti-cally or using MATLAB’sfftroutine. Both methods are presented in the exam-ple below.

Example 4.2 Determine the frequency response of the two-point central difference algorithm.

Analytical: Since the coefficient function is nonzero only for n= ± L, the Fou-rier transform, after adjusting the summation limits for a symmetrical coefficient function with positive and negative n, becomes:

X(k)=n=−L

L b(n)e(−j2π kn/N)=2Lt1 se(−j2π kL/N)2LT1 se(−j2πk(−L)/N) where L is the skip factor and N is the number of samples in the waveform.

To put Eq. (16) in terms of frequency, note that f= m/(NTs); hence, m= fNTs. Substituting:

*X(f)* =

jsin(2πfLTLTs s)

=sin(2πfLTLTs s) (17)

Eq. (17) shows that *X(k)* is a sine function that goes to zero at f = 1/(LTs) or fs/L. Figure 4.8shows the frequency characteristics of the two-point central difference algorithm for two different skip factors, and the MATLAB code used to calculate and plot the frequency plot is shown in Example 4.2. A true derivative would have a linear change with frequency (specifically, a line with a slope of 2πf ) as shown by the dashed lines in Figure 4.8. The two-point central difference curves approximate a true derivative for the lower frequencies, but has the characteristic of a lowpass filter for higher frequencies. Increasing the skip factor, L, has the effect of lowering the frequency range over which the filter acts like a derivative operator as well as the lowpass filter range. Note that for skip factors >1, the response curve repeats above f = 1/(LTs). Usually the assumption is made that the signal does not contain frequencies in this range. If this is not true, then these higher frequencies could be altered by the frequency characteristics of this filter above 1/(LTs).

MATLAB Implementation

Since the FIR coefficient function is the same as the impulse response of the filter process, design and application of these filters can be achieved using only FFT and convolution. However, the MATLAB Signal Processing Toolbox has a number of useful FIR filter design routines that greatly facilitate the design of FIR filters, particularly if the desired frequency response is complicated. The

following two examples show the application use of FIR filters using only con-volution and the FFT, followed by a discussion and examples of FIR filter design using MATLAB’s Signal Processing Toolbox.

Example 4.2 Generate the coefficient function for the two-point central difference derivative algorithm and plot the frequency response. This program was used to generate the plots inFigure 4.8.

% Example 4.2 and Figure 4.8

% Program to determine the frequency response

% of the two point central difference algorithm for

% differentiation

bn = zeros((2*L)ⴙ1,1); %Set up b(n). Initialize to

% zero

bn(1,1) = -1/(2*L*Ts); % Put negative coefficient at

% b(1)

bn((2*L)ⴙ1,1) = 1/(2*L*Ts); % Put positive coefficient at

% b(2Lⴙ1)

H = abs(fft(bn,N)); % Cal. frequency response

% using FFT

Note that the second to fourth lines of the forloop are used to build the filter coefficients, b(n), for the given skip factor, L. The next line takes the absolute value of the Fourier transform of this function. The coefficient function is zero-padded out to 1000 points, both to improve the appearance of the

result-ing frequency response curve and to simulate the application of the filter to a 1000 point data array sampled at 1 kHz.

Example 4.3 Develop and apply an FIR bandpass filter to physiological data. This example presents the construction and application of a narrowband filter such as shown in Figure 4.10 (right side) using convolution. The data are from a segment of an EEG signal in the PhysioNet data bank (http://www.

physionet.org).A spectrum analysis is first performed on both the data and the filter to show the range of the filter’s operation with respect to the frequency spectrum of the data. The standard FFT is used to analyze the data without windowing or averaging. As shown in Figure 4.10, the bandpass filter transmits most of the signal’s energy, attenuating only a portion of the low frequency and

FIGURE4.10 Frequency spectrum of EEG data shown in Figure 4.11obtained using the FFT. Also shown is the frequency response of an FIR bandpass filter constructed using Eq. (12). The MATLAB code that generated this figure is pre-sented in Example 4.3.

high frequency components. The result of applying this filter to the EEG signal is shown in Figure 4.11.

% Example 4.3 andFigures 4.10and 4.11

% Application of an FIR bandpass filter based

% on a rectangular window design as shown inFigure 4.7

%

close all; clear all;

N = 1000; % Number of data points

fs = 100; % Sample frequency

load sig_2; % Get data

FIGURE4.11 A segment of unfiltered EEG data (upper trace) and the bandpass filtered version (lower trace). A frequency response of the FIR bandpass filter is given in Figure 4.10.

wh = .3 * pi; % Set bandpass cutoff

% frequencies wl = .1*pi;

L = 128; % Number of coeffients

% equals 128

for i = 1:Lⴙ1 % Generate bandpass

% coefficient function

bn = bn .* blackman(Lⴙ1)’; % Apply Blackman window

% to filter coeffs.

H_data = abs(fft(data)); % Plot data spectrum for

% comparison

freq = (1:N/2)*fs/N; % Frequency vector for

% plotting

plot(freq,H_data(1:N/2),’k’); % Plot data FFT only to

% fs/2 hold on;

%

H = abs(fft(bn,N)); % Find the filter

% frequency response H = H*1.2 * (max(H_data)/max(H)); % Scale filter H(z) for

% comparison plot(freq,H(1:N/2),’--k’); % Plot the filter

% frequency response xlabel(’Frequency (Hz)’); ylabel(’H(f)’);

y = conv(data,bn); % Filter the data using

% convolution figure;

t = (1:N)/fs; % Time vector for

% plotting subplot(2,1,1);

plot(t(1:N/2),data(1:N/2),’k’) % Plot only 1/2 of the

% data set for clarity

In this example, the initial loop constructs the filter weights based on Eq.

(12). The filter has high and low cutoff frequencies of 0.1π and 0.3 π radians/

sample, or 0.1fs/2 and 0.3fs/2 Hz. Assuming a sampling frequency of 100 Hz this would correspond to cutoff frequencies of 5 to 15 Hz. The FFT is also used to evaluate the filter’s frequency response. In this case the coefficient function is zero-padded to 1000 points both to improve the appearance of the frequency response curve and to match the data length. A frequency vector is constructed to plot the correct frequency range based on a sampling frequency of 100 Hz.

The bandpass filter is applied to the data using convolution. Two adjustments must be made when using convolution to implement an FIR filter. If the filter weighting function is asymmetrical, as with the two-point central difference algorithm, then the filter order should be reversed to compensate for the way in which convolution applies the weights. In all applications, the MATLAB convolution routine generates additional points (N= length(data) + length(b(n) − 1) so the output must be shortened to N points. Here the initial N points are taken, but other strategies are mentioned inChapter 2.In this example, only the first half of the data set is plotted inFigure 4.11to improve clarity.

Comparing the unfiltered and filtered data in Figure 4.11, note the sub-stantial differences in appearance despite the fact that only a small potion of the signal’s spectrum is attenuated. Particularly apparent is the enhancement of the oscillatory component due to the suppression of the lower frequencies. This figure shows that even a moderate amount of filtering can significantly alter the appearance of the data. Also note the 50 msec initial transient and subsequent phase shift in the filtered data. This could be corrected by shifting the filtered data the appropriate number of sample points to the left.

Documento similar