Beruflich Dokumente
Kultur Dokumente
Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University October 7, 2009
Digital Filters
Any medium through which a signal passes may be regarded as a lter. Typically however, a lter is viewed as something which modies the signal in some way. Examples include: stereo speakers our vocal tract our musical instruments A digital lter is a formula for going from one digital signal to another.
x(n)
1111111111 0000000000 1111111111 0000000000 0000000000 1111111111 1111111111 0000000000 0000000000 1111111111 0000000000 1111111111 0000000000 111111111111111 00000 11111 00000 0000000000 1111111111 1111111111 0000000000 0000000000 1111111111 0000000000 1111111111 0000000000 1111111111 0000000000 1111111111 0000000000 1111111111
Figure 1: A black box lter.
y(n)
A lter is linear if it satises the following two properties: 1. Scaling: the amplitude of the output is proportional to the amplitude of the input (i.e., scaling can be done either at the input or the output) L{gx()} = g L{x()}
2. Superposition: the output due to a sum of input signals is equal to the sum of outputs due to each signal alone. L{x1() + x2()} = L{x1()} + L{x2()}
A lter is time-invariant if its behaviour is not dependent on time: if the input signal is delayed by N samples, the output waveform is simply delayed by N samples: L{x( N )} = LnN {x()} = y (n N )
Liner Time-Invariant (LTI) lters modify the amplitude and/or phase of the spectral components of an input signal. LTI lters are guaranteed to produce a sinusoid in response to a sinusoid, without altering the frequency. They may only attenuate, reject, amplify (boost) or leave unchanged, any existing frequency components of the input signal.
The characteristic of a lter is described by its frequency response which consists of both a magnitude and a phase response: 1. magnitude response Describes the gain of a lter at every frequency: a positive gain boosts the signal while a negative gain attenuates the signal. Determined by the ratio of the peak output amplitude to the peak input amplitude at a given frequency. 2. phase response: Determined by subtracting the input phase from the output phase at a particular frequency. A frequency response describes how the amplitude and phase of a sounds spectral components are changed by the lter.
HighPass
fc
frequency
fc
frequency
Bandpass
magnitude (dB) magnitude (dB) 0 3 0 3
Bandreject (notch)
fc BW
frequency
fc frequency BW
The cuto frequency is typically dened as the frequency at which the power transmitted by the lter drops to one-half (by -3 dB) of the maximum power transmitted in the passband.
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 7
Quality Factor
A pass-band lter is characterized by its quality factor Q which is inversely proportional to its bandwidth fc Q= BW where fc is the center frequency of the band. A high quality factor (a high Q) denotes a lter with a narrow bandwidth and a low Q denotes a lter with a wide bandwidth.
Delay Review
Recall that whenever a signal can be expressed in the form x(t) = s(t t1), we say that x(t) is a time-shifted version of s(t).
If t1 is a positive number, then the shift is to the right and we say that the signal has been delayed in time. If t1 is a negative number, x(t) eectively becomes x(t) = s(t + t1), the shift is to the left and the signal has been advanced in time.
Recall that a lowpass lter is one that allows low frequencies to pass, while attenuating anything above a specied cuto frequency fc. The amplitude response of the ideal low-pass lter is given in the gure below:
1 Gain 0 0 Frequency fc fs/2
Figure 3: Amplitude Response for an ideal low-pass lter.
Unfortunately such an ideal amplitude response is not realizable in discrete time. How close we come depends on the complexity of the lter.
10
The simplest low-pass lter (and therefore furthest from ideal) is given by the dierence equation y (n) = x(n) + x(n 1). The corresponding system diagram is given below:
x(n) z 1 y(n)
Figure 4: System diagram for the simple low-pass lter y (n) = x(n) + x(n 1).
This lter is really just a running averager (2-point averager) with a factor of two. That is, it takes the average of two adjacent samples. Why is it a Low-Pass Filter?
11
The running average of an input signal with little or no variation from sample to sample is very close to the input signal. Consider an input to this lter at the lowest possible frequency 0 Hz (DC), where the signal has a single value over time: x1(n) = [A, A, A, ...]. The output of the lowpass lter is y (n) = x1(n) + x1(n 1) = 2A.
12
The running average of an input signal with signicant variation from sample to sample will be very dierent than the input signal. Consider a second input x2(n) at the highest possible frequency fs/2 (the Nyquist limit), which swings more signicantly from sample to sample: x2(n) = [A, A, A, ...]. The output of the lowpass lter is y (n) = x2(n) + x2(n 1) = A A = 0.
13
This lter seems to boost low frequencies while rejecting components at higher frequencies. We may nd the frequency response of the lter by checking the behaviour of the lter at every possible frequency between 0 and fs/2 Hz, a method called sinewave analysis. Alternatively, we can use an input signal that contains all of those frequencies, and then we only have to do the checking operation once. If we use an input signal with the broadest possible spectrum, i.e. an impulse, we will obtain an output from the lter, called an impulse response, which is a time domain description of the lters response to all frequencies from DC to the Nyquist limit. The spectrum of the impulse response gives us the the frequency response of the lter.
14
subplot(211); plot(fn, Y); grid; title(Amplitude Response y(n) = x(n) + x(n-1)); xlabel(Frequency (normalized)); ylabel(Magnitude (linear));
Amplitude Response y(n) = x(n) + x(n1) 2
Magnitude (linear)
1.5
0.5
0.05
0.1
0.15
0.35
0.4
0.45
0.5
15
The output of the lter in response to x(n) = ej (2f nT ) is given by y (n ) = = = = = x(n) + x(n 1) ejnT + ej(n1)T ejnT + ejnT ejT (1 + ejT )ejnT (1 + ejT )x(n) H (ejT )x(n)
where H (ejT ) = (1 + ejT ), a complex multiply, is the frequency response of the lter.
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 16
Frequency Response
The lters frequency response is acting as a complex multiply, which means a gain scaling and phase shift on the input signal. The complex lter gain in polar form is given by H (ejT ) = G( )ej () and ( ) where the amplitude and phase response are given by G ( ) |H (ejT )| H (ejT ).
To solve, we may balance the exponents to obtain H (ejT ) = (1 + ejT ) = (ejT /2 + ejT /2)ejT /2 = 2 cos(T /2)ejT /2, G( ) = 2 cos(T /2)ejT /2 = 2 cos(f T ), and the phase response is given by f T = , |f | fs/2. ( ) = 2 fs
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 17
18
Amplitude Response for y(n) = x(n)+x(n1) 2 Magnitude (linear) 1.5 1 0.5 0 Magnitude (linear) 0 0.1 0.2 0.3 0.4 Frequency (normalized)
Amplitude Response for y(n) = x(n)+x(n2) 2 Magnitude (linear) 1.5 1 0.5 0 Magnitude (linear) 0 0.1 0.2 0.3 0.4 Frequency (normalized)
19
Generalized FIR
Several dierent nonrecursive lters can be made by changing the delay (and thus order) and the coecients of the lter terms. The general equation for an FIR (Finite Impulse Response) lter is given by
M
y (n ) =
k =0
b k x (n k )
where M is the order of the lter. A lter can be dened simply by a set of coecients. For example if bk = [1, 3, 3, 1] the lter is third order, (i.e. M = 3), and can be expanded into the dierence equation y (n) = x(n) + 3x(n 1) + 3x(n 2) + x(n 3); When the input to the FIR lter is a unit impulse sequence, (n) = 1 if n = 1 and (n) = 0 otherwise, the output is the unit impulse response.
20
The Matlab implementation for the lter is most easily accomplished using the filter function y = filter(B, A, x). The filter function takes three (3) arguments: 1. feedforward coecients B, 2. feedback coecients A, 3. and the input signal x.
If the lter doesnt have feedback coecients, as is the case with an FIR lter, A = 1. Our simple low-pass lter, y (n) = x(n) + x(n 1), is a rst order lter with 2 feedforward coecients (B = [1, 1]). In matlab: B = [1 1]; A = 1; y = filter(B, A, x);
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 21
0.05
0.1
0.15
0.35
0.4
0.45
0.5
22
h(n) =
k =0
b k (n k ) = b n ;
23
Cascade Connection
An order can be increased by using a lter cascade.
Each time the previous output y is input to the lter, a new impulse response representing is created.
x(n) = {1, 0, 0, ...} y(n) = x(n)+x(n1) y(n) = {1,1,0,0...}
The new output always has a nite number of non-zero components and a nite impulse response. For each lter in the cascade, the impulse response increases by one.
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 24
x = [1 zeros(1, N-1)];
Cascade...
B = [1 1]; A = [1]; y1 = filter(B, y2 = filter(B, y3 = filter(B, y4 = filter(B, A, A, A, A, x); y1); y2); y3);
Or Simply...
B = [1 5 10 10 5 1]; A = [1]; y = filter(B, A, x);
25
Recursive Filters
Using FIR lters often required signicant computation and coecients to reproduce a desired frequency response. It is often possible to reduce the number of feedforward coecients needed to obtain a frequency response by introducing feedback coecients. A simple example of a rst order recursive low-pass lter is given by y (n) = b0x(n) + a1y (n 1) The general dierence equation for LTI lter therefore, is given by y (n) = b0x(n) + b1x(n 1) + + bM x(n m) a1y (n 1) aN y (n N )
26
Amplitude Response y(n) = x(n) + y(n1) 50 40 Magnitude (linear) 30 20 10 0 10 0 0.05 0.1 0.15 0.2 0.25 0.3 Frequency (normalized) 0.35 0.4 0.45 0.5
Figure 9: Simple Non-recursive low-pass y (n) = x(n)+ x(n 1) (top) and Recursive low-pass y (n) = x(n) + y (n 1) (bottom).
27
Z Transforms
The bilateral z transform of the discrete-time signal x(n) is given by X (z )
n=
x(n)z n.
Since most signals well be using are causal, we will typically see the unilateral z transform given by X (z )
n=0
x(n)z n.
The z transform is a generalization of the DTFT (the DFT in the limit as the number of its samples approaches innity). The carrier term is a generalized sampled complex sinusoid z n = e(+j)t with an exponential envelope rather than a constant modulus. The DTFT equals the z transform evaluated on the unit circle in the z plane. The z transform of a signal x is given by Z{x()} = X (z ) also notated Z{x(n)} = X (z ).
28
Z Transform Properties
Two (2) important properties of z transforms: 1. The z transform Z{} is a linear operator which means, by denition Z{x1(n) + x2(n)} = Z{x1(n)} + Z{x2(n)} X1(z ) + X2(z ). 2. From the shift theorem for z transforms, the z transform of a signal delayed by M samples is given by Z{x(n M )} = z M X (z ). Shift Theorem: A delay of M samples in the time domain corresponds to a multiplication by z M in the frequency domain.
29
Grouping the Y (z ) terms together on the left hand side and factoring out common terms X (z ) and Y (z ) yields
Y (z )[1 + a1z 1 + + aN z N ] = X (z )[b0 + b1z 1 + + bM z M ]
30
Transfer Function
Beginning with the equation from the previous slide:
Y (z )[1 + a1z 1 + + aN z N ] = X (z )[b0 + b1z 1 + + bM z M ]
We may dene the following polynomials: A(z ) B (z ) 1 + a1z 1 + + zN z N b0 + b1z 1 + + bM z M A(z )Y (z ) = B (z )X (z ) In solving for Y (z )/X (z ) we obtain the transfer function H (z ) of a digital LTI lter H (z ) = Y (z ) , X (z )
where X (z ) and Y (z ) are the z transforms of the input and output signal, respectively, and H (z ) is the z transform of the impulse response describing the lter.
31
Recall the transfer function for the recursive LTI digital lter 1 + 1z 1 + + M z M H (z ) = g 1 + a1z 1 + + zN z N We can factor the numerator and denominator to obtain (1 q1z 1)(1 q2z 1) (1 qM z 1) H (z ) = g (1 p1z 1)(1 p2z 1) (1 pN z 1) Every polynomial can be characterized by its roots plus a scale factor. We may therefore characterize a transfer function, by its 1. Numerator roots, called the zeros of the lter 2. Denominator roots, called poles of the lter 3. Constant gain factor
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 33
The factored transfer function is given by (1 q1z 1)(1 q2z 1) (1 qM z 1) H (z ) = g (1 p1z 1)(1 p2z 1) (1 pN z 1) Zeros The roots of the numerator polynomial are given by {q1, q2, . . . , qM }. When z takes on any of these values, the transfer function evaluates to zero and thus they are called the zeros of the lter. Poles The roots of the denominator polynomial are given by {p1, p2, . . . , pM } When z approaches any of these values, the transfer function becomes larger and larger, approaching innity. Thus these are called the poles of the lter.
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 34
Since z is complex, it lies in the z plane. Since the magnitude H (z ) is real, it can be represented a distance above the z plane. The plot appears as an innitely thin surface spanning in all directions over the z plane. The zeros are the points where the surface dips down to touch the z plane. The poles look just like poles that rise forever from the z plane, getting thinner the higher they go.
Notice M + 1 feedforward coecients gives rise to M zeros while N feedback coecients gives rise to N poles. The lter order is given by the maximum of the numerator and denominator polynomial orders.
35
(1 q1ejT )(1 q2ejT ) (1 qM ejT ) )=g (1 p1ejT )(1 p2ejT ) (1 pN ejT ) |H (ejT )|
|1 q1ejT | |1 q2ejT | |1 qM ejT | G ( ) = | g | |1 p1ejT | |1 p2ejT | |1 pN ejT | |ejMT | |ejT q1| |ejT qM | = |g | jN T |e | |ejT p1| |ejT pM | |ejT q1| |ejT q2| |ejT qM | = |g | jT . jT jT |e p 1 | |e p2| |e pM | The amplitude response is the product of the dierence between two complex numbers.
36
In the complex plane, the number z = x + jy is plotted at the coordinates (x, y ). The dierence of two vectors u = x1 + jy1 and v = x2 + jy2 is given by u v = (x1 x2) + j (y1 y2).
imaginary
uv u real
37
p1 d2 q2
d3
1 0 0 1
d1 q1 d4 real
p2
Figure 11: Measurement of amplitude response from a pole-zero diagram (a bi-quad section).
Thus the term ejT qi may be drawn as an arrow from the ith zero to the point ejT on the unit circle, and ejT pi is an arrow from the ith pole. The amplitude response at frequency is given by d1 d2 G ( ) = d3 d4
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 38
The term biquad is short for bi-quadratic, and is a common name for a two-pole, two-zero digital lter. The transfer function of the biquad can be dened as 1 + 1z 1 + 2z 2 H (z ) = g 1 + a1z 1 + a2z 2 When the coecients a1 and a2 are real (as we typically assume), the poles must be either 1. real (when (a1/2)2 a2) 2. form a complex conjugate pair (when (a1/2)2 < a2). When the poles form a complex pair, we may express them in polar form as p1 = Rejc p2 = Rejc R is the pole radius, or distance from the origin in the z -plane.
39
Stability
A lter is said to be stable if its impulse response h(n) decays to 0 as n goes to innity. Recall, the transfer function is the z transform of the impulse response. H (z ) Z{h(n)}
Consider a causal impulse response of the form h(n) = Rn ejnT , n = 0, 1, 2, . . . which is a damped complex sinusoid when 0 < R < 1 and is exponentially increasing when R > 1.
1 = 1 RejT z 1 where the last step is a closed form representation of a geometric series and holds for |RejT z 1| < 1, which is true whenever R < |z |.
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 40
Stability cont.
The transfer function H (z ) =
1 1 RejT z 1
If R > 1 the pole of H (z ) moves outside the unit circle, and the impulse response h(n) has an exponentially increasing amplitude (|h(n)| = Rn ). That is, it is unstable. A pole lying on the unit circle is considered marginally stable, that is, it neither decays nor grows in amplitude. In physically modelling synthesis, marginally stable poles often occur in lossless systems such as ideal vibrating strings. Therefore, to verify stability, we may nd the roots of the denominator polynomial and ensure their roots are less than 1. For the stability of any nite order LTI lter, all its poles must lie strictly inside the unit circle.
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 41
The two control parameters for this lter are 1. the center frequency fc 2. the quality factor Q (or alternatively, the bandwidth).
T = 1/44100; Q = 20; fc = 400; Bw = fc/Q; R = exp(-pi*Bw*T); B = [1 0 -R]; A = [1 -2*R*cos(2*pi*fc*T) R*R]; [h, w] = freqz(B,A); N = length(h); plot([0:N-1]/T/N/1000, ... max(20*log10(abs(h)/max(abs(h))), -40)); grid;
These parameters determine the characteristic of the resonator, and can be changed in real-time, making this an ecient and ideal implementation for performance situations.
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 43
FIR/Convolution
Since the feedforward coecients of the FIR lter are the same as the non-zero elements of the impulse response, a general expression for the FIR lters output can also be given by y (n ) =
N 1 k =0
h(k )x(n k ),
where h() is the impulse responses and replaces the coecients bk . When the relation between the input and the output of the FIR lter is expressed in terms of the input and impulse response, we say the the output is obtained by convolving the sequences x(n) and h(n). Note that this is circular or cyclic convolution. To simulate acyclic convolution (as we do when simulating sampled continuous-time systems), we need to zero-pad suciently (N + M 1) so that non-zero samples do not wrap around as a resulting of shifting x.
CMPT 889: Computational Modelling for Sound Synthesis: Lecture 5 44
h(k )x(n k ).
0 1 1 1
3 0 1 0 0 3 1 1 4 6 4
1 1 3 1 3
2 0 3 0 3 3
4 0 0 0 0 0 1 1
5 0 0 0 0 0 0 0
45
Convolution
The operation of convolution performed on two signals in the time domain is equivalent to the multiplication of their spectrum in the frequency domain. That is, the convolution of two time domain signals x(n) and w (n), with Fourier transforms X ( ) and W ( ) respectively, is given by Y ( ) = X ( )W ( ) where y (n), the result of the convolution, is obtained through the inverse Fourier Transform of Y ( ).
46
sign( )( 1) ,
g = 1.
47
To form the coecients of a prototype shelf lter with a transition frequency of /2, is set to the following: = (g + 1)/(g 1), The parameter = sin(ft/2 /4)/ sin(ft/2 + /4) is the coecient of the rst-order allpass transformation warping the prototype lter to the proper transition frequency. g = 1.
48