A hearing aid's chip has a tight budget of multiplications per second, and every filter it runs spends some of them on each sample (fs samples per second). The designer needs a low-pass that keeps frequencies up to fpass hertz within ripple_db decibels of full volume, and pushes everything from fstop hertz up at least atten_db decibels down. Two kinds of filter can do it; which is cheaper?
FIR (windowed-sinc). A rule of thumb gives the number of weights:
estimate = atten_db * fs / (22 * (fstop - fpass))
N = the smallest whole number >= estimate, raised by 1 if it is even (so the filter is symmetric)
A symmetric filter adds the two inputs that share a weight before multiplying, so it costs (N + 1) / 2 multiplications per sample.
IIR (Butterworth, built from biquads). The order n it needs is the smallest whole number at least
log10((10 ** (atten_db / 10) - 1) / (10 ** (ripple_db / 10) - 1)) / (2 * log10(tan(pi * fstop / fs) / tan(pi * fpass / fs)))
(the tan terms stretch the frequency axis the way the digital design does). It is built from ceil(n / 2) biquad sections of 5 multiplications each (an odd order still uses a whole section for its last stage).
Write filter_cost(fs, fpass, fstop, ripple_db, atten_db) that returns the tuple (N, fir_mults, n, iir_mults).
Examples
Input: fs = 1000, fpass = 50, fstop = 100, ripple_db = 1, atten_db = 40
Output: (37, 19, 8, 20)
Explanation: the estimate is 40 * 1000 / 1100 = 36.4, so N = 37 (odd), costing 19.
The IIR order formula gives 7.35, so n = 8: four biquads, 20 multiplications.
Input: fs = 16000, fpass = 300, fstop = 400, ripple_db = 1, atten_db = 60
Output: (437, 219, 27, 70)
Explanation: a 100 Hz transition at 16000 samples per second makes the FIR filter long, but
for the IIR filter only the ratio 400 / 300 matters: order 27, fourteen biquads, 70.
Constraints
0 < fpass < fstop < fs / 2,0 < ripple_db < atten_db <= 120- the tests keep both raw estimates away from whole numbers, so the rounding is never in doubt
Goals
- Estimate how many weights an FIR low-pass needs for a given transition and attenuation
- Estimate the order of a Butterworth IIR low-pass for the same specification
- Compare the two by the multiplications they cost per sample