# 雙二階濾波器原理

 雙二階濾波器說明 　　雙二階濾波器(biquad)是最常用的濾波器之一。雙二階濾波器是雙二階（兩個極點和兩個零點）的IIR濾波器。考慮到更高階數的濾波器對系數敏感，雙二階可以單獨使用，或者在更復雜的濾波器中作為基本的構建單元。例如，一個雙二階低通濾波器有12dB/octave斜度的截止頻點，可以用于音調控制；如果需要一個24dB/octave的斜度，可以級聯兩個雙二階濾波器，這個結構比一個單4階濾波器對參數更不敏感。 　　雙二階濾波器有幾種不同的結構。最直觀也最直接的實現是用一個二階差分方程（ (y[n] = a0*x[n] + a1*x[n-1] + a2*x[n-2] – b1*y[n-1] – b2*y[n-2])），稱之為Direct Form I: Direct Form I 　　在定點（沒有浮點數）處理器中，Direct Form I是最好實現的，因為只有一個加法點（定點DSP一般都有擴展的累加器，可以在運算中有一定的溢出）。 　　我們可以把Direct Form I在加法那個點分成兩部分，如下圖： 然后，我們把分開的兩部分交換順序，讓反饋的部分（極點）先計算： 可以看到，上圖中成對的Z延時是有冗余的，剔除冗余后得到Direct Form II: Direct Form II 　　Direct Form II更適合浮點處理器，節省了存儲空間，并且浮點處理器不會有溢出問題（不像定點處理器有溢出問題）。我們可以通過轉置來進一步改進Direct Form II. 把信號的流向前后顛倒，輸出變成輸入，分支節點變成加法，加法變成分支節點。通過轉置，濾波器的特征不變，并且，轉置后更有利于用浮點來計算。當相加的兩個數比較臨近時，浮點數會有更高的精度（把很小的數加到一個很大的數上，精度會比兩個相近的數相加有更低的精度）。下圖是轉置后的Direct Form II( Transposed Direct Form II): Transposed direct form II   補充說明和一些建議 　　總結，direct form I更適合定點計算，轉置(transposed) Direct form II更適合浮點計算。 　　量化誤差，在低頻情況下，容易導致雙二階濾波器更脆弱（不穩定），主要是因為反饋系數(b1和b2)以及中間值(the delay memory)。精度不夠，會導致不容易精確控制極點的位置，尤其在極點靠近單位圓的情況下，因此，量化誤差會變成一個重要問題。第二個問題，是關于中間值的，相乘之后會產生更多的位，當保存下來時，需要做截斷處理。這個誤差反饋回濾波器系統，可能導致系統不穩定。32位浮點數對于音頻濾波器來講一般是足夠好了。但是，對于很低頻率（如控制類信號的濾波）和高采樣率情況下，可能需要雙精度才行。 　　對于定點實現的濾波器，24位的系數和中間存儲，基本上可以很好的滿足需要了，但是48k采樣率下從300Hz往下（或者96k采樣率下從600Hz往下），就開始變得不穩定了。定點處理器上實現雙精度開銷很大，幸好，有一個簡單的方法可以提高穩定度。看看Direct Form I的結構圖，把高精度的累加結果保存在低精度的中間存儲器中，這個過程會造成精度損失。把精度損失（量化損失：高精度計算結果與低精度存儲之間的差值）加回到下一次輸出結果的計算中，近似達到雙精度，當然，會增加一些運算量。這個技巧被稱為First Order Noise Shaping. 有更高階的Noise Shapers，但是First Order已經足夠好了，可以應付所有音頻需要，即使在高采樣率的情況下。 Direct Form I with first-order noise shaping 　　沒有雙精度的系數和計算，僅僅用16位定點，通常情況下，是不適合用于處理音頻的。 　　最后，雙二階濾波器僅僅是DSP工具之一，它也不全是最好的濾波器。還有其它的濾波器沒有低頻敏感問題的（通常，雙二階濾波器對于高頻是有比較好精度的，對于低頻則不太好；有其它類型的濾波器，精度的分布更均勻，或者可以犧牲部分高頻的精度來換取低頻的精度）。然而，雙二階濾波器很通用和普遍，設計工具也很充足，所以，雙二階濾波器會是首選，除非有明確的原因需要用其它類型的濾波器。 　　有太多濾波器類型了，另外一個對synthesizer來說很經常使用的是state variable filter。對于低頻有很好的性能，對于高頻有一些需要繞過的限制，但是，最重要的是頻率和Q系數是分開的，且很容易改變。它也構成了低頻sine波產生器。   三種濾波器類型的計算過程（系數如何使用） 　　歸一化公式如下： 　　H(z)=b0+b1z−1+b2z−21+a1z−1+a2z−2H(z)=b0+b1z−1+b2z−21+a1z−1+a2z−2 　　系數為 b0b0, b1b1and b2b2和 a1a1, a2a2   Direct Form I 的計算過程 　　y[n]=b0x[n]+b1x[n−1]+b2x[n−2]−a1y[n−1]−a2y[n−2]y[n]=b0x[n]+b1x[n−1]+b2x[n−2]−a1y[n−1]−a2y[n−2]   Direct Form II 的計算過程 　　y[n]=b0w[n]+b1w[n−1]+b2w[n−2]y[n]=b0w[n]+b1w[n−1]+b2w[n−2] 　　where 　　w[n]=x[n]−a1w[n−1]−a2w[n−2]w[n]=x[n]−a1w[n−1]−a2w[n−2]   Transposed direct forms 　　y[n]=b0x[n]+s1[n−1]y[n]=b0x[n]+s1[n−1] 　　where 　　s1[n]=s2[n−1]+b1x[n]−a1y[n]s1[n]=s2[n−1]+b1x[n]−a1y[n] 　　and 　　s2[n]=b2x[n]−a2y[n]s2[n]=b2x[n]−a2y[n]   附錄：英文原文   第一個鏈接來源的英文原文 　　One of the most-used filter forms is the biquad. A biquad is a second order (two poles and two zeros) IIR filter. It is high enough order to be useful on its own, and—because of coefficient sensitivities in higher order filters—the biquad is often used as the basic building block for more complex filters. For instance, a biquad lowpass filter has a cutoff slope of 12 dB/octave, useful for tone controls; if you need a 24 dB/octave slope, you can cascade two biquads, and it will have less coefficient-sensitivity problems than a single fourth-order design. 　　Biquads come in several forms. The most obvious, a direct implementation of the second order difference equation (y[n] = a0*x[n] + a1*x[n-1] + a2*x[n-2] – b1*y[n-1] – b2*y[n-2]), called direct form I: Direct form I 　　Direct form I is the best choice for implementation in a fixed point processor because it has a single summation point (fixed point DSPs usually have an extended accumulator that allows for intermediate overflows). 　　We can take direct form I and split it at the summation point like this: We then take the two halves and swap them, so that the feedback half (the poles) comes first: Now, notice that one pair of the z delays is redundant, storing the same information as the other. So, we can merge the two pairs, yielding the direct form II configuration: Direct form II 　　In floating point, direct form II is better because it saves two memory locations, and floating point is not sensitive to overflow in the way fixed point math is. We can improve this a little by transposing the filter. To transpose a filter, reverse the signal flow direction—output becomes input, distribution nodes become summers, and summers become nodes. The characteristics of the filter are unchanged, but in this case it happens that the floating point characteristics are a little better. Floating point has better accuracy when intermediate sums are with closer values (adding small numbers to large number in floating point is less precise than with similar values). Here is the transposed direct form II: Transposed direct form II 　　Notes and recommendations 　　Again, direct form I is usually the best choice for fixed point, and transposed direct form II for floating point. 　　At low frequency settings, biquads are more susceptible to quantization error, mainly from the feedback coefficients (b1 and b2) and the delay memory. Lack of resolution in the coefficients makes precise positioning of the poles difficult, which is particularly a problem when the poles are positioned near the unit circle. The second problem, delay memory, is because multiplication generates more bits, and the bits are truncated when stored to memory. This quantization error is fed back in the filter, causing instability. 32-bit floating point is usually good enough for audio filters, but you may need to use double precision, especially at very low frequencies (for control filtering) and at high sample rates. 　　For fixed point filters, 24-bit coefficients and memory work well for most filters, but start to become unstable below about 300 Hz at 48 kHz sample rate (or twice that at 96 kHz). Double precision is always costly on a fixed point processor, but fortunately there is a simple technique for improving stability. Looking at the direct form I drawing, the quantization occurs when the higher precision accumulator is stored in the lower precision delay memory on the right side. By taking the quantization error (the difference between the full accumulated value and its value after storing it to memory) and adding it back in for the next sample calculation, the filter performs nearly as well as using full double precision calculations, but at a much lower computational cost. This technique is called first order noise shaping. There are higher order noise shapers, but this one works well enough to handle almost all audio needs, even at high sample rates. Direct form I with first-order noise shaping 　　In general, 16-bit fixed point processing is not suitable for audio without double precision coefficients and computation. 　　Finally, biquads are just one of a DSP programmers tools—they aren’t always the best filter form. There are other filters that don’t share the biquad’s low-frequency sensitivities (in general, biquad coefficient precision is very good at high frequencies, and poor at low ones; there are other filter forms that spread the precision out more evenly, or trade off reduced high frequency performance for better low frequency performance). However, biquads are well known and design tools are plentiful, so they are usually the first choice for an IIR filter unless you find a reason to use another. 　　There are too many filter forms to cover, but one other filter form that is popular for synthesizers is the state variable filter. It has very excellent low frequency performance, and limitations in the high frequencies that have to be worked around, but most importantly frequency and Q coefficients are separate and easy to change for dynamic filtering. It also make a great low frequency sine wave generator.   第二個鏈接來源的英文原文 　　In signal processing, a digital biquad filter is a second-order recursive linear filter, containing two poles and two zeros. “Biquad” is an abbreviation of “biquadratic”, which refers to the fact that in the Z domain, its transfer function is the ratio of two quadratic functions: 　　H(z)=b0+b1z−1+b2z−2a0+a1z−1+a2z−2H(z)=b0+b1z−1+b2z−2a0+a1z−1+a2z−2 　　The coefficients are often normalized such that a0 = 1: 　　H(z)=b0+b1z−1+b2z−21+a1z−1+a2z−2H(z)=b0+b1z−1+b2z−21+a1z−1+a2z−2 　　High-order IIR filters can be highly sensitive to quantization of their coefficients, and can easily become unstable. This is much less of a problem with first and second-order filters; therefore, higher-order filters are typically implemented as serially-cascaded biquad sections (and a first-order filter if necessary). The two poles of the biquad filter must be inside the unit circle for it to be stable. In general, this is true for all filters i.e. all poles must be inside the unit circle for the filter to be stable. 　　Implementation 　　Direct form 1 　　The most straightforward implementation is the direct form 1, which has the following difference equation: 　　y[n]=1a0(b0x[n]+b1x[n−1]+b2x[n−2]−a1y[n−1]−a2y[n−2])y[n]=1a0(b0x[n]+b1x[n−1]+b2x[n−2]−a1y[n−1]−a2y[n−2]) 　　or, if normalized: 　　y[n]=b0x[n]+b1x[n−1]+b2x[n−2]−a1y[n−1]−a2y[n−2]y[n]=b0x[n]+b1x[n−1]+b2x[n−2]−a1y[n−1]−a2y[n−2] 　　Here the b0b0, b1b1and b2b2coefficients determine zeros, and a1a1, a2a2 determine the position of the poles. 　　Flow graph of biquad filter in direct form 1: Biquad filter DF-I.svg 　　Direct form 2 　　The direct form 1 implementation requires four delay registers. An equivalent circuit is the direct form 2 implementation, which requires only two delay registers: The direct form 2 implementation is called the canonical form, because it uses the minimal amount of delays, adders and multipliers, yielding in the same transfer function as the direct form 1 implementation. The difference equations for direct form 2 are: 　　y[n]=b0w[n]+b1w[n−1]+b2w[n−2]y[n]=b0w[n]+b1w[n−1]+b2w[n−2] 　　where 　　w[n]=x[n]−a1w[n−1]−a2w[n−2]w[n]=x[n]−a1w[n−1]−a2w[n−2] 　　Transposed direct forms 　　Each of the two direct forms may be transposed by reversing the flow graph without altering the transfer function. Branch points are changed to summers and summers are changed to branch points. These provide modified implementations that accomplish the same transfer function which can be mathematically significant in a real-world implementation where precision may be lost in state storage. 　　The difference equations for Transposed Direct Form 2 are: 　　y[n]=b0x[n]+s1[n−1]y[n]=b0x[n]+s1[n−1] 　　where 　　s1[n]=s2[n−1]+b1x[n]−a1y[n]s1[n]=s2[n−1]+b1x[n]−a1y[n] 　　and 　　s2[n]=b2x[n]−a2y[n] 　　本文翻譯自：http://www.earlevel.com/main/2003/02/28/biquads/和wiki: https://en.wikipedia.org/wiki/Digital_biquad_filter（英文原文附在最后面） 注：本文轉載自未知，轉載目的在于傳遞更多信息，并不代表本網贊同其觀點和對其真實性負責。如有侵權行為，請聯系我們，我們會及時刪除。 1. 1
03-02
2. 2
10-23
3. 3
04-24
4. 4
10-29
5. 5
11-18
6. 6
03-01
7. 7
09-11
8. 8
04-17
9. 9
11-17
10. 10
03-04