Example #1
0
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.InvCGC,
        in_buffer,
        chunksize,
        sample_rate,
        processing_conf=processing_conf,
        buffersize_margin=backward_chunksize)

    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()
Example #2
0
    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)
Example #3
0
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)
    
    #~ 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 setup_files():
    json.dump(params, open(path + 'params.json', 'w'), indent=4)

    globals().update(params)
    length = nloop * chunksize

    in_buffer = hls.moving_erb_noise(length,
                                     sample_rate=sample_rate,
                                     speed=.5,
                                     f1=80.,
                                     f2=1000.,
                                     ampl=.8)
    in_buffer = np.tile(in_buffer[:, None], (1, nb_channel))
    with open(path + 'sound.raw', mode='wb') as f:
        f.write(in_buffer.tobytes())

    in_buffer2 = np.fromstring(open(path + 'sound.raw', mode='rb').read(),
                               dtype='float32').reshape(length, nb_channel)
    helper.assert_arrays_equal(in_buffer, in_buffer2)
Example #5
0
chunksize = 256
#~ chunksize = 512
#~ chunksize = 1024
#~ chunksize = 2048
#~ backward_chunksize = chunksize*5
#~ backward_chunksize = chunksize*4

lost_chunksize = 1024
backward_chunksize = lost_chunksize + chunksize

nloop = 1000

length = int(chunksize * nloop)

in_buffer = hls.moving_erb_noise(length, sample_rate=sample_rate)
#~ 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., 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.],
import hearinglosssimulator as hls
import numpy as np

if __name__ == '__main__':
    from matplotlib import pyplot
    length = 512 * 1000
    #~ hls.play_with_vlc(hls.crossing_chirp(length))
    #~ hls.play_with_vlc(hls.several_sinus(length, freqs = [440.]))
    #~ hls.play_with_vlc(hls.moving_sinus(length))
    #~ hls.play_with_vlc(hls.whitenoise(length))
    #~ hls.play_with_vlc(hls.notchnoise(length))
    #~ hls.play_with_vlc(hls.whitenoise(length)+hls.moving_sinus(length)*.3)
    #~ hls.play_with_vlc(hls.bandfiltered_noise(length))
    #~ hls.play_with_vlc(hls.erb_noise(length))
    #~ hls.play_with_vlc(hls.moving_erb_noise(length))
    hls.play_with_vlc(
        hls.moving_erb_noise(length, trajectorytype='sinus', speed=.1))
    #~ hls.play_with_vlc(hls.moving_mask_around_tone(length))

    #~ sound = hls.moving_mask_around_tone(length)
    #~ sound = moving_erb_noise(length,  trajectorytype='triangle',  f1 = 400, f2 = 1000, speed = .01, ampl = .1)
    #~ sound = notchnoise(length)

    #~ fig, ax = pyplot.subplots()
    #~ ax.plot(sound)
    #~ fig, ax = pyplot.subplots()
    #~ ax.specgram(sound, NFFT=1024, Fs=44100, noverlap=512,)
    #~ fig, ax = pyplot.subplots()
    #~ ax.psd(sound, Fs=44100, NFFT = 2**12, color = 'b')
    #~ pyplot.show()