def test_modelquality():
    """
    Tests the quality of the model. On observing the models it is found that the upsampling hammerstein model doesnot
    produce aliasing effect. But other models produces aliasing. This test makes sure that aliasing will be produced
    in the model if frequency*max_harm is greater than sampling rate
    """
    max_harm = [1, 2, 3, 4, 5]
    frequency = [1000, 5000, 10000, 15000, 23000]
    s_rate = 48000
    length = s_rate

    combinations = list(itertools.product(frequency, max_harm))
    for comb in combinations:
        freq = comb[0]
        harm = comb[1]
        sine_signal = sumpf.modules.SineWaveGenerator(frequency=freq,
                                                      phase=0.0,
                                                      samplingrate=s_rate,
                                                      length=length)
        sine_spec = sumpf.modules.FourierTransform(
            signal=sine_signal.GetSignal())
        Test_Model_Hammerstein = nlsp.AliasingCompensatedHM_lowpass(
            input_signal=sine_signal.GetSignal(),
            nonlin_func=nlsp.function_factory.power_series(harm),
            max_harm=harm)
        Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput()
        e = nlsp.calculateenergy_freq(Test_Model_outputsignal)
        h = nlsp.predictharmonics_usingupsampling([freq], harm, s_rate)
        f = nlsp.calculateenergy_atparticularfrequencies(
            Test_Model_outputsignal, h)
        quality = numpy.sum(f) / numpy.sum(e)
        if freq * harm > s_rate / 2:
            assert quality <= 1
        else:
            assert quality == 1
Beispiel #2
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 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.AliasingCompensatedHM_upsampling(
            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_up(
        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 numpy.sum(Test_groupmodel_energy_all) == numpy.sum(
        Test_groupmodel_energy_freq)