Example #1
0
def test_energy():
    """
    Test of the relation between the energy of hammerstein group model and simple hammerstein model.
    (a + b)^2 <= 2a^2 + 2b^2
    The above relation is proved for the model using this test
    """
    max_harm = [1]*2
    freq = 5000
    s_rate = 48000
    length = s_rate
    ip_sine_signal = sumpf.modules.SineWaveGenerator(frequency=freq,
                                          phase=0.0,
                                          samplingrate=s_rate,
                                          length=length).GetSignal()
    imp = sumpf.modules.ImpulseGenerator(samplingrate=s_rate, length=length).GetSignal()
    e = []
    for harm in max_harm:
        Test_Model_Hammerstein = nlsp.AliasingCompensatedHM_lowpass(input_signal=ip_sine_signal,
                                                       nonlin_func=nlsp.function_factory.power_series(harm),
                                                       filter_impulseresponse=imp,
                                                       max_harm=harm)
        Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput()
        e.append(numpy.multiply(nlsp.calculateenergy_freq(Test_Model_outputsignal),2))
    hammerstein_group = nlsp.HammersteinGroupModel_lp(input_signal=ip_sine_signal,
                                                   nonlinear_functions=(nlsp.function_factory.power_series(max_harm[0]),
                                                                        )*len(max_harm),
                                                   filter_irs=(imp,)*len(max_harm),
                                                   max_harmonics=max_harm)
    e_g = nlsp.calculateenergy_freq(hammerstein_group.GetOutput())
    assert float(numpy.sum(e_g)) <= float(numpy.sum(e))
Example #2
0
def test_puretone():
    """
    Tests whether hammerstein group model produces reliable result for pure sine tone.
    The pure sine tone is given to hammerstein model of different maximum harmonic alias compensation and the output
    freq are found for each branch. Then the same pure sine tone is given to hammerstein group model and the output
    is observed. It should have the same frequencies which we get from individual hammerstein model.
    """
    max_harm = [1,2,3,4,5]
    freq = 8000
    s_rate = 48000
    length = s_rate
    ip_sine_signal = sumpf.modules.SineWaveGenerator(frequency=freq,
                                          phase=0.0,
                                          samplingrate=s_rate,
                                          length=length).GetSignal()
    h = []
    for harm in max_harm:
        Test_Model_Hammerstein = nlsp.AliasingCompensatedHM_lowpass(input_signal=ip_sine_signal,
                                                       nonlin_func=nlsp.function_factory.power_series(harm),
                                                       max_harm=harm)
        Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput()
        h.append(nlsp.find_frequencies(Test_Model_outputsignal))
    Test_model_freq = sorted(list(set([item for sublist in h for item in sublist])))
    imp = sumpf.modules.ImpulseGenerator(samplingrate=s_rate, length=length).GetSignal()
    hammerstein_group = nlsp.HammersteinGroupModel_lp(input_signal=ip_sine_signal,
                                                   nonlinear_functions=(nlsp.function_factory.power_series(max_harm[0]),
                                                   nlsp.function_factory.power_series(max_harm[1]),
                                                   nlsp.function_factory.power_series(max_harm[2]),
                                                   nlsp.function_factory.power_series(max_harm[3]),
                                                   nlsp.function_factory.power_series(max_harm[4])),
                                                   filter_irs=(imp,)*len(max_harm),
                                                   max_harmonics=max_harm)
    Test_groupmodel_freq = nlsp.find_frequencies(hammerstein_group.GetOutput())
    assert Test_groupmodel_freq == Test_model_freq
Example #3
0
def test_aliasing():
    """
    Tests whether aliasing is present in the hammerstein group model.
    The pure sine tone is given to the model and the output frequencies found in all the branches are found. The energy
    of these frequencies in the output of the hammerstein group model is calculated. If there is no aliasing then this
    should be equal to the total energy of the output signal.
    """
    max_harm = [1,2,3,4,5]
    freq = 5000
    s_rate = 48000
    length = s_rate
    ip_sine_signal = sumpf.modules.SineWaveGenerator(frequency=freq,
                                          phase=0.0,
                                          samplingrate=s_rate,
                                          length=length).GetSignal()
    h = []
    for harm in max_harm:
        Test_Model_Hammerstein = nlsp.AliasingCompensatedHM_lowpass(input_signal=ip_sine_signal,
                                                       nonlin_func=nlsp.function_factory.power_series(harm),
                                                       max_harm=harm)
        Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput()
        h.append(nlsp.find_frequencies(Test_Model_outputsignal))
    Test_model_freq = sorted(list(set([item for sublist in h for item in sublist])))
    imp = sumpf.modules.ImpulseGenerator(samplingrate=s_rate, length=length).GetSignal()
    hammerstein_group = nlsp.HammersteinGroupModel_lp(input_signal=ip_sine_signal,
                                                   nonlinear_functions=(nlsp.function_factory.power_series(max_harm[0]),
                                                   nlsp.function_factory.power_series(max_harm[1]),
                                                   nlsp.function_factory.power_series(max_harm[2]),
                                                   nlsp.function_factory.power_series(max_harm[3]),
                                                   nlsp.function_factory.power_series(max_harm[4])),
                                                   filter_irs=(imp,)*len(max_harm),
                                                   max_harmonics=max_harm)
    Test_groupmodel_energy_freq = nlsp.calculateenergy_atparticularfrequencies(hammerstein_group.GetOutput(),
                                                                            frequencies=Test_model_freq)
    Test_groupmodel_energy_all = nlsp.calculateenergy_freq(hammerstein_group.GetOutput())
    assert int(numpy.sum(Test_groupmodel_energy_all)) == int(numpy.sum(Test_groupmodel_energy_freq))
def sweep_evaluation_power():
    """
    Evaluation of Hammerstein group models using sweep
    Nonlinear system: hgm with same branch and same nl and linear functions
    input: sweep of certain length
    the SNR and MSE between the expected and identified output is evaluated
    """
    # virtual nonlinear systems for evaluation
    # nlsystem = nlsp.HammersteinGroupModel_up(nonlinear_functions=nonlinear_functions_power,
    #                                           filter_irs=filter_spec_tofind,
    #                                           max_harmonics=max_harmonics)
    nlsystem = nlsp.HammersteinGroupModel_lp(
        nonlinear_functions=nonlinear_functions_power,
        filter_irs=filter_spec_tofind,
        max_harmonics=max_harmonics,
        filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH(order=100),
        attenuation=0.01)
    # nlsystem = nlsp.HammersteinGroupModel(nonlinear_functions=nonlinear_functions_power,
    #                                           filter_irs=filter_spec_tofind)
    nlsystem.SetInput(input_sweep)

    # system identification of the nonlinear system
    found_filter_spec = nlsp.nonlinearconvolution_powerseries_filter(
        input_sweep, nlsystem.GetOutput(),
        [sweep_start_freq, sweep_stop_freq, branches])

    # construct hammerstein group model from identified parameters
    hgm_up = nlsp.HammersteinGroupModel_up(
        nonlinear_functions=nonlinear_functions_power,
        filter_irs=found_filter_spec,
        max_harmonics=max_harmonics)
    hgm_lp = nlsp.HammersteinGroupModel_lp(
        nonlinear_functions=nonlinear_functions_power,
        filter_irs=found_filter_spec,
        max_harmonics=max_harmonics,
        filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH(order=100),
        attenuation=0.01)
    hgm = nlsp.HammersteinGroupModel(
        nonlinear_functions=nonlinear_functions_power,
        filter_irs=found_filter_spec)
    hgm_up.SetInput(input_sweep)
    hgm.SetInput(input_sweep)
    hgm_lp.SetInput(input_sweep)

    # print the mean square error value
    print "SNR of nl model: %r and identified system: %r and btw nl and iden system,simple: %r" \
          %(nlsp.signal_to_noise_ratio_freq_range(input_sweep,nlsystem.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(input_sweep,hgm.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(nlsystem.GetOutput(),hgm.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]))
    print "MSE of nl model: %r and identified system: %r and btw nl and iden system,simple: %r" \
          %(nlsp.signal_to_noise_ratio_freq_rangesignal_to_noise_ratio_freq_range(input_sweep,nlsystem.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(input_sweep,hgm.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(nlsystem.GetOutput(),hgm.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]))
    print "SNR of nl model: %r and identified system: %r and btw nl and iden system,upsample: %r" \
          %(nlsp.signal_to_noise_ratio_freq_range(input_sweep,nlsystem.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(input_sweep,hgm_up.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(nlsystem.GetOutput(),hgm_up.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]))
    print "MSE of nl model: %r and identified system: %r and btw nl and iden system,upsample: %r" \
          %(nlsp.signal_to_noise_ratio_freq_range(input_sweep,nlsystem.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(input_sweep,hgm_up.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(nlsystem.GetOutput(),hgm_up.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]))
    print "SNR of nl model: %r and identified system: %r and btw nl and iden system,lowpass: %r" \
          %(nlsp.signal_to_noise_ratio_freq_range(input_sweep,nlsystem.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(input_sweep,hgm_lp.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(nlsystem.GetOutput(),hgm_lp.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]))
    print "MSE of nl model: %r and identified system: %r and btw nl and iden system,lowpass: %r" \
          %(nlsp.signal_to_noise_ratio_freq_range(input_sweep,nlsystem.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(input_sweep,hgm_lp.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]),
            nlsp.signal_to_noise_ratio_freq_range(nlsystem.GetOutput(),hgm_lp.GetOutput(),[sweep_start_freq+100,sweep_stop_freq-100]))
def harmonics_difference_chebyshev_evaluation():

    # the nonlinear reference system
    nlsystem = nlsp.HammersteinGroupModel(
        nonlinear_functions=nonlinear_functions_chebyshev,
        filter_irs=filter_spec_tofind)
    nlsystem_up = nlsp.HammersteinGroupModel_up(
        nonlinear_functions=nonlinear_functions_chebyshev,
        filter_irs=filter_spec_tofind,
        max_harmonics=max_harmonics)
    nlsystem_lp = nlsp.HammersteinGroupModel_lp(
        nonlinear_functions=nonlinear_functions_chebyshev,
        filter_irs=filter_spec_tofind,
        max_harmonics=max_harmonics,
        filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH(order=100),
        attenuation=0.0001)
    nlsystem.SetInput(input_sweep)
    nlsystem_up.SetInput(input_sweep)
    nlsystem_lp.SetInput(input_sweep)

    # identification method to find the nonlinear system parameters
    found_filter_spec = nlsp.nonlinearconvolution_chebyshev_filter(
        input_sweep, nlsystem.GetOutput(),
        [sweep_start_freq, sweep_stop_freq, branches])
    found_filter_spec_up = nlsp.nonlinearconvolution_powerseries_filter(
        input_sweep, nlsystem.GetOutput(),
        [sweep_start_freq, sweep_stop_freq, branches])
    found_filter_spec_lp = nlsp.nonlinearconvolution_powerseries_filter(
        input_sweep, nlsystem.GetOutput(),
        [sweep_start_freq, sweep_stop_freq, branches])

    # construct hgm using identified parameters
    hgm_up = nlsp.HammersteinGroupModel_up(
        nonlinear_functions=nonlinear_functions_chebyshev,
        filter_irs=found_filter_spec_up,
        max_harmonics=max_harmonics)
    hgm_up.SetInput(input_sweep)
    hgm_lp = nlsp.HammersteinGroupModel_lp(
        nonlinear_functions=nonlinear_functions_chebyshev,
        filter_irs=found_filter_spec_lp,
        max_harmonics=max_harmonics,
        filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH(order=100),
        attenuation=0.0001)
    hgm_lp.SetInput(input_sweep)
    hgm = nlsp.HammersteinGroupModel(
        nonlinear_functions=nonlinear_functions_chebyshev,
        filter_irs=found_filter_spec)
    hgm.SetInput(input_sweep)

    # calculate the harmonics from the input and output using helper function
    hgm_harmonics = nlsp.get_sweep_harmonics_spectrum(input_sweep,
                                                      hgm.GetOutput(),
                                                      sweep_start_freq,
                                                      sweep_stop_freq,
                                                      branches)
    hgm_lp_harmonics = nlsp.get_sweep_harmonics_spectrum(
        input_sweep, hgm_lp.GetOutput(), sweep_start_freq, sweep_stop_freq,
        branches)
    hgm_up_harmonics = nlsp.get_sweep_harmonics_spectrum(
        input_sweep, hgm_up.GetOutput(), sweep_start_freq, sweep_stop_freq,
        branches)
    nlsystem_harmonics = nlsp.get_sweep_harmonics_spectrum(
        input_sweep, nlsystem.GetOutput(), sweep_start_freq, sweep_stop_freq,
        branches)
    nlsystem_lp_harmonics = nlsp.get_sweep_harmonics_spectrum(
        input_sweep, nlsystem_lp.GetOutput(), sweep_start_freq,
        sweep_stop_freq, branches)
    nlsystem_up_harmonics = nlsp.get_sweep_harmonics_spectrum(
        input_sweep, nlsystem_up.GetOutput(), sweep_start_freq,
        sweep_stop_freq, branches)

    # calculate the difference between the observed and identified harmonics
    simple_hgm_diff = nlsp.cut_spectrum(hgm_harmonics - nlsystem_harmonics,
                                        [100, 19900])
    lp_hgm_diff = nlsp.cut_spectrum(hgm_lp_harmonics - nlsystem_lp_harmonics,
                                    [100, 19900])
    up_hgm_diff = nlsp.cut_spectrum(hgm_up_harmonics - nlsystem_up_harmonics,
                                    [100, 19900])

    # plot the difference
    plot.log()
    print "Simple hammerstein model harmonics plot"
    plot.plot(simple_hgm_diff)
    print "Lowpass hammerstein model harmonics plot"
    plot.plot(lp_hgm_diff)
    print "Upsampling hammerstein model harmonics plot"
    plot.plot(up_hgm_diff)