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.InvCGC, in_buffer, chunksize, sample_rate, processing_conf=processing_conf, buffersize_margin=backward_chunksize) freq_band = 4 online_hpaf = online_arrs['hpaf'] 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_hpaf[::-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'
import hearinglosssimulator as hls import numpy as np sample_rate = 44100. duration = 4. #s length = int(sample_rate * duration) nb_channel = 2 # there are some helper for creating in_sounds in hls.in_soundgenerators in_sound = hls.whitenoise( length, sample_rate=sample_rate, ) #~ in_sound = hls.moving_erb_noise(length, trajectorytype='sinus', speed = .1) # the shape must (length, nb_channel) so in_sound = np.tile(in_sound[:, None], (1, nb_channel)) # define loss parameters 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.], } }
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()