def test_FIR_process_single(): # check if a single channel is processed correctly insig = loadmat('test/Inputdata.mat', squeeze_me=True)['indata16k'] fbFIR = FIR(fs=16000, cfs=ERBnum2Hz(np.arange(1, 32.1, .5)), complexresponse=True) refout = loadmat('test/FIR16kTestdata.mat', squeeze_me=True)['y'] outsig = fbFIR.process_single(insig, 10) assert(peak_error(outsig, refout[:, 10])<1e-10)
def test_FIR_process(): # check if filtering works correctly insig = loadmat('test/Inputdata.mat', squeeze_me=True)['indata16k'] fbFIR = FIR(fs=16000, cfs=ERBnum2Hz(np.arange(1, 32.1, .5)), complexresponse=True) outsig = fbFIR.process(insig) refout = loadmat('test/FIR16kTestdata.mat', squeeze_me=True)['y'].T assert(peak_error(outsig, refout)<1e-10)
def test_FIR_ir(): # check if the impulse response matches the MATLAB version Fdata = loadmat('test/FIR16kTestdata.mat', squeeze_me=True)['FD'] fbFIR = FIR(fs=16000, cfs=ERBnum2Hz(np.arange(1, 32.1, .5)), complexresponse=True) assert(peak_error(fbFIR.ir, Fdata['G'][()].conj())<1e-10)
def test_FIR_ERB(): # check if the filter bandwidths are calculated the same way # as the MATLAB reference Fdata = loadmat('test/FIR16kTestdata.mat', squeeze_me=True)['FD'] fbFIR = FIR(fs=16000, cfs=ERBnum2Hz(np.arange(1, 32.1, .5)), complexresponse=True) assert(peak_error(1.0186*fbFIR.ERB, Fdata['b'][()])<1e-10)
def test_FIR_clear(): # make sure that if _clear() is called result is same as a brand # new filterbank insig = loadmat('test/Inputdata.mat', squeeze_me=True)['indata16k'] fbFIR = FIR(fs=16000, cfs=ERBnum2Hz(np.arange(1, 32.1, .5)), complexresponse=True) refout = fbFIR.process(insig) fbFIR = FIR(fs=16000, cfs=ERBnum2Hz(np.arange(1, 32.1, .5)), complexresponse=True) _ = fbFIR.process(np.random.randn(1000)) fbFIR._clear() outsig = fbFIR.process(insig) assert(peak_error(outsig, refout)<1e-10)
def test_FIR_process_memory(): # check if processing a whole is the same as processing 2 chunks insig = loadmat('test/Inputdata.mat', squeeze_me=True)['indata16k'] fbFIR = FIR(fs=16000, cfs=ERBnum2Hz(np.arange(1, 32.1, .5)), complexresponse=True) refout = fbFIR.process(insig) fbFIR = FIR(fs=16000, cfs=ERBnum2Hz(np.arange(1, 32.1, .5)), complexresponse=True) outsig1 = fbFIR.process(insig[:800]) outsig2 = fbFIR.process(insig[800:]) outsig = np.hstack((outsig1, outsig2)) assert(peak_error(outsig, refout)<1e-10)
from __future__ import division import numpy as np from scipy.io import loadmat from gtfblib.gtfb import ERBnum2Hz from gtfblib.fir import FIR cfs_erb = (np.arange(1, 33.1, .5)) fbFIR = FIR(fs=16000, L=512, cfs=ERBnum2Hz(np.arange(1, 33.1, .5)), complexresponse=True) print( f' nb of filters = {fbFIR.nfilt}, cfs size = {fbFIR.cfs.size}, cfs = {fbFIR.cfs}, cfs_erb= {cfs_erb} size {cfs_erb.size}' ) fbFIR.plot_ir(3, 3)
def __init__(self, N=32, sr=16000, nb_gamma_filters=32, gamma_filt_len=1024, initialize_ana_kernels=False, initialize_syn_kernels=False): super(ConvGamma, self).__init__() self.N = N self.gamma_filter_len = gamma_filt_len self.pad_amount = int(self.gamma_filter_len - 1) // 2 self.initialize_ana_kernels = initialize_ana_kernels self.initialize_syn_kernels = initialize_syn_kernels self.sampling_rate = sr self.nb_gamma_filters = nb_gamma_filters # # 1D input self.nb_freq_pts = int((self.N / 2) + 1) # # Filterbanks to generate gamatone (real part, imag part) # self.gamma_filt_r = nn.Conv1d(in_channels=1, out_channels=self.nb_gamma_filters, kernel_size=self.gamma_filter_len, stride=1, padding=(self.pad_amount), padding_mode='zeros', bias=True) self.gamma_filt_i = nn.Conv1d(in_channels=1, out_channels=self.nb_gamma_filters, kernel_size=self.gamma_filter_len, stride=1, padding=(self.pad_amount), padding_mode='zeros', bias=True) # Bank of Convolution Filters to do inverse Gamma self.inv_gamma_filt_r = nn.Conv1d(in_channels=self.nb_gamma_filters, out_channels=1, kernel_size=self.gamma_filter_len, stride=1, padding=(self.pad_amount), padding_mode='zeros', bias=True) self.inv_gamma_filt_i = nn.Conv1d(in_channels=self.nb_gamma_filters, out_channels=1, kernel_size=self.gamma_filter_len, stride=1, padding=(self.pad_amount), padding_mode='zeros', bias=True) # # Weight Initialization # ===================== # Get the Gamatone filters fbFIR = FIR(fs=self.sampling_rate, L=self.gamma_filter_len, cfs=ERBnum2Hz(np.arange(1, 33.1, .5)), complexresponse=True, groupdelay=self.gamma_filter_len // 2) print( f' nb of filters = {fbFIR.nfilt}, cfs size = {fbFIR.cfs.size}, cfs = {fbFIR.cfs}' ) gamma_filts = fbFIR.ir / float(2.0 * self.gamma_filter_len) # Get the reverse Gamatone filters fbinvFIR = FIR(fs=self.sampling_rate, L=self.gamma_filter_len, cfs=ERBnum2Hz(np.arange(1, 33.1, .5)), complexresponse=True, reversetime=True, groupdelay=0) inv_gamma_filts = fbinvFIR.ir # Load the values into the kernels (with the correct dimensions) # initialize the kernels of the Gamatone FilterBank if (self.initialize_ana_kernels == True): print(f'initializing Gammatone Filter Bank') init_gamma_filts_r = torch.FloatTensor(gamma_filts.real[:, None, :]) init_gamma_filts_i = torch.FloatTensor(gamma_filts.imag[:, None, :]) with torch.no_grad(): self.gamma_filt_r.weight = torch.nn.Parameter( init_gamma_filts_r) self.gamma_filt_i.weight = torch.nn.Parameter( init_gamma_filts_i) self.gamma_filt_r.bias = torch.nn.Parameter( torch.zeros(self.nb_gamma_filters)) self.gamma_filt_i.bias = torch.nn.Parameter( torch.zeros(self.nb_gamma_filters)) # initialize the kernels of the Gamatone FilterBank if (self.initialize_syn_kernels == True): print(f'initializing Gammatone Filter Bank') init_inv_gamma_filts_r = torch.FloatTensor( inv_gamma_filts.real.copy()[None, :, :]) init_inv_gamma_filts_i = torch.FloatTensor( inv_gamma_filts.imag.copy()[None, :, :]) with torch.no_grad(): self.inv_gamma_filt_r.weight = torch.nn.Parameter( init_inv_gamma_filts_r) self.inv_gamma_filt_i.weight = torch.nn.Parameter( init_inv_gamma_filts_i) self.inv_gamma_filt_r.bias = torch.nn.Parameter(torch.zeros(1)) self.inv_gamma_filt_i.bias = torch.nn.Parameter(torch.zeros(1)) if (self.initialize_syn_kernels == False): print(f'initializing zeros Filter Bank') z_data = np.zeros([self.nb_gamma_filters, self.gamma_filter_len], dtype=float) init_inv_gamma_filts_r = torch.FloatTensor( z_data.copy()[None, :, :]) init_inv_gamma_filts_i = torch.FloatTensor( z_data.copy()[None, :, :]) with torch.no_grad(): self.inv_gamma_filt_r.weight = torch.nn.Parameter( init_inv_gamma_filts_r) self.inv_gamma_filt_i.weight = torch.nn.Parameter( init_inv_gamma_filts_i) self.inv_gamma_filt_r.bias = torch.nn.Parameter(torch.zeros(1)) self.inv_gamma_filt_i.bias = torch.nn.Parameter(torch.zeros(1)) print(f' zero init')
def test_FIR_reversetime(): # check if the reverse time option works as expected fbFIR1 = FIR() fbFIR2 = FIR(reversetime=True) assert(all(f1.ir[10, ::-1]==f2.ir[10,:]))