Bugfix: FilterBankDesigner now always returns second order sections with shape (SOS_ORDER, 6), even for the high pass section. Expanded the third octave bank compliance test. Since it passed for all common sample rates, I removed the warning that the filters might be non-compliant for fs unequal to 48 kHz.

This commit is contained in:
Casper Jansen 2023-01-09 11:59:48 +01:00
parent 786afc3fac
commit 0e31dbceac
2 changed files with 55 additions and 46 deletions

View File

@ -192,7 +192,10 @@ class FilterBankDesigner:
# Handle fu >= fNyquist: return high pass # Handle fu >= fNyquist: return high pass
if fu_n >= 1: if fu_n >= 1:
return butter(SOS_ORDER, fl_n, output='sos', btype='highpass') highpass = butter(SOS_ORDER, fl_n, output='sos', btype='highpass')
allpass = np.tile(np.asarray([1, 0, 0, 1, 0, 0]),
(SOS_ORDER-highpass.shape[0], 1))
return np.vstack((highpass, allpass)) # same shape=(SOS_ORDER, 6)
return butter(SOS_ORDER, [fl_n, fu_n], output='sos', btype='band') return butter(SOS_ORDER, [fl_n, fu_n], output='sos', btype='band')
@ -458,8 +461,6 @@ class OctaveBankDesigner(FilterBankDesigner):
if int(self.fs) in [44100, 48000, 96000]: if int(self.fs) in [44100, 48000, 96000]:
return 1.0 return 1.0
else: else:
warnings.warn(f'Sampling frequency {self.fs} Hz might result in '
'incorrect filters. Use 48 kHz instead.')
return 1.0 return 1.0
def sosFac_u(self, x): def sosFac_u(self, x):
@ -472,8 +473,6 @@ class OctaveBankDesigner(FilterBankDesigner):
if int(self.fs) in [44100, 48000, 96000]: if int(self.fs) in [44100, 48000, 96000]:
return 1.0 return 1.0
else: else:
warnings.warn(f'Sampling frequency {self.fs} Hz might result in '
'incorrect filters. Use 48 kHz instead.')
return 1.0 return 1.0
@ -614,8 +613,6 @@ class ThirdOctaveBankDesigner(FilterBankDesigner):
if int(self.fs) in [48000]: if int(self.fs) in [48000]:
return 1.0 return 1.0
else: else:
warnings.warn(f'Sampling frequency {self.fs} Hz might result in '
'incorrect filters. Use 48 kHz instead.')
return 1.0 return 1.0
def sosFac_u(self, x): def sosFac_u(self, x):
@ -624,6 +621,4 @@ class ThirdOctaveBankDesigner(FilterBankDesigner):
if int(self.fs) in [48000]: if int(self.fs) in [48000]:
return 1.0 return 1.0
else: else:
warnings.warn(f'Sampling frequency {self.fs} Hz might result in '
'incorrect filters. Use 48 kHz instead.')
return 1.0 return 1.0

View File

@ -13,24 +13,26 @@ from scipy.signal import freqz, iirdesign, sosfreqz, butter, cheby2
plt.close('all') plt.close('all')
# %% Settings of 'filter under test'
fs = 44.1e3
nsections = 5 # filter property
designer = ThirdOctaveBankDesigner(fs)
xmin = designer.nominal_txt_tox('25') # lowest highest band, nominal name
xmax = designer.nominal_txt_tox('20k') # highest band
# %% Run check # %% Run check
compliant = True # remains True if all filters comply def check_one(fs):
fnyq = fs/2 """Check for fs = fs"""
fll = designer.fl(xmin) / 1.1 # plot limits # Create filter bank
fuu = designer.fu(xmax) * 1.1 nsections = 5 # filter property
fig = plt.figure() designer = ThirdOctaveBankDesigner(fs)
plt.xlabel('f (Hz)')
plt.ylabel('Magnitude (dB)') # Test
for x in range(xmin, xmax+1): # loop over bands xmin = designer.nominal_txt_tox('25') # lowest highest band, nominal name
xmax = designer.nominal_txt_tox('20k') # highest band
compliant = True # remains True if all filters comply
fnyq = fs/2
fll = designer.fl(xmin) / 1.1 # plot limits
fuu = designer.fu(xmax) * 1.1
fig = plt.figure()
plt.xlabel('f (Hz)')
plt.ylabel('Magnitude (dB)')
for x in range(xmin, xmax+1): # loop over bands
fl = designer.fl(x) # lower cut off fl = designer.fl(x) # lower cut off
fu = designer.fu(x) # upper fu = designer.fu(x) # upper
@ -38,8 +40,6 @@ for x in range(xmin, xmax+1): # loop over bands
fln = fl/fnyq # lower cutoff, normalized [0, 1] fln = fl/fnyq # lower cutoff, normalized [0, 1]
fun = fu/fnyq # upper fun = fu/fnyq # upper
print(designer.nominal_txt(x))
sos = designer.createSOSFilter(x) sos = designer.createSOSFilter(x)
fnorm, h = sosfreqz(sos, worN=2**18, whole=False) fnorm, h = sosfreqz(sos, worN=2**18, whole=False)
freq = fnorm*fnyq/np.pi freq = fnorm*fnyq/np.pi
@ -47,7 +47,7 @@ for x in range(xmin, xmax+1): # loop over bands
h_dB = 20*np.log10(np.abs(h) + np.finfo(float).eps) h_dB = 20*np.log10(np.abs(h) + np.finfo(float).eps)
if not designer.testStandardCompliance(x, freq, h_dB): if not designer.testStandardCompliance(x, freq, h_dB):
print('==============================') print('==============================')
print(designer.nominal_txt(x), ' does not comply') print(designer.nominal_txt(x), ' does not comply\n')
compliant = False compliant = False
plt.semilogx(freq, h_dB) plt.semilogx(freq, h_dB)
@ -60,4 +60,18 @@ for x in range(xmin, xmax+1): # loop over bands
plt.ylim(-5, 1) plt.ylim(-5, 1)
plt.xlim(fll, fuu) plt.xlim(fll, fuu)
print(f"\nPassed test : {compliant}") print(f"Passed test : {compliant} for fs = {fs} Hz")
return compliant
if __name__ == "__main__":
# Make list of fs to be tested
# Taken from https://en.wikipedia.org/wiki/Sampling_(signal_processing)
fs_all = [8000, 11025, 16000, 22050, 32000, 37800, 44056, 44100, 47250,
48000, 50000, 50400, 64000, 88200, 96000, 176400, 192000, 352800]
allcompliant = True
for fs in fs_all:
compliant = check_one(fs)
if not compliant:
allcompliant = False
print(f"\n\nAll passed test : {allcompliant}")