-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval_filterconfig.py
72 lines (61 loc) · 2.85 KB
/
eval_filterconfig.py
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
import math
import cmath
from camilladsp_plot.filters import Biquad, BiquadCombo, Conv, DiffEq, Gain
def logspace(minval, maxval, npoints):
logmin = math.log10(minval)
logmax = math.log10(maxval)
perstep = (logmax-logmin)/npoints
values = [10.0**(logmin+n*perstep) for n in range(npoints)]
return values
def eval_filter(filterconf, name=None, samplerate=44100, npoints=1000):
fvect = logspace(1.0, samplerate*0.95/2.0, npoints)
if name is None:
name = "unnamed {}".format(filterconf['type'])
result = {"name": name, "samplerate": samplerate, "f": fvect }
if filterconf['type'] in ('Biquad', 'DiffEq', 'BiquadCombo'):
if filterconf['type'] == 'DiffEq':
currfilt = DiffEq(filterconf['parameters'], samplerate)
elif filterconf['type'] == 'BiquadCombo':
currfilt = BiquadCombo(filterconf['parameters'], samplerate)
else:
currfilt = Biquad(filterconf['parameters'], samplerate)
_fplot, magn, phase = currfilt.gain_and_phase(fvect)
result["magnitude"] = magn
result["phase"] = phase
elif filterconf['type'] == 'Conv':
if 'parameters' in filterconf:
currfilt = Conv(filterconf['parameters'], samplerate)
else:
currfilt = Conv(None, samplerate)
_ftemp, magn, phase = currfilt.gain_and_phase(fvect)
t, impulse = currfilt.get_impulse()
result["magnitude"] = magn
result["phase"] = phase
result["time"] = t
result["impulse"] = impulse
return result
def eval_filterstep(conf, pipelineindex, name="filterstep", npoints=1000, toimage=False):
samplerate = conf['devices']['samplerate']
fvect = logspace(1.0, samplerate*0.95/2.0, npoints)
pipelinestep = conf['pipeline'][pipelineindex]
totcgain=[1.0 for n in range(npoints)]
for filt in pipelinestep['names']:
filterconf = conf['filters'][filt]
if filterconf['type'] == 'DiffEq':
currfilt = DiffEq(filterconf['parameters'], samplerate)
elif filterconf['type'] == 'BiquadCombo':
currfilt = BiquadCombo(filterconf['parameters'], samplerate)
elif filterconf['type'] == "Biquad":
currfilt = Biquad(filterconf['parameters'], samplerate)
elif filterconf['type'] == "Conv":
currfilt = Conv(filterconf['parameters'], samplerate)
elif filterconf['type'] == "Gain":
currfilt = Gain(filterconf['parameters'])
else:
continue
_, cgainstep = currfilt.complex_gain(fvect)
totcgain = [cg * cgstep for (cg, cgstep) in zip(totcgain, cgainstep)]
gain = [20.0 * math.log10(abs(cg) + 1e-15) for cg in totcgain]
phase = [180 / math.pi * cmath.phase(cg) for cg in totcgain]
result = {"name": name, "samplerate": samplerate, "f": fvect, "magnitude": gain, "phase": phase}
return result