def test_inputparameters_of_modifymodel():
    """
    Test the input constructors of the ModifyModel class.
    """
    branches = 3
    signal = sumpf.modules.SweepGenerator(samplingrate=48000,
                                          length=2**14).GetSignal()
    ref_nlfunctions = [
        nlsp.nonlinear_functions.Power(degree=i + 1) for i in range(branches)
    ]
    ref_bpfilters = nlsp.helper_functions.create_arrayof_bpfilter(
        branches=branches, sampling_rate=48000)
    ref_aliasingcomp = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(
    )
    ref_HGM = nlsp.HammersteinGroupModel(
        nonlinear_functions=ref_nlfunctions,
        filter_impulseresponses=ref_bpfilters,
        aliasing_compensation=ref_aliasingcomp)
    test_HGM = nlsp.HammersteinGroupModel()
    ref_HGM.SetInput(signal)
    test_HGM.SetInput(signal)
    assert nlsp_private.calculateenergy_timedomain(ref_HGM.GetOutput()) != \
           nlsp_private.calculateenergy_timedomain(test_HGM.GetOutput())
    modify_model = nlsp.ModifyModel(input_model=test_HGM,
                                    filter_impulseresponses=ref_bpfilters,
                                    nonlinear_functions=ref_nlfunctions,
                                    aliasing_compensation=ref_aliasingcomp)
    modified_HGM = modify_model.GetOutputModel()
    modified_HGM.SetInput(signal)
    assert nlsp_private.calculateenergy_timedomain(ref_HGM.GetOutput()) == \
           nlsp_private.calculateenergy_timedomain(modified_HGM.GetOutput())
def test_methods_savemodel():
    """
    Test the methods of the SaveHGMModel class.
    """
    branches = 5
    location1 = "C:\Users\diplomand.8\Desktop\some1.npz"
    location2 = "C:\Users\diplomand.8\Desktop\some2.npz"
    ref_filters = nlsp.helper_functions.create_arrayof_bpfilter(
        branches=branches)
    ref_nl = [
        nlsp.nonlinear_function.Power(degree=i + 1) for i in range(branches)
    ]
    ref_aliasing = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(
    )
    model1 = nlsp.HammersteinGroupModel()
    model2 = nlsp.HammersteinGroupModel(nonlinear_functions=ref_nl,
                                        filter_impulseresponses=ref_filters,
                                        aliasing_compensation=ref_aliasing)
    save_model = nlsp.SaveHGMModel(filename=location1, model=model1)
    save_model.SetFilename(location2)
    save_model.SetModel(model=model1)
    model1_retrieved = nlsp.RetrieveHGMModel(filename=location1).GetModel()
    model2_retrieved = nlsp.RetrieveHGMModel(filename=location2).GetModel()
    sample = sumpf.modules.NoiseGenerator().GetSignal()
    model1.SetInput(sample)
    model1_retrieved.SetInput(sample)
    evaluation = nlsp.evaluations.CompareWithReference(
        reference_signal=model1.GetOutput(),
        signal_to_be_evaluated=model1_retrieved.GetOutput())
    assert evaluation.GetSignaltoErrorRatio() > 500
    evaluation.SetReferenceOutput(model2.GetOutput())
    evaluation.SetIdentifiedOutput(model2_retrieved.GetOutput())
    assert evaluation.GetSignaltoErrorRatio() > 500
    os.remove(location1)
    os.remove(location2)
def test_modify_model_SetAliasingCompensation():
    """
    Test the SetAliasingCompensation method of the ModifyModel class.
    """
    branches = 3
    input_signal = sumpf.modules.SweepGenerator(samplingrate=48000.0,
                                                length=2**14).GetSignal()
    nonlinear_functions = [
        nlsp.nonlinear_functions.Power(degree=i + 1) for i in range(branches)
    ]
    filter_irs = nlsp.helper_functions.create_arrayof_bpfilter(
        branches=branches, sampling_rate=48000.0)
    downsampling_posititon = nlsp.HammersteinGroupModel.AFTERNONLINEARBLOCK
    aliasing_compensation = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(
    )
    HGM = nlsp.HammersteinGroupModel(
        nonlinear_functions=nonlinear_functions,
        filter_impulseresponses=filter_irs,
        aliasing_compensation=aliasing_compensation,
        downsampling_position=downsampling_posititon)
    HGM.SetInput(input_signal=input_signal)
    test_HGM = nlsp.HammersteinGroupModel(
        downsampling_position=downsampling_posititon)
    test_HGM.SetInput(input_signal)
    energy1 = nlsp_private.calculateenergy_timedomain(HGM.GetOutput())[0]
    energy2 = nlsp_private.calculateenergy_timedomain(test_HGM.GetOutput())[0]
    assert energy1 != energy2
    modify_model = nlsp.ModifyModel(
        input_model=test_HGM,
        filter_impulseresponses=filter_irs,
        nonlinear_functions=nonlinear_functions,
        aliasing_compensation=nlsp.aliasing_compensation.
        NoAliasingCompensation(),
        downsampling_position=downsampling_posititon)
    modified_HGM = modify_model.GetOutputModel()
    modified_HGM.SetInput(input_signal)
    energy1 = nlsp_private.calculateenergy_timedomain(HGM.GetOutput())[0]
    energy2 = nlsp_private.calculateenergy_timedomain(
        modified_HGM.GetOutput())[0]
    assert energy1 != energy2
    modify_model.SetAliasingCompensation(aliasing_compensation)
    modified_HGM = modify_model.GetOutputModel()
    modified_HGM.SetInput(input_signal)
    energy1 = nlsp_private.calculateenergy_timedomain(HGM.GetOutput())[0]
    energy2 = nlsp_private.calculateenergy_timedomain(
        modified_HGM.GetOutput())[0]
    distance = energy1 - energy2
    distance = abs(distance)
    assert distance < 100
 def __init__(self, model_1=None, model_2=None):
     """
     :param model_1: the first model
     :type model_1: nlsp.models
     :param model_2: the second model
     :type model_2: nlsp.models
     """
     if model_1 is None:
         self._model_1 = nlsp.HammersteinGroupModel()
     else:
         self._model_1 = model_1
     if model_2 is None:
         self._model_2 = nlsp.HammersteinGroupModel()
     else:
         self._model_2 = model_2
def test_modify_model_SetFilterImpulseResponses():
    """
    Test the SetFilterImpulseResponses method of the ModifyModel class.
    """
    branches = 3
    downsampling_posititon = nlsp.HammersteinGroupModel.AFTERNONLINEARBLOCK
    input_signal = sumpf.modules.SweepGenerator(samplingrate=48000,
                                                length=2**15).GetSignal()
    ref_nlfunctions1 = [
        nlsp.nonlinear_functions.Power(degree=i + 1) for i in range(branches)
    ]
    ref_nlfunctions2 = [
        nlsp.nonlinear_functions.Power(degree=i + 1) for i in range(branches)
    ]
    ref_bpfilters = nlsp.helper_functions.create_arrayof_bpfilter(
        branches=branches, sampling_rate=48000)
    ref_aliasingcomp1 = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(
    )
    ref_aliasingcomp2 = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(
    )
    ref_HGM = nlsp.HammersteinGroupModel(
        nonlinear_functions=ref_nlfunctions1,
        filter_impulseresponses=ref_bpfilters,
        aliasing_compensation=ref_aliasingcomp1,
        downsampling_position=downsampling_posititon)
    test_HGM = nlsp.HammersteinGroupModel(
        downsampling_position=downsampling_posititon)
    ref_HGM.SetInput(input_signal)
    test_HGM.SetInput(input_signal)
    assert nlsp_private.calculateenergy_timedomain(ref_HGM.GetOutput()) != \
           nlsp_private.calculateenergy_timedomain(test_HGM.GetOutput())
    rev_bp = [i for i in reversed(ref_bpfilters)]
    modify_model = nlsp.ModifyModel(input_model=test_HGM,
                                    aliasing_compensation=ref_aliasingcomp2,
                                    nonlinear_functions=ref_nlfunctions2,
                                    filter_impulseresponses=rev_bp)
    modified_HGM = modify_model.GetOutputModel()
    modified_HGM.SetInput(input_signal)
    assert nlsp_private.calculateenergy_timedomain(ref_HGM.GetOutput()) != \
           nlsp_private.calculateenergy_timedomain(modified_HGM.GetOutput())
    modify_model.SetFilterImpulseResponses(
        filter_impulseresponses=ref_bpfilters)
    modified_HGM = modify_model.GetOutputModel()
    modified_HGM.SetInput(input_signal)
    energy1 = nlsp_private.calculateenergy_timedomain(ref_HGM.GetOutput())[0]
    energy2 = nlsp_private.calculateenergy_timedomain(
        modified_HGM.GetOutput())[0]
    assert int(energy1) == int(energy2)
 def __init__(self,
              reference_system=None,
              identification_algorithms=None,
              test_signal=None):
     """
     :param reference_system: the reference system object
     :param identification_algorithm: the identification algorithm object
     :param test_signal: the test signal which is used for evaluation
     """
     if reference_system is None:
         self._reference_system = nlsp.HammersteinGroupModel(
             nonlinear_functions=[
                 nlsp.nonlinear_function.Power(degree=i + 1)
                 for i in range(5)
             ],
             filter_impulseresponses=nlsp.helper_functions.
             create_arrayof_bpfilter(),
             aliasing_compensation=nlsp.aliasing_compensation.
             ReducedUpsamplingAliasingCompensation())
     else:
         self._reference_system = reference_system
     self._identification_algorithms = identification_algorithms
     if test_signal is None:
         self._test_signal = sumpf.modules.NoiseGenerator(
             distribution=sumpf.modules.NoiseGenerator.LaplaceDistribution(
             ),
             seed="seed").GetSignal()
     else:
         self._test_signal = test_signal
def test_savemodel_withclippingfunction():
    branches = 2
    artificial_location_adaptive = "O:\\Diplomanden\\Logeshwaran.Thamilselvan\\Loudspeaker nonlinearity\\models\\artificialadaptive.npz"
    method = 'Nelder-Mead'
    ref_hgm = nlsp.HammersteinGroupModel(
        nonlinear_functions=[
            nlsp.nonlinear_function.Power(degree=i) for i in range(branches)
        ],
        filter_impulseresponses=nlsp.helper_functions.
        create_arrayof_simplefilter(branches=branches),
        aliasing_compensation=nlsp.aliasing_compensation.
        ReducedUpsamplingAliasingCompensation())
    adaptive_identification = nlsp.system_identification.ClippingAdaptive(
        select_branches=range(1, branches + 1),
        excitation_length=2**15,
        thresholds=[[-1.0, 1.0], [-0.9, 0.9]])
    excitation = adaptive_identification.GetExcitation()
    ref_hgm.SetInput(excitation)
    adaptive_identification.SetResponse(response=ref_hgm.GetOutput())
    output_model_adaptive = adaptive_identification.GetOutputModel()
    save_adaptive = nlsp.SaveHGMModel(filename=artificial_location_adaptive,
                                      model=output_model_adaptive)
    iden_hgm = nlsp.RetrieveHGMModel(
        filename=artificial_location_adaptive).GetModel()
    os.remove(artificial_location_adaptive)
def test_additioninHGM():
    """
    Construct different HM's and construct a HGM with the same parameters of the HM's. The output of the sum of HM's and
    the output of the HGM should be equal.
    """
    sweep = sumpf.modules.SweepGenerator(start_frequency=20.0, stop_frequency=20000.0, samplingrate=48000.0,
                                         length=2 ** 14).GetSignal()
    nonlinear_functions = [nlsp.nonlinear_function.Power(degree=1), nlsp.nonlinear_function.Power(degree=2),
                           nlsp.nonlinear_function.Power(degree=3)]
    nonlinear_functions1 = [nlsp.nonlinear_function.Power(degree=1), nlsp.nonlinear_function.Power(degree=2),
                            nlsp.nonlinear_function.Power(degree=3)]
    aliasing_compensation1 = nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation()
    aliasing_compensation2 = nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation()
    aliasing_compensation3 = nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation()
    aliasing_compensation4 = nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation()
    model1 = nlsp.HammersteinModel(input_signal=sweep, nonlinear_function=nonlinear_functions[0],
                                   aliasing_compensation=aliasing_compensation1)
    model2 = nlsp.HammersteinModel(input_signal=sweep, nonlinear_function=nonlinear_functions[1],
                                   aliasing_compensation=aliasing_compensation2)
    model3 = nlsp.HammersteinModel(input_signal=sweep, nonlinear_function=nonlinear_functions[2],
                                   aliasing_compensation=aliasing_compensation3)
    total_output = model1.GetOutput() + model2.GetOutput() + model3.GetOutput()
    HGM = nlsp.HammersteinGroupModel(input_signal=sweep, nonlinear_functions=nonlinear_functions1,
                                     aliasing_compensation=aliasing_compensation4)
    HGM_output = HGM.GetOutput()
    assert nlsp.common.helper_functions_private.calculateenergy_timedomain(total_output) == \
           nlsp.common.helper_functions_private.calculateenergy_timedomain(HGM_output)
def test_HGM_withmultichannelinput():
    """
    Test the HGM with multichannel inputs.
    """
    sampling_rate = 48000
    length = 2 ** 16
    branches = 3
    input_signal_1 = sumpf.modules.SweepGenerator(samplingrate=sampling_rate, length=length).GetSignal()
    input_signal_2 = sumpf.modules.NoiseGenerator(samplingrate=sampling_rate, length=length).GetSignal()
    combined_signal = sumpf.modules.MergeSignals(signals=[input_signal_1, input_signal_2]).GetOutput()
    linear_kernels = nlsp.helper_functions.create_arrayof_bpfilter(branches=2, sampling_rate=sampling_rate)
    linear_kernels = sumpf.modules.MergeSignals(signals=linear_kernels).GetOutput()
    HGM = nlsp.HammersteinGroupModel(
        nonlinear_functions=[nlsp.nonlinear_function.Power(i + 1) for i in range(branches)],
        aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(),
        filter_impulseresponses=[linear_kernels, ] * branches)
    HGM.SetInput(combined_signal)
    combined_output = HGM.GetOutput()
    HGM.SetInput(input_signal_1)
    output_1 = HGM.GetOutput()
    HGM.SetInput(input_signal_2)
    output_2 = HGM.GetOutput()
    energy_1 = nlsp.common.helper_functions_private.calculateenergy_timedomain(output_1)
    energy_2 = nlsp.common.helper_functions_private.calculateenergy_timedomain(output_2)
    combined_energy = nlsp.common.helper_functions_private.calculateenergy_timedomain(combined_output)
    assert energy_1[0] == combined_energy[0]
    assert energy_2[1] == combined_energy[1]
def test_inputandoutputmethods_HMandHGM():
    """
    Test the input and output methods of HM and HGM.
    """
    input_signal_sweep = sumpf.modules.SweepGenerator(samplingrate=48000.0, length=2 ** 14).GetSignal()
    nonlinear_function = nlsp.nonlinear_function.Power(degree=5)
    HM = nlsp.HammersteinModel(nonlinear_function=nonlinear_function,
                               aliasing_compensation=nlsp.aliasing_compensation.NoAliasingCompensation())
    HM.SetInput(input_signal_sweep)
    output1 = HM.GetOutput()
    energy1 = nlsp.common.helper_functions_private.calculateenergy_timedomain(output1)
    input_signal_sine = sumpf.modules.SineWaveGenerator(samplingrate=48000.0, length=2 ** 14).GetSignal()
    HM.SetInput(input_signal_sine)
    output2 = HM.GetOutput()
    energy2 = nlsp.common.helper_functions_private.calculateenergy_timedomain(output2)
    assert energy1 != energy2
    nonlinear_function = nonlinear_function.CreateModified()
    HGM = nlsp.HammersteinGroupModel(nonlinear_functions=[nonlinear_function, ],
                                     aliasing_compensation=nlsp.aliasing_compensation.NoAliasingCompensation())
    HGM.SetInput(input_signal_sweep)
    energy3 = nlsp.common.helper_functions_private.calculateenergy_timedomain(HGM.GetOutput())
    assert energy1 == energy3
    HGM.SetInput(input_signal_sine)
    energy4 = nlsp.common.helper_functions_private.calculateenergy_timedomain(HGM.GetOutput())
    assert energy2 == energy4
예제 #11
0
def test_identify_a_HGM_clippingvsclippingadaptive():
    """
    Test the comparison of accuracy between adaptive and clipping adaptive system identification.
    """
    branches = 2
    excitation_length = 2**14
    sampling_rate = 48000
    select_branches = range(1, branches + 1)
    aliasing_compensation = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(
    )
    linear_filters = nlsp.helper_functions.create_arrayof_bpfilter(
        branches=branches, sampling_rate=sampling_rate)
    nonlinear_functions = [
        nlsp.nonlinear_function.Power(i + 1) for i in range(branches)
    ]
    black_box = nlsp.HammersteinGroupModel(
        nonlinear_functions=nonlinear_functions,
        aliasing_compensation=aliasing_compensation,
        filter_impulseresponses=linear_filters)
    identification_algorithm_clippingadaptive = nlsp.system_identification.ClippingAdaptive(
        select_branches=select_branches,
        excitation_length=excitation_length,
        nonlinear_function=nlsp.nonlinear_function.HardClip,
        thresholds=[[-1.0, 1.0], [-0.8, 0.9]])
    identification_algorithm_adaptive = nlsp.system_identification.Adaptive(
        select_branches=select_branches,
        excitation_length=excitation_length,
        nonlinear_function=nlsp.nonlinear_function.Legendre)

    excitation_adaptive = identification_algorithm_adaptive.GetExcitation()
    black_box.SetInput(input_signal=excitation_adaptive)
    identification_algorithm_adaptive.SetResponse(
        response=black_box.GetOutput())
    model_black_box_adaptive = identification_algorithm_adaptive.GetOutputModel(
    )

    excitation_clippingadaptive = identification_algorithm_clippingadaptive.GetExcitation(
    )
    black_box.SetInput(input_signal=excitation_clippingadaptive)
    identification_algorithm_clippingadaptive.SetResponse(
        response=black_box.GetOutput())
    model_black_box_clippingadaptive = identification_algorithm_clippingadaptive.GetOutputModel(
    )

    exc = sumpf.modules.NoiseGenerator(
        distribution=sumpf.modules.NoiseGenerator.UniformDistribution(),
        samplingrate=48000,
        length=2**14,
        seed="signal")
    model_black_box_adaptive.SetInput(exc.GetSignal())
    model_black_box_clippingadaptive.SetInput(exc.GetSignal())
    black_box.SetInput(exc.GetSignal())

    evaluation_adaptive = nlsp.evaluations.CompareWithReference(
        black_box.GetOutput(), model_black_box_adaptive.GetOutput())
    evaluation_clippingadaptive = nlsp.evaluations.CompareWithReference(
        black_box.GetOutput(), model_black_box_clippingadaptive.GetOutput())

    assert evaluation_clippingadaptive.GetSignaltoErrorRatio(
    )[0] > evaluation_adaptive.GetSignaltoErrorRatio()[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.HammersteinModel(
            input_signal=ip_sine_signal,
            nonlin_func=nlsp.function_factory.power_series(harm),
            filter_impulseresponse=imp)
        Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput()
        e.append(
            numpy.multiply(nlsp.calculateenergy_freq(Test_Model_outputsignal),
                           2))
    hammerstein_group = nlsp.HammersteinGroupModel(
        input_signal=ip_sine_signal,
        nonlinear_functions=(nlsp.function_factory.power_series(
            max_harm[0]), ) * len(max_harm),
        filter_irs=(imp, ) * len(max_harm))
    e_g = nlsp.calculateenergy_freq(hammerstein_group.GetOutput())
    assert float(numpy.sum(e_g)) <= float(numpy.sum(e))
def test_puretone():
    """
    Tests whether hammerstein group model produces relaiable 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.HammersteinModel(
            input_signal=ip_sine_signal,
            nonlin_func=nlsp.function_factory.power_series(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(
        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))
    Test_groupmodel_freq = nlsp.find_frequencies(hammerstein_group.GetOutput())
    assert Test_groupmodel_freq == Test_model_freq
예제 #14
0
def test_accuracy_evaluation():
    """
    Test whether the accuracy evaluation class works fine.
    """
    branches = 2
    aliasing_comp = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation()
    reference_nlsystem = nlsp.HammersteinGroupModel(
        nonlinear_functions=[nlsp.nonlinear_function.Power(i + 1) for i in range(branches)],
        filter_impulseresponses=nlsp.helper_functions.create_arrayof_bpfilter(branches=branches),
        aliasing_compensation=aliasing_comp)
    identification_alg = [nlsp.system_identification.SineSweep(select_branches=range(1, branches + 1),
                                                               aliasing_compensation=aliasing_comp),
                          nlsp.system_identification.CosineSweep(select_branches=range(1, branches + 1),
                                                                 aliasing_compensation=aliasing_comp),
                          nlsp.system_identification.MISOapproach(select_branches=range(1, branches + 1),
                                                                  aliasing_compensation=aliasing_comp),
                          nlsp.system_identification.WienerGapproach(select_branches=range(1, branches + 1),
                                                                     aliasing_compensation=aliasing_comp),
                          nlsp.system_identification.Adaptive(select_branches=range(1, branches + 1),
                                                              aliasing_compensation=aliasing_comp)]
    test_signal = sumpf.modules.NoiseGenerator(distribution=sumpf.modules.NoiseGenerator.LaplaceDistribution(),
                                               length=2 ** 14).GetSignal()
    test_signal = sumpf.modules.MergeSignals(signals=[test_signal, test_signal]).GetOutput()
    evaluation = nlsp.evaluate_systemidentification.AccuracyEvaluation(reference_nonlinearsystem=reference_nlsystem,
                                                                       identification_algorithms=identification_alg,
                                                                       test_signal=test_signal)
    evaluation.GetAccuracy()
def test_identify_an_HGM_Cosinesweep():
    """
    Test the accuracy of the Sweep based system identification using cosine sweep signal.
    """
    branches = 2
    aliasing_compensation = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(
    )
    linear_filters = nlsp.helper_functions.create_arrayof_bpfilter(
        branches=branches, sampling_rate=48000.0)
    nonlinear_functions = [
        nlsp.nonlinear_function.Power(degree=i + 1) for i in range(branches)
    ]
    black_box = nlsp.HammersteinGroupModel(
        nonlinear_functions=nonlinear_functions,
        filter_impulseresponses=linear_filters,
        aliasing_compensation=aliasing_compensation)
    identification_algorithm = nlsp.system_identification.CosineSweep(
        select_branches=range(1, branches + 1),
        aliasing_compensation=aliasing_compensation,
        excitation_length=2**15,
        excitation_sampling_rate=48000)
    excitation = identification_algorithm.GetExcitation()
    black_box.SetInput(excitation)
    identification_algorithm.SetResponse(black_box.GetOutput())
    model_black_box = identification_algorithm.GetOutputModel()

    exc = nlsp.excitation_generators.Sinesweepgenerator_Novak(
        sampling_rate=48000.0, approximate_numberofsamples=2**15)
    model_black_box.SetInput(exc.GetOutput())
    black_box.SetInput(exc.GetOutput())
    evaluation = nlsp.evaluations.CompareWithReference(
        black_box.GetOutput(), model_black_box.GetOutput())
    assert evaluation.GetSignaltoErrorRatio()[0] >= 100
예제 #16
0
 def __init__(self, model=None):
     """
     :param model: the instance of the nonlinear model
     """
     if model is None:
         self._input_model = nlsp.HammersteinGroupModel()
     else:
         self._input_model = model
예제 #17
0
def filter_kernels_test():
    nl_func = [nlsp.nonlinear_function.Power, nlsp.nonlinear_function.Chebyshev, nlsp.nonlinear_function.Legendre,
               nlsp.nonlinear_function.Hermite]
    branches = 3
    ref_filters = nlsp.helper_functions.create_arrayof_bpfilter(branches=branches, filter_length=2 ** 15)
    for nl1, nl2 in itertools.permutations(nl_func, 2):
        ref_nlsystem = nlsp.HammersteinGroupModel(nonlinear_functions=[nl1(degree=i + 1) for i in range(branches)],
                                                  aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(),
                                                  filter_impulseresponses=ref_filters)
        ref_nlsystem1 = ref_nlsystem
        recompute_filterkernels = nlsp.RecomputeFilterKernels(input_model=ref_nlsystem1)
        recompute_filterkernels.SetNonlinearFunction(nonlinearfunction=nl2)
        found_filter_kernels = recompute_filterkernels._filter_impulseresponses
        mod_system = nlsp.HammersteinGroupModel(nonlinear_functions=[nl2(degree=i + 1) for i in range(branches)],
                                                aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(),
                                                filter_impulseresponses=found_filter_kernels)
        ip = sumpf.modules.NoiseGenerator().GetSignal()
        ref_nlsystem.SetInput(ip)
        mod_system.SetInput(ip)
        evaluation = nlsp.evaluations.CompareWithReference(reference_signal=ref_nlsystem.GetOutput(),
                                                           signal_to_be_evaluated=mod_system.GetOutput())
        ser = evaluation.GetSignaltoErrorRatio()[0][0]
        print ser, nl1, nl2
def test_SISO_NLMS_algorithm():
    """
    Test the SISO_NLMS_algorithm class by identifying a HGM.
    """
    sampling_rate = 48000
    length = 2 ** 15
    branches = 2
    input_signal = sumpf.modules.NoiseGenerator(samplingrate=sampling_rate, length=length, seed="signal").GetSignal()
    linear_kernels = nlsp.helper_functions.create_arrayof_bpfilter(sampling_rate=sampling_rate, branches=branches)
    nl_functions_1 = [nlsp.nonlinear_function.Power(i + 1) for i in range(branches)]
    nl_functions_2 = [nlsp.nonlinear_function.Power(i + 1) for i in range(branches)]
    nl_system = nlsp.HammersteinGroupModel(nonlinear_functions=nl_functions_1, filter_impulseresponses=linear_kernels,
                                           aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation())
    nl_system.SetInput(input_signal=input_signal)

    outputs = []
    nl_ = [nlsp.nonlinear_function.Power(degree=1), nlsp.nonlinear_function.Power(degree=2)]
    for nl in nl_:
        hm = nlsp.HammersteinModel(
            aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(),
            nonlinear_function=nl)
        hm.SetInput(input_signal)
        outputs.append(hm.GetOutput())
    outputs = sumpf.modules.MergeSignals(signals=outputs).GetOutput()
    adaptation_algorithm = nlsp.SISO_NLMS_algorithm(input_signal=outputs, filter_length=len(linear_kernels[0]),
                                                    desired_output=nl_system.GetOutput())
    filter_kernels = adaptation_algorithm.GetFilterKernel()
    filter_kernel_1 = sumpf.modules.SplitSignal(data=filter_kernels, channels=[0]).GetOutput()
    filter_kernel_2 = sumpf.modules.SplitSignal(data=filter_kernels, channels=[1]).GetOutput()
    identified_model = nlsp.HammersteinGroupModel(nonlinear_functions=nl_functions_2,
                                                  filter_impulseresponses=[filter_kernel_1, filter_kernel_2],
                                                  aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation())
    identified_model.SetInput(input_signal=input_signal)
    evaluation = nlsp.evaluations.CompareWithReference(reference_signal=nl_system.GetOutput(),
                                                       signal_to_be_evaluated=identified_model.GetOutput())
    assert evaluation.GetSignaltoErrorRatio()[0][0] > 55
 def blackbox(input_signal):
     input_signal = sumpf.modules.MergeSignals(
         signals=[input_signal, input_signal]).GetOutput()
     aliasing_compensation = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(
     )
     nonlinear_functions = [
         nlsp.nonlinear_function.Power(degree=i + 1)
         for i in range(branches)
     ]
     filter_spec_tofind = nlsp.helper_functions.create_arrayof_bpfilter(
         branches=branches, sampling_rate=sampling_rate)
     ref_nlsystem = nlsp.HammersteinGroupModel(
         nonlinear_functions=nonlinear_functions,
         filter_impulseresponses=filter_spec_tofind,
         aliasing_compensation=aliasing_compensation)
     ref_nlsystem.SetInput(input_signal)
     return ref_nlsystem.GetOutput()
def test_HGM():
    """
    Test the SetInput method of HammersteinGroupModel.
    """
    branches = 3
    input_signal = sumpf.modules.SweepGenerator(samplingrate=48000.0, length=2 ** 14).GetSignal()
    nonlinear_functions = [nlsp.nonlinear_functions.Power(degree=i + 1) for i in range(branches)]
    filter_irs = nlsp.helper_functions.create_arrayof_bpfilter(branches=branches, sampling_rate=48000)
    HGM = nlsp.HammersteinGroupModel(nonlinear_functions=nonlinear_functions, filter_impulseresponses=filter_irs,
                                     aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation())
    energy1 = nlsp.common.helper_functions_private.calculateenergy_timedomain(HGM.GetOutput())
    HGM.SetInput(input_signal)
    energy2 = nlsp.common.helper_functions_private.calculateenergy_timedomain(HGM.GetOutput())
    input_signal = sumpf.modules.SineWaveGenerator(frequency=2000.0, samplingrate=48000.0, length=2 ** 14).GetSignal()
    HGM.SetInput(input_signal)
    energy3 = nlsp.common.helper_functions_private.calculateenergy_timedomain(HGM.GetOutput())
    assert energy1[0] == 0.0
    assert energy2[0] != energy3[0]
 def GetModel(self):
     """
     Get the model which is saved in the file location.
     """
     if self._filename is None:
         raise Exception("Please enter the filename")
     else:
         model = sumpf.modules.SignalFile(filename=self._filename, file_format=self._file_format).GetSignal()
         label = model.GetLabels()[0]
         nonlinear_functions, aliasingcomp, aliasingcomp_loc = decode_label(label=label)
         filter_kernels = []
         for i in range(len(model.GetChannels())):
             kernel = sumpf.modules.SplitSignal(data=model, channels=[i]).GetOutput()
             filter_kernels.append(kernel)
         model = nlsp.HammersteinGroupModel(nonlinear_functions=nonlinear_functions,
                                            filter_impulseresponses=filter_kernels,
                                            aliasing_compensation=aliasingcomp(),
                                            downsampling_position=aliasingcomp_loc)
         return model
 def __init__(self,
              system_response=None,
              select_branches=None,
              aliasing_compensation=None,
              excitation_length=None,
              excitation_sampling_rate=None,
              filter_length=None):
     """
     :param system_response: the response of the nonlinear system
     :param select_branches: the branches of the model to which the filter kernels have to be found Eg. [1,2,3,4,5]
     :param aliasing_compensation: the aliasing compensation parameter of the resulting model
     :param excitation_length: the length of the excitation and response signals
     :param excitation_sampling_rate: the sampling rate of the excitation and response signals
     :param filter_length: the identified filter length
     """
     if system_response is None:
         self._system_response = sumpf.Signal()
     else:
         self._system_response = system_response
     if select_branches is None:
         self._select_branches = [1, 2, 3, 4, 5]
     else:
         self._select_branches = select_branches
     if excitation_length is None:
         self._length = sumpf.config.get("default_signal_length")
     else:
         self._length = excitation_length
     if excitation_sampling_rate is None:
         self._sampling_rate = sumpf.config.get("default_samplingrate")
     else:
         self._sampling_rate = excitation_sampling_rate
     if aliasing_compensation is None:
         self._aliasing_compensation = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(
         )
     else:
         self._aliasing_compensation = aliasing_compensation
     self._filter_length = filter_length
     self._input_model = nlsp.HammersteinGroupModel()
     HGMModelGenerator.__init__(
         self,
         input_model=self._input_model,
         aliasing_compensation=self._aliasing_compensation)
def test_SER_multichannel():
    """
    Test the SER calculation of multichannel signals.
    """
    sampling_rate = 48000
    length = 2 ** 16
    branches = 3
    input_signal_1 = sumpf.modules.SweepGenerator(samplingrate=sampling_rate, length=length).GetSignal()
    input_signal_2 = sumpf.modules.NoiseGenerator(samplingrate=sampling_rate, length=length).GetSignal()
    combined_signal_1 = sumpf.modules.MergeSignals(signals=[input_signal_1, input_signal_2]).GetOutput()
    combined_signal_2 = sumpf.modules.MergeSignals(signals=[input_signal_2, input_signal_1]).GetOutput()
    HGM = nlsp.HammersteinGroupModel(
        nonlinear_functions=[nlsp.nonlinear_function.Power(i + 1) for i in range(branches)],
        aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation())
    HGM.SetInput(combined_signal_1)
    output_1 = HGM.GetOutput()
    HGM.SetInput(combined_signal_2)
    output_2 = HGM.GetOutput()
    evaluation = nlsp.evaluations.CompareWithReference(output_1, output_2)
    assert len(evaluation.GetSignaltoErrorRatio()[0]) == 2
예제 #24
0
    def __init__(self,
                 input_model=None,
                 nonlinear_functions=None,
                 filter_impulseresponses=None,
                 aliasing_compensation=None,
                 downsampling_position=None):
        """
        :param input_model: the input model
        :param filter_impulseresponses: the filter impulse responses Eg, [filter_impulseresponse1, filter_impulseresponse2, ...]
        :param nonlinear_function: the nonlinear functions Eg, [nonlinear_function1, nonlinear_function2, ...]
        :param aliasing_compensation: the aliasing compensation technique Eg, nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation()
        :param downsampling_position: the downsampling position in the HGM
        """
        if input_model is None:
            self._input_model = nlsp.HammersteinGroupModel()
        else:
            self._input_model = input_model

        if nonlinear_functions is None:
            self._nonlinear_functions = self._input_model.GetNonlinearFunctions(
            )
        else:
            self._nonlinear_functions = nonlinear_functions

        if filter_impulseresponses is None:
            self._filter_impulseresponses = self._input_model.GetFilterImpulseResponses(
            )
        else:
            self._filter_impulseresponses = filter_impulseresponses

        if aliasing_compensation is None:
            self._aliasing_compensation = self._input_model._get_aliasing_compensation(
            )
        else:
            self._aliasing_compensation = aliasing_compensation

        if downsampling_position is None:
            self._downsampling_position = self._input_model._downsampling_position
        else:
            self._downsampling_position = downsampling_position
def test_aliasing():
    """
    Tests whether aliasing is present in the hammerstein group model.
    The pure sine tone is given to the model and the ouput 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.HammersteinModel(
            input_signal=ip_sine_signal,
            nonlin_func=nlsp.function_factory.power_series(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(
        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))
    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 numpy.sum(Test_groupmodel_energy_all) == numpy.sum(
        Test_groupmodel_energy_freq)
def test_biquadtracing():
    branches = 3
    clipping_thresholds = [[-1.0, 1.0], [-0.9, 0.9], [-0.8, 0.9]]
    filter_length = 2**8
    nonlinear_function_ref = [
        nlsp.nonlinear_function.HardClip(clipping_threshold=th)
        for th in clipping_thresholds
    ]
    filter_impulse_responses_ref = nlsp.helper_functions.create_arrayof_complexfilters(
        branches=branches, filter_length=filter_length)
    ref_hgm = nlsp.HammersteinGroupModel(
        nonlinear_functions=nonlinear_function_ref,
        filter_impulseresponses=filter_impulse_responses_ref,
        aliasing_compensation=nlsp.aliasing_compensation.
        NoAliasingCompensation())
    curve_tracing_filters_iir, coeff = nlsp.curve_fitting_algorithms.compute_iir_from_fir_using_curvetracing_biquads(
        fir_kernels=ref_hgm.GetFilterImpulseResponses(),
        filter_order=4,
        Print=False,
        max_iterations=10)
    assert len(curve_tracing_filters_iir) == len(coeff.coefficients) == len(
        coeff.frequencies)
예제 #27
0
def test_identify_an_HGM():
    """
    Test the accuracy of identification of an HGM by MISO approach of system identification.
    """
    branches = 3
    excitation_length = 2**14
    sampling_rate = 48000
    select_branches = range(1, branches + 1)
    aliasing_compensation = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(
    )
    linear_filters = nlsp.helper_functions.create_arrayof_bpfilter(
        branches=branches, sampling_rate=48000.0)
    nonlinear_functions = [
        nlsp.nonlinear_function.Power(degree=i + 1) for i in range(branches)
    ]
    black_box = nlsp.HammersteinGroupModel(
        nonlinear_functions=nonlinear_functions,
        aliasing_compensation=aliasing_compensation,
        filter_impulseresponses=linear_filters)
    identification_algorithm = nlsp.system_identification.MISOapproachusingHermite(
        select_branches=select_branches,
        excitation_length=excitation_length,
        excitation_sampling_rate=sampling_rate)
    excitation = identification_algorithm.GetExcitation()
    black_box.SetInput(excitation)
    identification_algorithm.SetResponse(response=black_box.GetOutput())

    model_black_box = identification_algorithm.GetOutputModel()

    exc = sumpf.modules.NoiseGenerator(
        distribution=sumpf.modules.NoiseGenerator.UniformDistribution(),
        samplingrate=48000,
        length=2**16).GetSignal()
    model_black_box.SetInput(exc)
    black_box.SetInput(exc)
    evaluation = nlsp.evaluations.CompareWithReference(
        black_box.GetOutput(), model_black_box.GetOutput())
    assert evaluation.GetSignaltoErrorRatio()[0] >= 40
def test_saveandretreive_clippingHGM():
    """
    test save and retreive model class for HGM with clipping functions as nonlinear functions.
    """
    branches = 5
    thresholds = [
        [-1.0, 1.0],
    ] * branches
    location = "C:\Users\diplomand.8\Desktop\some1.npz"
    model = nlsp.HammersteinGroupModel(nonlinear_functions=[
        nlsp.nonlinear_function.HardClip(clipping_threshold=threshold)
        for threshold in thresholds
    ])
    save = nlsp.SaveHGMModel(filename=location, model=model)
    retrieve = nlsp.RetrieveHGMModel(filename=location)
    model_retrieved = retrieve.GetModel()
    sample = sumpf.modules.SweepGenerator().GetSignal()
    model.SetInput(sample)
    model_retrieved.SetInput(sample)
    evaluation = nlsp.evaluations.CompareWithReference(
        reference_signal=model.GetOutput(),
        signal_to_be_evaluated=model_retrieved.GetOutput())
    assert evaluation.GetSignaltoErrorRatio() > 500
예제 #29
0
def test_recomputefilterkernels():
    branches = 3
    ref_filters = nlsp.helper_functions.create_arrayof_bpfilter(branches=branches, filter_length=2 ** 15)
    ref_nlsystem = nlsp.HammersteinGroupModel(
        nonlinear_functions=[nlsp.nonlinear_function.Power(degree=i + 1) for i in range(branches)],
        aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(),
        filter_impulseresponses=ref_filters)
    system_iden = nlsp.system_identification.SineSweep()
    excitation = system_iden.GetExcitation()
    ref_nlsystem.SetInput(excitation)
    response = ref_nlsystem.GetOutput()
    system_iden.SetResponse(response)
    output_model = system_iden.GetOutputModel()
    recompute_filterkernels = nlsp.RecomputeFilterKernels(input_model=output_model)
    recompute_filterkernels.SetNonlinearFunction(nonlinearfunction=nlsp.nonlinear_function.Laguerre)
    modified_output_model = recompute_filterkernels.GetOutputModel()
    sample_signal = sumpf.modules.NoiseGenerator().GetSignal()
    output_model.SetInput(sample_signal)
    modified_output_model.SetInput(sample_signal)
    ref_nlsystem.SetInput(sample_signal)
    evaluation = nlsp.evaluations.CompareWithReference(reference_signal=output_model.GetOutput(),
                                                       signal_to_be_evaluated=modified_output_model.GetOutput())
    assert evaluation.GetSignaltoErrorRatio()[0] > 100
예제 #30
0
def test_identify_a_HGM_adaptive():
    """
    Test the accuracy of adaptive system identification using a HGM.
    """
    branches = 2
    excitation_length = 2**15
    sampling_rate = 48000
    select_branches = range(1, branches + 1)
    aliasing_compensation = nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(
    )
    linear_filters = nlsp.helper_functions.create_arrayof_bpfilter(
        branches=branches, sampling_rate=sampling_rate)
    nonlinear_functions = [
        nlsp.nonlinear_function.Power(i + 1) for i in range(branches)
    ]
    black_box = nlsp.HammersteinGroupModel(
        nonlinear_functions=nonlinear_functions,
        aliasing_compensation=aliasing_compensation,
        filter_impulseresponses=linear_filters)
    identification_algorithm = nlsp.system_identification.Adaptive(
        select_branches=select_branches,
        excitation_length=excitation_length,
        nonlinear_function=nlsp.nonlinear_function.Power)
    excitation = identification_algorithm.GetExcitation()
    black_box.SetInput(input_signal=excitation)
    identification_algorithm.SetResponse(response=black_box.GetOutput())
    model_black_box = identification_algorithm.GetOutputModel()
    exc = sumpf.modules.NoiseGenerator(
        distribution=sumpf.modules.NoiseGenerator.UniformDistribution(),
        samplingrate=48000,
        length=2**16,
        seed="signal")
    model_black_box.SetInput(exc.GetSignal())
    black_box.SetInput(exc.GetSignal())
    evaluation = nlsp.evaluations.CompareWithReference(
        black_box.GetOutput(), model_black_box.GetOutput())
    assert evaluation.GetSignaltoErrorRatio() >= 70