4Author: J.A. de Jong - ASCEE
6Provides the implementations of (fractional) octave filter banks
9__all__ = [
'FirOctaveFilterBank',
'FirThirdOctaveFilterBank',
10 'OverallFilterBank',
'SosOctaveFilterBank',
11 'SosThirdOctaveFilterBank']
13from .filter.filterbank_design
import (OctaveBankDesigner,
14 ThirdOctaveBankDesigner)
15from .lasp_cpp
import BiquadBank
21 Dummy type filter bank. Does nothing special, only returns output in a
27 Initialize overall filter bank
37 if data.ndim == 2
and data.shape[1] != 1:
38 raise RuntimeError(
"invalid number of channels, should be 1")
40 if data.shape[0] == 0:
47 tstart = self.
N / self.
fs
49 tend = tstart + Ncur / self.
fs
51 t = np.linspace(tstart, tend, Ncur, endpoint=
False)
54 output[
'Overall'] = {
't': t,
'data': data,
'x': 0}
63 Single channel (fractional) octave filter bank implementation, based on FIR
64 filters and sample rate decimation.
69 Initialize a OctaveFilterBank object.
72 fs: Sampling frequency of base signal
75 assert np.isclose(fs, 48000),
"Only sampling frequency" \
76 " available is 48 kHz"
79 self.
xs = list(range(xmin, xmax + 1))
81 maxdecimation = self.designer.firDecimation(self.
xs[0])
83 for dec
in maxdecimation:
95 dec = self.designer.firDecimation(x)
96 if len(dec) == 1
and dec[0] == 1:
98 elif len(dec) == 1
and dec[0] == 4:
107 raise ValueError(f
'No decimation found for x={x}')
109 xs_all = [xs_d1, xs_d4, xs_d16, xs_d64, xs_d256]
113 firs = np.empty((self.designer.firFilterLength, len(xs)), order=
'F')
114 for i, x
in enumerate(xs):
117 nominals_txt.append(self.designer.nominal_txt(x))
118 firs[:, i] = self.designer.createFirFilter(x)
119 filterbank = {
'fb': pyxFilterBank(firs, 1024),
121 'nominals': nominals_txt}
127 self.
dec = [1, 4, 16, 64, 256]
132 self.
Nf = [915, 806, 780, 582, 338]
136 Filter data for a given decimation stage
139 dec_stage: decimation stage
140 data: Pre-filtered data
143 if data.shape[0] == 0:
147 Nf = filtered.shape[0]
149 dec = self.
dec[dec_stage]
152 oldNf = self.
Nf[dec_stage]
154 tend = tstart + Nf/fd
156 t = np.linspace(tstart, tend, Nf, endpoint=
False)
157 self.
Nf[dec_stage] += Nf
158 for i, nom_txt
in enumerate(self.
filterbanks[dec_stage][
'nominals']):
159 x = self.designer.nominal_txt_tox(nom_txt)
160 output[nom_txt] = {
't': t,
'data': filtered[:, [i]],
'x': x}
168 assert data.ndim == 2
169 assert data.shape[1] == 1,
"invalid number of channels, should be 1"
171 if data.shape[0] == 0:
177 self.
N += data.shape[0]
179 output_unsorted = {**output_unsorted, **self.
filterd(0, data)}
183 if data.shape[0] > 0:
186 output_unsorted = {**output_unsorted,
187 **self.
filterd(dec_stage, data)}
191 nom_txt = self.designer.nominal_txt(x)
192 output[nom_txt] = output_unsorted[nom_txt]
197 return self.designer.firDecimation(x)
202 Filter bank which uses FIR filtering for each octave frequency band
207 FirFilterBank.__init__(self, fs, xmin, xmax)
212 Filter bank which uses FIR filtering for each one-third octave frequency
218 FirFilterBank.__init__(self, fs, xmin, xmax)
224 Initialize a second order sections filterbank
227 fs: Sampling frequency [Hz]
228 xmin: Minimum value for the bands
229 xmax: Maximum value for the bands
232 xmin = self.designer.xs[0]
234 xmax = self.designer.xs[-1]
237 self.
xs = list(range(xmin, xmax + 1))
245 for i, x
in enumerate(self.
xs):
246 channel = self.designer.createSOSFilter(x)
248 sos = np.empty((channel.size, len(self.
xs)))
249 sos[:, i] = channel.flatten()
251 self.
_fb = BiquadBank(sos)
262 if data.ndim > 1
and data.shape[1] != 1:
263 raise RuntimeError(
"invalid number of channels, should be 1")
265 if data.shape[0] == 0:
268 filtered_data = self.
_fb.filter(data)
269 if filtered_data.ndim == 1:
270 filtered_data = filtered_data[:,
None]
275 tstart = self.
N / self.
fs
277 tend = tstart + Ncur / self.
fs
279 t = np.linspace(tstart, tend, Ncur, endpoint=
False)
282 for i, x
in enumerate(self.
xs):
284 nom_txt = self.designer.nominal_txt(x)
285 output[nom_txt] = {
't': t,
'data': filtered_data[:, [i]],
'x': x}
295 Filter bank which uses FIR filtering for each one-third octave frequency
301 Initialize a second order sections filterbank.
304 fs: Sampling frequency [Hz]
305 xmin: Minimum value for the bands
306 xmax: Maximum value for the bands
309 SosFilterBank.__init__(self, fs, xmin, xmax)
314 Filter bank which uses FIR filtering for each one-third octave frequency
320 Initialize a second order sections filterbank.
323 fs: Sampling frequency [Hz]
324 xmin: Minimum value for the bands, if not specified, use minimum
325 xmax: Maximum value for the bands, if not specified, use maximum
328 SosFilterBank.__init__(self, fs, xmin, xmax)
Octave band filter designer.
Single channel (fractional) octave filter bank implementation, based on FIR filters and sample rate d...
__init__(self, fs, xmin, xmax)
Initialize a OctaveFilterBank object.
filter_(self, data)
Filter input data.
filterd(self, dec_stage, data)
Filter data for a given decimation stage.
Filter bank which uses FIR filtering for each octave frequency band.
__init__(self, fs, xmin, xmax)
Initialize a OctaveFilterBank object.
Filter bank which uses FIR filtering for each one-third octave frequency band.
__init__(self, fs, xmin, xmax)
Initialize a OctaveFilterBank object.
filter_(self, data)
Filter input data.
__init__(self, fs)
Initialize overall filter bank.
filter_(self, data)
Filter input data.
__init__(self, fs, xmin, xmax)
Initialize a second order sections filterbank.
Filter bank which uses FIR filtering for each one-third octave frequency band.
__init__(self, fs, xmin=None, xmax=None)
Initialize a second order sections filterbank.
Filter bank which uses FIR filtering for each one-third octave frequency band.
__init__(self, fs, xmin=None, xmax=None)
Initialize a second order sections filterbank.