def run_benchmark(self): sample_rate = self.params['sample_rate'] chunksize = self.params['chunksize'] nloop = self.params['nloop'] nb_channel = self.params['nb_channel'] gpu = self.gpuDeviceSelection.get_configuration() gpu_platform_index = gpu['platform_index'] gpu_device_index = gpu['device_index'] lost_chunksize = 1024 backward_chunksize = lost_chunksize + chunksize length = int(chunksize*nloop) in_buffer = hls.moving_erb_noise(length, sample_rate=sample_rate) in_buffer = np.tile(in_buffer[:, None],(1, nb_channel)) loss_params = { 'left' : {'freqs' : [125., 250., 500., 1000., 2000., 4000., 8000.], 'compression_degree': [0., 0., 0., 0., 0., 0., 0.], 'passive_loss_db' : [0., 0., 0., 0., 0., 0., 0.], }, 'right' : {'freqs' : [125., 250., 500., 1000., 2000., 4000., 8000.], 'compression_degree': [0., 0., 0., 0., 0., 0., 0.], 'passive_loss_db' : [0., 0., 0., 0., 0., 0., 0.], } } processing_conf = dict(nb_freq_band=32, level_step=1, loss_params=loss_params, low_freq = 100., high_freq = sample_rate*0.45, debug_mode=False, chunksize=chunksize, backward_chunksize=backward_chunksize) #~ node, online_arrs = hls.run_one_node_offline(hls.MainProcessing, in_buffer, chunksize, #~ sample_rate, node_conf=node_conf, buffersize_margin=backward_chunksize) for _class in [hls.InvCGC, hls.InvComp]: #~ for _class in [hls.InvComp, hls.InvComp2,]: #~ for _class in [hls.InvComp,]: #~ for _class in [hls.InvComp2,]: print() print(_class.__name__, sample_rate, chunksize, backward_chunksize, nb_channel, processing_conf['nb_freq_band']) processing = _class(nb_channel=nb_channel, sample_rate=sample_rate, apply_configuration_at_init=False, **processing_conf) processing.create_opencl_context(gpu_platform_index=gpu_platform_index, gpu_device_index=gpu_device_index) print(processing.ctx) processing.initialize() online_arrs = hls.run_instance_offline(processing, in_buffer, chunksize, sample_rate, buffersize_margin=backward_chunksize, time_stats=True)
def test_passive_loss(): in_buffer = hls.moving_sinus(length, sample_rate=sample_rate, speed=.5, f1=500., f2=2000., ampl=.8) in_buffer = np.tile(in_buffer[:, None], (1, nb_channel)) loss_params = { 'left': { 'freqs': [125 * 2**i for i in range(7)], 'compression_degree': [1.] * 7, 'passive_loss_db': [-20.] * 7 } } loss_params['right'] = loss_params['left'] processing_conf = dict(nb_freq_band=32, level_step=10, debug_mode=True, chunksize=chunksize, backward_chunksize=backward_chunksize, loss_params=loss_params) #~ processing, online_arrs = hls.run_class_offline(hls.InvComp, in_buffer, chunksize, sample_rate, processing_conf=processing_conf, buffersize_margin=backward_chunksize) processing = hls.InvComp(nb_channel=nb_channel, sample_rate=sample_rate, dtype='float32', **processing_conf) online_arrs = hls.run_instance_offline( processing, in_buffer, chunksize, sample_rate, dtype='float32', buffersize_margin=backward_chunksize, time_stats=True) n = processing.nb_freq_band online_pgc2 = online_arrs['pgc2'] online_passive = online_arrs['passive'] #~ offline_passive = online_pgc2.copy() #~ channels = ('left', 'right')[:nb_channel] #~ for c, chan in enumerate(channels): #~ for i in range(n): #~ offline_passive[:, c*n + i] = processing.passive_gain[c*n + i] * online_pgc2 offline_passive = processing.passive_gain.T * online_pgc2 residual = np.abs( (online_passive.astype('float64') - offline_passive.astype('float64')) / np.mean(np.abs(offline_passive.astype('float64')))) print(np.max(residual)) freq_band = 15 fig, ax = plt.subplots(nrows=2, sharex=True) #~ ax[0].plot(in_buffer2[:, freq_band], color = 'k') ax[0].plot(offline_passive[:, freq_band], color='g') ax[0].plot(online_passive[:, freq_band], color='r', ls='--') ax[1].plot(residual[:, freq_band], color='m') for i in range(nloop): ax[1].axvline(i * chunksize) plt.show() assert np.max(residual) < 1e-4, 'passive online differt from offline'
def test_invcomp(): #~ in_buffer = hls.moving_erb_noise(length) in_buffer = hls.moving_sinus(length, sample_rate=sample_rate, speed=.5, f1=100., f2=2000., ampl=.8) in_buffer = np.tile(in_buffer[:, None], (1, nb_channel)) #~ print(in_buffer.shape) #~ exit() loss_params = { 'left': { 'freqs': [125 * 2**i for i in range(7)], 'compression_degree': [0] * 7, 'passive_loss_db': [0] * 7 } } loss_params['right'] = loss_params['left'] processing_conf = dict(nb_freq_band=32, level_step=1., level_max=100., loss_params=loss_params, low_freq=100., high_freq=15000., debug_mode=True, chunksize=chunksize, backward_chunksize=backward_chunksize) #~ processing, online_arrs = hls.run_class_offline(hls.InvComp, in_buffer, chunksize, sample_rate, processing_conf=processing_conf, buffersize_margin=backward_chunksize) processing = hls.InvComp(nb_channel=nb_channel, sample_rate=sample_rate, dtype='float32', **processing_conf) online_arrs = hls.run_instance_offline( processing, in_buffer, chunksize, sample_rate, dtype='float32', buffersize_margin=backward_chunksize, time_stats=True) print('nlevel', processing.levels.size, 'nb_freq_band', processing.nb_freq_band) freq_band = 15 fig, ax = plt.subplots(nrows=7, sharex=True) #, sharey=True) ax[0].plot(in_buffer[:, 0], color='k') steps = ['pgc1', 'levels', 'dyngain', 'pgc2', 'passive'] for i, k in enumerate(steps): online_arr = online_arrs[k] print(online_arr.shape) ax[i + 1].plot(online_arr[:, freq_band], color='b') ax[i + 1].set_ylabel(k) #~ ax[0].plot(offline_arr[:, 0], color = 'g') out_buffer = online_arrs['main_output'] ax[-1].plot(out_buffer[:, 0], color='k') if nb_channel == 2: #test stereo is like mono #~ fig, ax = plt.subplots() #~ ax.plot(out_buffer[:,0], color='b') #~ ax.plot(out_buffer[:,1], color='r') #~ fig, ax = plt.subplots() #~ ax.plot(out_buffer[:,0]-out_buffer[:,1], color='b') #~ plt.show() assert np.all(np.abs(out_buffer[:, 0] - out_buffer[:, 1]) < 1e-5) plt.show()
def test_pgc2(): assert nb_channel == 1 #~ in_buffer = hls.moving_sinus(length, sample_rate=sample_rate, speed = .5, f1=50., f2=2000., ampl = .8) #~ in_buffer = hls.moving_erb_noise(length, sample_rate=sample_rate,) in_buffer = hls.whitenoise( length, sample_rate=sample_rate, ) in_buffer = np.tile(in_buffer[:, None], (1, nb_channel)) loss_params = { 'left': { 'freqs': [125 * 2**i for i in range(7)], 'compression_degree': [0] * 7, 'passive_loss_db': [0] * 7 } } loss_params['right'] = loss_params['left'] processing_conf = dict(nb_freq_band=32, level_max=120, level_step=1, debug_mode=True, low_freq=60., high_freq=15000., loss_params=loss_params, chunksize=chunksize, backward_chunksize=backward_chunksize) #~ processing, online_arrs = hls.run_class_offline(hls.InvComp, in_buffer, chunksize, sample_rate, processing_conf=processing_conf, buffersize_margin=backward_chunksize) processing = hls.InvComp(nb_channel=nb_channel, sample_rate=sample_rate, dtype='float32', **processing_conf) online_arrs = hls.run_instance_offline( processing, in_buffer, chunksize, sample_rate, dtype='float32', buffersize_margin=backward_chunksize, time_stats=True) freq_band = 4 online_dyngain = online_arrs['dyngain'] online_pgc2 = online_arrs['pgc2'] offline_pgc2 = online_pgc2.copy() n = processing.nb_freq_band for i in range(n): offline_pgc2[:, i] = scipy.signal.sosfilt( processing.coefficients_pgc[i, :, :], online_dyngain[::-1, i])[::-1] online_pgc2 = online_pgc2[:-backward_chunksize] offline_pgc2 = offline_pgc2[:-backward_chunksize] residual = np.abs( (online_pgc2.astype('float64') - offline_pgc2.astype('float64')) / np.mean(np.abs(offline_pgc2.astype('float64')), axis=0)) print(np.max(residual, axis=0)) print(np.max(residual)) #~ freq_band = 4 fig, ax = plt.subplots(nrows=2, sharex=True) #~ ax[0].plot(online_hpaf[:, freq_band], color = 'b') ax[0].plot(offline_pgc2[:, freq_band], color='g') ax[0].plot(online_pgc2[:, freq_band], color='r', ls='--') ax[1].plot(residual[:, freq_band], color='m') ax[1].set_ylabel('residual for band {:0.2f}'.format( processing.freqs[freq_band])) for i in range(nloop): ax[1].axvline(i * chunksize) plt.show() # TODO make one values per band assert np.max(residual) < 2e-2, 'hpaf online differt from offline'
def test_dyngain(): """ For testing dynamic gain we take coefficient with only one level so it is dynamic with alwas the same coefficient. """ assert nb_channel == 1 #~ in_buffer = hls.moving_sinus(length, sample_rate=sample_rate, speed = .5, f1=500., f2=2000., ampl = .8) in_buffer = hls.moving_erb_noise(length) in_buffer = np.tile(in_buffer[:, None], (1, nb_channel)) loss_params = { 'left': { 'freqs': [125 * 2**i for i in range(7)], 'compression_degree': [0] * 7, 'passive_loss_db': [0] * 7 } } loss_params['right'] = loss_params['left'] processing_conf = dict(nb_freq_band=5, level_max=120, level_step=120, debug_mode=True, chunksize=chunksize, backward_chunksize=backward_chunksize, loss_params=loss_params) #~ processing, online_arrs = hls.run_class_offline(hls.InvComp, in_buffer, chunksize, sample_rate, processing_conf=processing_conf, buffersize_margin=backward_chunksize) processing = hls.InvComp(nb_channel=nb_channel, sample_rate=sample_rate, dtype='float32', **processing_conf) online_arrs = hls.run_instance_offline( processing, in_buffer, chunksize, sample_rate, dtype='float32', buffersize_margin=backward_chunksize, time_stats=True) #~ assert len(processing.levels)==1 freq_band = 2 online_pgc1 = online_arrs['pgc1'] online_dyngain = online_arrs['dyngain'] n = processing.nb_freq_band offline_dyngain = online_pgc1.copy() for i in range(n): offline_dyngain[:, i] = online_pgc1[:, i] * processing.gain_controlled[i, 0] print(processing.gain_controlled[i, 0]) residual = np.abs( (online_dyngain.astype('float64') - offline_dyngain.astype('float64')) / np.mean(np.abs(offline_dyngain.astype('float64')))) print(np.max(residual)) freq_band = 4 fig, ax = plt.subplots(nrows=2, sharex=True) #~ ax[0].plot(online_pgc1[:, freq_band], color = 'b') ax[0].plot(offline_dyngain[:, freq_band], color='g') ax[0].plot(online_dyngain[:, freq_band], color='r', ls='--') ax[1].plot(residual[:, freq_band], color='m') for i in range(nloop): ax[1].axvline(i * chunksize) plt.show() assert np.max(residual) < 2e-2, 'hpaf online differt from offline'
def test_levels(): assert nb_channel == 1 #~ in_buffer = hls.moving_sinus(length, sample_rate=sample_rate, speed = .5, f1=500., f2=2000., ampl = .8) in_buffer = hls.moving_erb_noise(length) in_buffer = np.tile(in_buffer[:, None], (1, nb_channel)) loss_params = { 'left': { 'freqs': [125 * 2**i for i in range(7)], 'compression_degree': [0] * 7, 'passive_loss_db': [0] * 7 } } loss_params['right'] = loss_params['left'] processing_conf = dict(nb_freq_band=5, level_step=10, debug_mode=True, chunksize=chunksize, backward_chunksize=backward_chunksize, loss_params=loss_params) #~ processing, online_arrs = hls.run_class_offline(hls.InvComp, in_buffer, chunksize, sample_rate, processing_conf=processing_conf, buffersize_margin=backward_chunksize) processing = hls.InvComp(nb_channel=nb_channel, sample_rate=sample_rate, dtype='float32', **processing_conf) online_arrs = hls.run_instance_offline( processing, in_buffer, chunksize, sample_rate, dtype='float32', buffersize_margin=backward_chunksize, time_stats=True) freq_band = 2 out_pgc1 = online_arrs['pgc1'] hilbert_env = np.abs(scipy.signal.hilbert(out_pgc1[:, freq_band], axis=0)) hilbert_level = 20 * np.log10(hilbert_env) + processing.calibration #~ online_levels= online_arrs['levels'][:, freq_band]*processing.level_step online_levels = online_arrs['levels'][:, freq_band] online_env = 10**((online_levels - processing.calibration) / 20.) residual = np.abs( (online_levels.astype('float64') - hilbert_level.astype('float64')) / np.mean(np.abs(online_levels.astype('float64')))) residual[:100] = 0 residual[-100:] = 0 print(np.max(residual)) #~ assert np.max(residual)<3e-2, 'levelfrom hilbert offline' fig, ax = plt.subplots(nrows=2, sharex=True) ax[0].plot(out_pgc1[:, freq_band], color='k', alpha=.8) ax[0].plot(np.abs(out_pgc1[:, freq_band]), color='k', ls='--', alpha=.8) ax[0].plot(hilbert_env, color='g', lw=2) ax[0].plot(online_env, color='r', lw=2) ax[1].plot(online_levels, color='r') ax[1].plot(hilbert_level, color='g') ax[1].set_ylabel('level dB') plt.show()
def test_pgc1(): assert nb_channel == 1 in_buffer = hls.moving_sinus(length, sample_rate=sample_rate, speed=.5, f1=500., f2=2000., ampl=.8) in_buffer = np.tile(in_buffer[:, None], (1, nb_channel)) loss_params = { 'left': { 'freqs': [125 * 2**i for i in range(7)], 'compression_degree': [0] * 7, 'passive_loss_db': [0] * 7 } } loss_params['right'] = loss_params['left'] processing_conf = dict(nb_freq_band=5, level_step=10, debug_mode=True, chunksize=chunksize, backward_chunksize=backward_chunksize, loss_params=loss_params) #~ processing, online_arrs = hls.run_class_offline(hls.InvComp, in_buffer, chunksize, sample_rate, processing_conf=processing_conf, buffersize_margin=backward_chunksize) processing = hls.InvComp(nb_channel=nb_channel, sample_rate=sample_rate, dtype='float32', **processing_conf) online_arrs = hls.run_instance_offline( processing, in_buffer, chunksize, sample_rate, dtype='float32', buffersize_margin=backward_chunksize, time_stats=True) n = processing.nb_freq_band in_buffer2 = np.tile(in_buffer, (1, processing.nb_freq_band)) online_arr = online_arrs['pgc1'] offline_arr = in_buffer2.copy() for i in range(n): offline_arr[:, i] = scipy.signal.sosfilt( processing.coefficients_pgc[i, :, :], in_buffer2[:, i]) offline_arr = offline_arr[:online_arr.shape[0]] residual = np.abs( (online_arr.astype('float64') - offline_arr.astype('float64')) / np.mean(np.abs(offline_arr.astype('float64')))) #~ print(np.max(residual)) freq_band = 4 fig, ax = plt.subplots(nrows=2, sharex=True) #~ ax[0].plot(in_buffer2[:, freq_band], color = 'k') ax[0].plot(offline_arr[:, freq_band], color='g') ax[0].plot(online_arr[:, freq_band], color='r', ls='--') ax[1].plot(residual[:, freq_band], color='m') for i in range(nloop): ax[1].axvline(i * chunksize) plt.show() #~ print(np.argmax(residual), np.max(residual)) assert np.max( residual) < 1e-5, 'pgc1 online differt from offline {}'.format( np.max(residual))
} } processing_conf = dict(nb_freq_band=32, level_step=1, loss_params=loss_params, low_freq=100., high_freq=sample_rate * 0.45, debug_mode=False, chunksize=chunksize, backward_chunksize=backward_chunksize) #~ node, online_arrs = hls.run_one_node_offline(hls.MainProcessing, in_buffer, chunksize, #~ sample_rate, node_conf=node_conf, buffersize_margin=backward_chunksize) for _class in [hls.InvCGC, hls.InvComp]: #~ for _class in [hls.InvComp, hls.InvComp2,]: #~ for _class in [hls.InvComp,]: #~ for _class in [hls.InvComp2,]: print() print(_class.__name__, sample_rate, chunksize, backward_chunksize, nb_channel, processing_conf['nb_freq_band']) processing = _class(nb_channel=nb_channel, sample_rate=sample_rate, **processing_conf) online_arrs = hls.run_instance_offline( processing, in_buffer, chunksize, sample_rate, buffersize_margin=backward_chunksize, time_stats=True)
def plot_residual(): nb_channel = 1 sample_rate = 44100. chunksize = 256 #~ chunksize = 512 #~ chunksize = 1024 #~ chunksize = #~ nloop = 200 nloop = 200 nb_freq_band = 10 length = int(chunksize * nloop) in_buffer = hls.whitenoise( length, sample_rate=sample_rate, ) in_buffer = np.tile(in_buffer[:, None], (1, nb_channel)) #~ lost_chunksize = np.linspace(0,1024, 5).astype(int) lost_chunksize = np.arange(7).astype(int) * chunksize #~ backward_chunksizes = [512,1024,1536,2048] #~ backward_chunksizes = [1024,1536,2048] #~ backward_chunksizes = np.linspace(1024,2048, 5).astype(int) backward_chunksizes = lost_chunksize + chunksize all_mean_residuals = np.zeros((len(backward_chunksizes), nb_freq_band)) all_max_residuals = np.zeros((len(backward_chunksizes), nb_freq_band)) for i, backward_chunksize in enumerate(backward_chunksizes): print('backward_chunksize', backward_chunksize) loss_params = { 'left': { 'freqs': [125., 250., 500., 1000., 2000., 4000., 8000.], 'compression_degree': [0., 0., 0., 0., 0., 0., 0.], 'passive_loss_db': [0., 0., 0., 0., 0., 0., 0.], } } processing_conf = dict(nb_freq_band=nb_freq_band, low_freq=40., high_freq=500., level_max=100, level_step=100, debug_mode=True, chunksize=chunksize, backward_chunksize=backward_chunksize, loss_params=loss_params) processing = hls.InvCGC(nb_channel=nb_channel, sample_rate=sample_rate, dtype='float32', **processing_conf) online_arrs = hls.run_instance_offline( processing, in_buffer, chunksize, sample_rate, dtype='float32', buffersize_margin=backward_chunksize) #~ processing, online_arrs = hls.run_one_class_offline(hls.InvCGC, in_buffer, chunksize, sample_rate, processing_conf=processing_conf, buffersize_margin=backward_chunksize) #~ freq_band = 2 online_hpaf = online_arrs['hpaf'] online_pgc2 = online_arrs['pgc2'] offline_pgc2 = online_pgc2.copy() n = processing.nb_freq_band for b in range(n): offline_pgc2[:, b] = scipy.signal.sosfilt( processing.coefficients_pgc[b, :, :], online_hpaf[::-1, b])[::-1] online_pgc2 = online_pgc2[:-backward_chunksize] offline_pgc2 = offline_pgc2[:-backward_chunksize] residual = np.abs( (online_pgc2.astype('float64') - offline_pgc2.astype('float64')) / np.mean(np.abs(offline_pgc2.astype('float64')), axis=0)) all_mean_residuals[i, :] = np.mean(residual, axis=0) all_max_residuals[i, :] = np.max(residual, axis=0) def my_imshow(m, ax): im = ax.imshow(m, interpolation='nearest', origin='lower', aspect='auto', cmap='viridis') #, extent = extent, cmap=cmap) im.set_clim(0, 0.05) ax.set_xticks(np.arange(processing.freqs.size)) ax.set_xticklabels(['{:0.0f}'.format(f) for f in processing.freqs]) ax.set_yticks(np.arange(len(backward_chunksizes))) ax.set_yticklabels(['{}'.format(f) for f in lost_chunksize]) ax.set_xlabel('freq') ax.set_ylabel('lost_chunksize') return im print(all_max_residuals) fig, axs = plt.subplots(nrows=2, sharex=True) im1 = my_imshow(all_mean_residuals, axs[0]) im2 = my_imshow(all_max_residuals, axs[1]) cax = fig.add_axes([0.92, 0.05, .02, 0.9]) fig.colorbar(im1, ax=axs[0], cax=cax, orientation='vertical') plt.show()