-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiir_reson.h
94 lines (85 loc) · 1.83 KB
/
iir_reson.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#ifndef DSY_IIR_RESON_H
#define DSY_IIR_RESON_H
#include <stdint.h>
#include "arm_math.h"
#include "dumb_biquad.h"
#ifdef __cplusplus
namespace daisysp
{
/** iir_reson
*
* Εxtend the dumb_biquad to turn it into a resonator
* Jared Anderson May 2021
*
* Based on:
* STK BiQuad https://github.com/thestk/stk/blob/master/src/BiQuad.cpp
* Perry R. Cook's Real Sound Synthesis 3.10
* Garth Loy's Musimathics volume 2 5.13.3
* Will Pirkle - Designing Audio Effect Plugins in C++ 2nd Edition - 11.1.2
* A Note on Constant-Gain Digital Resonators
* Author(s): Ken Steiglitz
* Source: Computer Music Journal, Vol. 18, No. 4, (Winter, 1994), pp. 8-10
*
*/
class iir_reson : public dumb_biquad
{
public:
/*
* Initialize by setting the sample rate fs
* cutoff freq fc in Hz
* pole radius r
* and overall gain g
*/
void init(float fs, float fc, float r, float g)
{
fs_ = fs;
fc_ = fc;
to_wc_ = 2 * PI / fs_;
wc_ = to_wc_ * fc_;
r_ = r;
g_ = g;
a[0] = -2 * r_ * cos(wc_);
a[1] = r_ * r_;
/*
* Normalize by placing poles near DC and nyquist
* There are lots of different propositions of ways to do this
* This way seems to behave the best for a large range of r
*/
b[0] = g_ * r_;
b[1] = 0;
b[2] = -b[0];
}
void update_fc(float fc)
{
if (fc != fc_) {
fc_ = fc;
wc_ = to_wc_ * fc_;
a[0] = -2 * r_ * cos(wc_);
}
}
void update_r(float r)
{
if (r != r_) {
r_ = r;
wc_ = to_wc_ * fc_;
a[0] = -2 * r_ * cos(wc_);
a[1] = r_ * r_;
b[0] = g_ * r_;
b[2] = -b[0];
}
}
void update_g(float g)
{
if (g != g_) {
g_ = g;
b[0] = g_ * r_;
b[2] = -b[0];
}
}
private:
float fs_, fc_, wc_, r_, g_, to_wc_;
};
} // namespace daisysp
#endif
#endif