示例#1
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
def test_aliasingtest():
    """
    Test the Aliasing effect in the model. The output freq are calculated theoretically and is compared with the model.
    If input freq is greater than nyquist freq then aliasing occurs because of which frequencies which are not in input
    signal appears in the output. This test tests the aliasing effect. if it fails then there should be freq in the
    output other than the theoretically calculated freq, then there should be some problem in the signal processing
    block
    """
    max_harm = 5
    freq = 20000
    s_rate = 48000
    length = s_rate
    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(max_harm),
        max_harm=max_harm)
    Test_Model_Hammerstein.SetMaximumHarmonic(1)
    Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput()
    Test_Model_outputspec = sumpf.modules.FourierTransform(
        Test_Model_outputsignal).GetSpectrum()
    Test_Model_HarmonicFreq = []
    h = nlsp.find_frequencies(Test_Model_outputsignal)
    predicted_freq = nlsp.predictoutputfreq_usingsamplingtheory(
        freq, max_harm, s_rate)
    assert predicted_freq == h
def test_aliasingtest_comparewithupsampling():
    """
    Test the Aliasing effect in the model. The output freq are calculated by applying the signal to the
    upsampling hammerstein model and is compared with the model.
    If input freq is greater than nyquist freq then aliasing occurs because of which frequencies which are not in input
    signal appears in the output. This test tests the aliasing effect. if it fails then there should be freq in the
    output other than the theoretically calculated freq, then there should be some problem in the signal processing
    block
    """
    max_harm = 2
    frequency = [1000, 5000, 6000, 17000]
    s_rate = 48000
    length = s_rate
    for freq in frequency:
        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(max_harm),
            max_harm=max_harm)
        Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput()
        Test_Model_outputspec = sumpf.modules.FourierTransform(
            Test_Model_outputsignal).GetSpectrum()
        frequencies = nlsp.find_frequencies(Test_Model_outputsignal)
        predict_freq = nlsp.predictharmonics_usingupsampling([freq], max_harm,
                                                             s_rate)
        if freq * max_harm < s_rate / 2:
            assert frequencies == predict_freq
        else:
            assert frequencies != predict_freq
示例#4
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)