Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion shared-module/synthio/Biquad.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void common_hal_synthio_biquad_tick(mp_obj_t self_in) {
case SYNTHIO_PEAKING_EQ:
b0 = 1 + alpha * A;
b1 = -2 * sc.c;
b2 = 1 + alpha * A;
b2 = 1 - alpha * A;
a0 = 1 + alpha / A;
a1 = -2 * sc.c;
a2 = 1 - alpha / A;
Expand Down
54 changes: 54 additions & 0 deletions tests/circuitpython/synthio_biquad_peaking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# A peaking EQ bell should be unity at DC and near Nyquist, and hit its own
# gain at the centre frequency.
import array
import math
from audiocore import get_buffer, RawSample
from audiofilters import Filter
from synthio import Biquad, FilterMode

RATE = 48000
FRAMES = 4096
LEVEL = 2000
GAIN_DB = 6.0
CENTRE = 1000.0


def through(values):
source = RawSample(values, sample_rate=RATE, channel_count=1)
bell = Biquad(FilterMode.PEAKING_EQ, CENTRE, Q=1.0, A=10 ** (GAIN_DB / 40))
effect = Filter(
filter=bell,
sample_rate=RATE,
channel_count=1,
bits_per_sample=16,
samples_signed=True,
buffer_size=FRAMES * 2,
)
effect.play(source, loop=True)
out = []
for block in range(3):
data = bytes(get_buffer(effect)[1])
if block < 2: # let the filter settle
continue
for index in range(0, len(data) - 1, 2):
sample = data[index] | (data[index + 1] << 8)
out.append(sample - 65536 if sample >= 32768 else sample)
return out


def tone(hz):
return array.array(
"h",
[int(LEVEL * math.sin(2 * math.pi * hz * frame / RATE)) for frame in range(FRAMES)],
)


def rms_db(values):
total = sum(value * value for value in values)
return 20 * math.log10(math.sqrt(total / len(values)) / (LEVEL / math.sqrt(2)))


dc = through(array.array("h", [LEVEL] * FRAMES))
print("DC %+.1f" % (20 * math.log10(abs(dc[-1]) / LEVEL)))
print("centre %+.1f" % rms_db(through(tone(CENTRE))))
print("12 kHz %+.1f" % rms_db(through(tone(12000.0))))
3 changes: 3 additions & 0 deletions tests/circuitpython/synthio_biquad_peaking.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DC -0.1
centre +5.9
12 kHz +0.0
Loading