-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiir_1p_lp.h
86 lines (74 loc) · 1.43 KB
/
iir_1p_lp.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
#pragma once
#ifndef DSY_IIR_1P_LP_H
#define DSY_IIR_1P_LP_H
#include <stdint.h>
#include "arm_math.h"
#ifdef __cplusplus
namespace daisysp
{
/** iir_1p_lp
* 1st order Butterworth LP filter
*
* fs = sampling frequency
*
* fc = center frequency in Hz
*
* Handbook for Digital Signal Processing Ch 9.
*
* [1] Sanjit K. Mitra,
* Digital Signal Processing a Computer Based Approach
*
*/
class iir_1p_lp
{
public:
/*
* Initialize by setting the sample rate fs
* cutoff freq fc in Hz
* and overall gain g
*/
void init(float fs, float fc, float g = 1)
{
fs_ = fs;
fc_ = fc;
to_wc_ = 2 * PI / fs_;
wc_ = to_wc_ * fc_;
xn_ = yn_ = 0;
alpha_ = (1 - tan(wc_ / 2.0)) / (1 + tan(wc_ / 2.0));
g_ = g * (1 - alpha_) / 2.0;
b0_ = b1_ = g_;
a1_ = -alpha_;
}
float Process(float in)
{
float out;
out = in * b0_ + xn_ * b1_ - a1_ * yn_;
xn_ = in;
yn_ = out;
return out;
}
void update_fc(float fc, float g = 1)
{
if (fc != fc_) {
fc_ = fc;
wc_ = to_wc_ * fc_;
alpha_ = (1 - tan(wc_ / 2.0)) / (1 + tan(wc_ / 2.0));
g_ = g * (1 - alpha_) / 2.0;
b0_ = b1_ = g_;
a1_ = -alpha_;
}
}
void update_g(float g)
{
if (g != g_) {
g_ = g * (1 - alpha_) / 2.0;
b0_ = b1_ = g_;
}
}
private:
float b0_, b1_, a1_, alpha_, xn_, yn_;
float fs_, fc_, wc_, g_, to_wc_;
};
} // namespace daisysp
#endif
#endif