forked from PaulStoffregen/Audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_biquad.cpp
117 lines (109 loc) · 3.89 KB
/
filter_biquad.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* Audio Library for Teensy 3.X
* Copyright (c) 2014, Paul Stoffregen, [email protected]
*
* Development of this audio library was funded by PJRC.COM, LLC by sales of
* Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
* open source software by purchasing Teensy or other PJRC products.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice, development funding notice, and this permission
* notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <Arduino.h>
#include "filter_biquad.h"
#include "utility/dspinst.h"
#if defined(__ARM_ARCH_7EM__)
void AudioFilterBiquad::update(void)
{
audio_block_t *block;
int32_t b0, b1, b2, a1, a2, sum;
uint32_t in2, out2, bprev, aprev, flag;
uint32_t *data, *end;
int32_t *state;
block = receiveWritable();
if (!block) return;
end = (uint32_t *)(block->data) + AUDIO_BLOCK_SAMPLES/2;
state = (int32_t *)definition;
do {
b0 = *state++;
b1 = *state++;
b2 = *state++;
a1 = *state++;
a2 = *state++;
bprev = *state++;
aprev = *state++;
sum = *state & 0x3FFF;
data = end - AUDIO_BLOCK_SAMPLES/2;
do {
in2 = *data;
sum = signed_multiply_accumulate_32x16b(sum, b0, in2);
sum = signed_multiply_accumulate_32x16t(sum, b1, bprev);
sum = signed_multiply_accumulate_32x16b(sum, b2, bprev);
sum = signed_multiply_accumulate_32x16t(sum, a1, aprev);
sum = signed_multiply_accumulate_32x16b(sum, a2, aprev);
out2 = signed_saturate_rshift(sum, 16, 14);
sum &= 0x3FFF;
sum = signed_multiply_accumulate_32x16t(sum, b0, in2);
sum = signed_multiply_accumulate_32x16b(sum, b1, in2);
sum = signed_multiply_accumulate_32x16t(sum, b2, bprev);
sum = signed_multiply_accumulate_32x16b(sum, a1, out2);
sum = signed_multiply_accumulate_32x16t(sum, a2, aprev);
bprev = in2;
aprev = pack_16b_16b(
signed_saturate_rshift(sum, 16, 14), out2);
// retaining part of the sum is meant to implement the
// "first order noise shaping" described in this article:
// http://www.earlevel.com/main/2003/02/28/biquads/
// TODO: is logical AND really correct, or maybe it
// should really be signed_saturate_rshift() ???
sum &= 0x3FFF;
bprev = in2;
*data++ = aprev;
} while (data < end);
flag = *state & 0x80000000;
*state++ = sum | flag;
*(state-2) = aprev;
*(state-3) = bprev;
} while (flag);
transmit(block);
release(block);
}
void AudioFilterBiquad::setCoefficients(uint32_t stage, const int *coefficients)
{
if (stage >= 4) return;
int32_t *dest = definition + (stage << 3);
__disable_irq();
if (stage > 0) *(dest - 1) |= 0x80000000;
*dest++ = *coefficients++;
*dest++ = *coefficients++;
*dest++ = *coefficients++;
*dest++ = *coefficients++ * -1;
*dest++ = *coefficients++ * -1;
//*dest++ = 0;
//*dest++ = 0; // clearing filter state causes loud pop
dest += 2;
*dest &= 0x80000000;
__enable_irq();
}
#elif defined(KINETISL)
void AudioFilterBiquad::update(void)
{
audio_block_t *block;
block = receiveReadOnly();
if (block) release(block);
}
#endif