RavEngine
Loading...
Searching...
No Matches
filter_coefficient_generators.h
1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef RESONANCE_AUDIO_DSP_FILTER_COEFFICIENT_GENERATORS_H_
18#define RESONANCE_AUDIO_DSP_FILTER_COEFFICIENT_GENERATORS_H_
19
20#include "dsp/biquad_filter.h"
21
22// Functions for the generation of filter coefficients for various common tasks.
23// Currently supports the following filter types:
24// Low pass first-order filter.
25// Low pass biquad filter.
26// Band pass biquad filter.
27// Dual band, matched phase biquad shelf filters.
28namespace vraudio {
29
30// Computes corresponding biquad coefficients for band pass filter with respect
31// to the centre frequency and the bandwidth between -3 dB frequencies.
32//
33// b0 + b1*z^-1 + b2*z^-2
34// H(z) = ------------------------
35// a0 + a1*z^-1 + a2*z^-2
36//
37// where:
38// w0 = 2*pi*center_frequency/sample_rate
39//
40// alpha = sin(w0)*sinh( ln(2)/2 * bandwidth * w0/sin(w0) )
41//
42// b0 = alpha
43// b1 = 0
44// b2 = -alpha
45// a0 = 1 + alpha
46// a1 = -2*cos(w0)
47// a2 = 1 - alpha
48//
49// @param sample_rate Sampling rate in Hz.
50// @param centre_frequency Centre frequency of passband.
51// @param bandwidth Bandwidth in octaves between -3 dB frequencies.
52// @return Output structure of band-pass BiquadCoefficients.
53BiquadCoefficients ComputeBandPassBiquadCoefficients(int sample_rate,
54 float centre_frequency,
55 int bandwidth);
56
57// Computes two sets of transfer function coefficients to be used with a
58// pair of generic bi-quad filters. The coefficients are used to implement a
59// phase-matched low-pass |low_pass_state| and high-pass |high_pass_state|
60// filter pair with a cross-over frequency defined as |crossover_frequency|.
61//
62// Implementation of the matched bi-quad filter pair as described in:
63// http://www.ai.sri.com/ajh/ambisonics/BLaH3.pdf
64//
65// b0 + b1*z^-1 + b2*z^-2
66// H(z) = ------------------------
67// a0 + a1*z^-1 + a2*z^-2
68//
69// where:
70//
71// a0 = 1
72//
73// 2(k^2 − 1)
74// a1 = --------------
75// k^2 + 2k + 1
76//
77// k^2 - 2k + 1
78// a2 = --------------
79// k^2 + 2k + 1
80//
81// low-pass: high-pass:
82//
83// k^2 1
84// b0 = ------------- b0 = --------------
85// k^2 + 2k + 1 k^2 + 2k + 1
86//
87// b1 = 2b0 b1 = -2b0
88//
89// b2 = b0 b2 = b0
90//
91// and
92//
93// pi * crossover_frequency
94// k = tan --------------------------
95// sample_frequency
96//
97// @param sample_rate Sampling rate in [Hz]
98// @param crossover_frequency Cross-over frequency in [Hz]
99// @param low_pass_coefficients Output structure of low-pass bi-quad
100// coefficients
101// @param high_pass_coefficients Output structure of high-pass bi-quad
102// coefficients.
103void ComputeDualBandBiquadCoefficients(
104 int sample_rate, float crossover_frequency,
105 BiquadCoefficients* low_pass_coefficients,
106 BiquadCoefficients* high_pass_coefficients);
107
108// Computes biquad coefficients for low pass filter with respect to the
109// specification frequency and the attenuation value at that frequency.
110//
111// b0 + b1*z^-1 + b2*z^-2
112// H(z) = ------------------------
113// a0 + a1*z^-1 + a2*z^-2
114//
115// where:
116// Q = 2.5273e-06*attenuation^4 + 0.00018737*attenuation^3 +
117// 0.0061882*attenuation^2 + 0.11395*attenuation + 0.99905
118//
119// w0 = 2*pi*specification_frequency/sample_rate;
120//
121// alpha = sin(w0)/(2*Q);
122//
123// a0 = 1 + alpha;
124// a1 = -2*cos(w0);
125// a2 = 1 - alpha;
126// b0 = (1 - cos(w0))/2;
127// b1 = 1 - cos(w0);
128// b2 = (1 - cos(w0)/2;
129//
130// These coefficients were generated after a bilinerar transform of an
131// analogue filter H(s) = 1 / (s^2 + s/Q + 1), from:
132// www.analog.com/library/analogdialogue/archives/43-09/edch%208%20filter.pdf.
133//
134// Please note Q has been calculated by fitting a 4th order polynomial to the
135// average of Q vs attenuation curves at different f0's from 10kHz to 19kHz.
136// Please note that at differing frequencies these graphs had identical shape
137// and differed only by an overall offset of at most 0.1 (attenuation) dB from
138// the mean. This script can be found at
139
140//
141// @param sample_rate Sampling rate in Hz.
142// @param specification_frequency Frequency at which attenuation applies in Hz.
143// @param attenuation Attenuation at specification_frequency in negative dB.
144// @return low_pass Output structure of low-pass BiquadCoefficients.
145BiquadCoefficients ComputeLowPassBiquadCoefficients(
146 int sample_rate, float specification_frequency, float attenuation);
147
148// Generates a coefficient for the |MonoPoleFilterClass| based on a 3dB
149// bandwidth.
150//
151// The Laplace transfer function of a first order low pass system is:
152//
153// 1
154// ------------ where tau is the RC time constant of the system.
155// 1 + tau * s
156//
157// For a discrete moving average filter with input x[n] and output y[n],
158// the difference equation is:
159// y[n] = a * y[n - 1] + (1 - a) * x[n]
160// tau
161// a = ---------- where T is the sample period.
162// tau + T
163// since the -3dB bandwith of a first order system can be
164// 1 related to its time constant by
165// f3 = --------------
166// 2 * pi * tau we can obtain 'a' from cuttoff_frequency and
167// sample rate.
168//
169// @param cuttoff_frequency The -3dB frequency in Hz of the low pass lobe.
170// @param sample_rate System sampling rate.
171// @return A |MonoPoleFilterClass| coefficient for the specified bandwidth.
172float ComputeLowPassMonoPoleCoefficient(float cuttoff_frequency,
173 int sample_rate);
174
175} // namespace vraudio
176
177#endif // RESONANCE_AUDIO_DSP_FILTER_COEFFICIENT_GENERATORS_H_