def theoretical_evaluation():
    puretone_freq = 10000
    input_tone = sumpf.modules.SineWaveGenerator(frequency=puretone_freq,
                                                 phase=0.0,
                                                 samplingrate=sampling_rate,
                                                 length=length)
    sine = input_tone.GetSignal()
    input_tone.SetFrequency(2 * puretone_freq)
    sine_2 = input_tone.GetSignal()
    input_tone.SetFrequency(3 * puretone_freq)
    sine_3 = input_tone.GetSignal()
    input_tone.SetPhaseInDegrees(90)
    cos = input_tone.GetSignal()
    input_tone.SetFrequency(2 * puretone_freq)
    cos_2 = input_tone.GetSignal()
    input_tone.SetFrequency(3 * puretone_freq)
    cos_3 = input_tone.GetSignal()

    theoreticl_op_2 = sumpf.modules.ConstantSignalGenerator(value=0.5,samplingrate=sine.GetSamplingRate(),length=len(sine)).GetSignal() - \
                    sumpf.modules.AmplifySignal(factor=0.5,input=cos_2).GetOutput()
    theoreticl_op_3 = sumpf.modules.AmplifySignal(factor=3.0/4.0,input=sine).GetOutput() - \
                      sumpf.modules.AmplifySignal(factor=3.0/4.0,input=sine_3).GetOutput()

    branch_simple_2 = nlsp.HammersteinModel(
        input_signal=sine, nonlin_func=nlsp.function_factory.power_series(2))
    branch_simple_3 = nlsp.HammersteinModel(
        input_signal=sine, nonlin_func=nlsp.function_factory.power_series(3))
    branch_up_2 = nlsp.AliasCompensatingHammersteinModelUpandDown(
        input_signal=sine,
        nonlin_func=nlsp.function_factory.power_series(2),
        max_harm=2)
    branch_up_3 = nlsp.AliasCompensatingHammersteinModelUpandDown(
        input_signal=sine,
        nonlin_func=nlsp.function_factory.power_series(3),
        max_harm=3)
    branch_lp_2 = nlsp.AliasCompensatingHammersteinModelLowpass(
        input_signal=sine,
        nonlin_func=nlsp.function_factory.power_series(2),
        max_harm=2)
    branch_lp_3 = nlsp.AliasCompensatingHammersteinModelLowpass(
        input_signal=sine,
        nonlin_func=nlsp.function_factory.power_series(3),
        max_harm=3)
    snr_simple_2 = nlsp.snr(theoreticl_op_2, branch_simple_2.GetOutput())
    snr_simple_3 = nlsp.snr(theoreticl_op_3, branch_simple_3.GetOutput())
    snr_up_2 = nlsp.snr(theoreticl_op_2, branch_up_2.GetOutput())
    snr_up_3 = nlsp.snr(theoreticl_op_3, branch_up_3.GetOutput())
    snr_lp_2 = nlsp.snr(theoreticl_op_2, branch_lp_2.GetOutput())
    snr_lp_3 = nlsp.snr(theoreticl_op_3, branch_lp_3.GetOutput())
    # plot.relabelandplot(sumpf.modules.FourierTransform(branch_simple.GetOutput()).GetSpectrum(),"simpleout",show=False)
    plot.relabelandplot(branch_up_2.GetOutput(), "upout", show=False)
    # plot.relabelandplot(sumpf.modules.FourierTransform(branch_lp.GetOutput()).GetSpectrum(),"lpout",show=False)
    plot.relabelandplot(theoreticl_op_2, "theoryout", show=True)
    # plot.relabelandplot(sumpf.modules.FourierTransform(ip_sine).GetSpectrum(),"input",show=True)
    # print "simple,2nd degree:%r" %snr_simple_2
    # print "simple,3rd degree:%r" %snr_simple_3
    print "up,2nd degree:%r" % snr_up_2
    print "up,3rd degree:%r" % snr_up_3
    print "lp,2nd degree:%r" % snr_lp_2
    print "lp,3rd degree:%r" % snr_lp_3
def higher_nonlinearity_evaluation():
    sampling_rate = 48000
    sweep_start_freq = 20.0
    sweep_stop_freq = 24000.0
    length = 2**15
    degree = 5
    input_signal = nlsp.NovakSweepGenerator_Sine(
        sampling_rate=sampling_rate,
        length=length,
        start_frequency=sweep_start_freq,
        stop_frequency=sweep_stop_freq)
    # input_signal = sumpf.modules.SweepGenerator(samplingrate=sampling_rate,length=length)
    normal = sumpf.modules.NoiseGenerator.GaussianDistribution(
        mean=0.0, standard_deviation=1.0)
    wgn_normal = nlsp.WhiteGaussianGenerator(sampling_rate=sampling_rate,
                                             length=length,
                                             start_frequency=20.0,
                                             stop_frequency=24000.0,
                                             distribution=normal)
    frequencies = [
        100, 500, 1000, 2000, 4000, 5000, 10000, 15000, 18000, 20000, 24000
    ]
    puretones = nlsp.generate_puretones(frequencies, sampling_rate, length)
    input_sweep_signal = input_signal.GetOutput()
    model_up_ref = nlsp.AliasCompensatingHammersteinModelUpandDown(
        input_signal=input_sweep_signal,
        nonlin_func=nlsp.function_factory.power_series(degree),
        max_harm=degree)
    model_lp_ref = nlsp.AliasCompensatingHammersteinModelLowpass(
        input_signal=input_sweep_signal,
        nonlin_func=nlsp.function_factory.power_series(degree),
        max_harm=degree)
    model_up_ref = model_up_ref.GetOutput()
    model_lp_ref = model_lp_ref.GetOutput()
    for factor in range(degree - 2, degree + 2):
        model_up = nlsp.AliasCompensatingHammersteinModelUpandDown(
            input_signal=input_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(degree),
            max_harm=factor)
        model_lp = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=input_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(degree),
            max_harm=factor)
        model_up = model_up.GetOutput()
        model_lp = model_lp.GetOutput()
        print "Upsampling HM, nonlinearity degree:%r, Alias compensation factor:%r, SNR:%r" % (
            degree, factor,
            nlsp.snr(model_up_ref, model_up, freqrange=[20.0, 24000.0]))
        print "Lowpass HM, nonlinearity degree:%r, Alias compensation factor:%r, SNR:%r" % (
            degree, factor,
            nlsp.snr(model_lp_ref, model_lp, freqrange=[20.0, 24000.0]))
Exemple #3
0
def resampler_compensation_evaluation():
    """
    Evaluation of the Upsampling and Downsampling alias compensating hammerstein model.
    The nonlinear block of hammerstein model is set to the power series of 1 but the maximum harmonics is set to higher
    value and evaluate for the attenuation introduced by alias compensation.
    we expect the input and the output is of same amplitude. and the low pass alias compensation introduces attenuation
    by lowpass filtering whereas the upsampling alias compensation model should be ideal
    """
    max_harm = 2
    ip_sweep_signal = sumpf.modules.SweepGenerator(
        samplingrate=sweep_samplingrate, length=sweep_length).GetSignal()
    ip_impulse = sumpf.modules.ImpulseGenerator(
        samplingrate=sweep_samplingrate, length=sweep_length).GetSignal()
    prp = sumpf.modules.ChannelDataProperties()
    prp.SetSignal(ip_sweep_signal)
    filter = sumpf.modules.FilterGenerator(
        sumpf.modules.FilterGenerator.BUTTERWORTH(order=100),
        frequency=24000,
        resolution=prp.GetResolution(),
        length=prp.GetSpectrumLength()).GetSpectrum()
    filterip = sumpf.modules.InverseFourierTransform(
        spectrum=filter).GetSignal()
    UPHModel_imp = nlsp.AliasCompensatingHammersteinModelUpandDown(
        input_signal=ip_sweep_signal,
        nonlin_func=nlsp.function_factory.power_series(1),
        max_harm=max_harm,
        filter_impulseresponse=ip_impulse)
    UPHModel_filt = nlsp.AliasCompensatingHammersteinModelUpandDown(
        input_signal=ip_sweep_signal,
        nonlin_func=nlsp.function_factory.power_series(1),
        max_harm=max_harm,
        filter_impulseresponse=filterip)
    DownHModel_imp = nlsp.AliasCompensatingHammersteinModelDownandUp(
        input_signal=ip_sweep_signal,
        nonlin_func=nlsp.function_factory.power_series(1),
        max_harm=max_harm,
        filter_impulseresponse=ip_impulse)
    DownHModel_filt = nlsp.AliasCompensatingHammersteinModelDownandUp(
        input_signal=ip_sweep_signal,
        nonlin_func=nlsp.function_factory.power_series(1),
        max_harm=max_harm,
        filter_impulseresponse=filterip)
    common.plot.plot(ip_sweep_signal, show=False)
    common.plot.plot(UPHModel_imp.GetOutput(), show=False)
    common.plot.plot(UPHModel_filt.GetOutput(), show=True)
    common.plot.plot(ip_sweep_signal, show=False)
    common.plot.plot(DownHModel_imp.GetOutput(), show=False)
    common.plot.plot(DownHModel_filt.GetOutput(), show=True)
def reliability_evaluation_puretone():
    length = 2**15
    for max_harm in range(3, 6):
        frequencies = [100, 500, 1000, 2000, 4000]
        puretones = nlsp.generate_puretones(frequencies, sampling_rate, length)
        ip_signal = puretones
        nl_degree = max_harm
        model_simple = nlsp.HammersteinModel(
            input_signal=puretones,
            nonlin_func=nlsp.function_factory.power_series(nl_degree))
        model_up = nlsp.AliasCompensatingHammersteinModelUpandDown(
            input_signal=ip_signal,
            nonlin_func=nlsp.function_factory.power_series(nl_degree),
            max_harm=max_harm)
        model_lp = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=ip_signal,
            nonlin_func=nlsp.function_factory.power_series(nl_degree),
            max_harm=max_harm)
        # plot.relabelandplot(sumpf.modules.FourierTransform(model_simple.GetOutput()).GetSpectrum(),"Uncompensated",show=False)
        # plot.relabelandplot(sumpf.modules.FourierTransform(model_up.GetOutput()).GetSpectrum(),"Upsampling",show=False)
        # plot.relabelandplot(sumpf.modules.FourierTransform(model_lp.GetOutput()).GetSpectrum(),"Lowpass filtering",show=True)

        plot.relabelandplot(model_simple.GetOutput(),
                            "Uncompensated",
                            show=False)
        plot.relabelandplot(model_up.GetOutput(), "upsampling", show=False)
        plot.relabelandplot(model_lp.GetOutput(), "lowpass", show=True)

        print "maxharmonics: %r" % max_harm
        print "snr between simple HGM and upsampling HGM: %r" % nlsp.snr(
            model_simple.GetOutput(), model_up.GetOutput())
        print "snr between simple HGM and lowpass HGM: %r" % nlsp.snr(
            model_simple.GetOutput(), model_lp.GetOutput())
        print
def puretone_evaluation():
    """
    Evaluation of alias compensation of hammerstein branch using pure tones.
    Puretone of certain frequency is given to different hammerstein branches with different Aliasing compensation.
    The order of the harmonics produces is changed and the output of different models are plotted.
    We observe the lowpass aliasing compensation completely filters out the signal harmonics even when they are in the
    baseband freq.
    """
    print "puretone evaluation"
    for i in range(1, degree + 1):
        input_tone = nlsp.generate_puretones([10000], sampling_rate, length)
        branch_simple = nlsp.HammersteinModel(
            input_signal=input_tone,
            nonlin_func=nlsp.function_factory.power_series(i))
        branch_up = nlsp.AliasCompensatingHammersteinModelUpandDown(
            input_signal=input_tone,
            nonlin_func=nlsp.function_factory.power_series(i),
            max_harm=i)
        branch_lp = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=input_tone,
            nonlin_func=nlsp.function_factory.power_series(i),
            max_harm=i)
        print "Pure tone evaluation"
        print "Order: %r" % i
        print "SNR of simple h. branch: %r" % nlsp.snr(
            input_tone, branch_simple.GetOutput())
        print "SNR of upsample h. branch: %r" % nlsp.snr(
            input_tone, branch_up.GetOutput())
        print "SNR of lowpass h. branch: %r" % nlsp.snr(
            input_tone, branch_lp.GetOutput())
        print "MSE of simple h. branch: %r" % nlsp.mean_squared_error_time(
            input_tone, branch_simple.GetOutput())
        print "MSE of upsample h. branch: %r" % nlsp.mean_squared_error_time(
            input_tone, branch_up.GetOutput())
        print "MSE of lowpass h. branch: %r" % nlsp.mean_squared_error_time(
            input_tone, branch_lp.GetOutput())
        if Plot is True:
            plot.log()
            branch_simple_spectrum = nlsp.relabel(
                sumpf.modules.FourierTransform(
                    branch_simple.GetOutput()).GetSpectrum(),
                "%d Simple Hammerstein Branch" % i)
            plot.plot(branch_simple_spectrum, show=False)
            branch_up_spectrum = nlsp.relabel(
                sumpf.modules.FourierTransform(
                    branch_up.GetOutput()).GetSpectrum(),
                "%d Upsampling Hammerstein Branch" % i)
            plot.plot(branch_up_spectrum, show=False)
            branch_lp_spectrum = nlsp.relabel(
                sumpf.modules.FourierTransform(
                    branch_lp.GetOutput()).GetSpectrum(),
                "%d Lowpass Hammerstein Branch" % i)
            plot.plot(branch_lp_spectrum, show=True)
def harmonics_evaluation():
    degree = 5
    length = 2**18
    input_signal = nlsp.NovakSweepGenerator_Sine(
        sampling_rate=sampling_rate,
        length=length,
        start_frequency=sweep_start_freq,
        stop_frequency=sweep_stop_freq)
    input_sweep_signal = input_signal.GetOutput()

    for i in range(degree, degree + 1):
        prp = sumpf.modules.ChannelDataProperties()
        prp.SetSignal(input_sweep_signal)
        filter = sumpf.modules.FilterGenerator(
            filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH(
                order=100),
            frequency=24000.0,
            transform=False,
            resolution=prp.GetResolution(),
            length=prp.GetSpectrumLength()).GetSpectrum()
        filter_ir = sumpf.modules.InverseFourierTransform(filter).GetSignal()
        branch_simple = nlsp.HammersteinModel(
            input_signal=input_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(i),
            filter_impulseresponse=filter_ir)
        branch_up = nlsp.AliasCompensatingHammersteinModelUpandDown(
            input_signal=input_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(i),
            max_harm=i,
            filter_impulseresponse=filter_ir)
        branch_lp = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=input_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(i),
            max_harm=i,
            filter_impulseresponse=filter_ir)
        harm_simple = nlsp.get_nl_impulse_response(input_signal,
                                                   branch_simple.GetOutput())
        harm_up = nlsp.get_nl_impulse_response(input_signal,
                                               branch_up.GetOutput())
        harm_lowpass = nlsp.get_nl_impulse_response(input_signal,
                                                    branch_lp.GetOutput())
        plot.relabelandplot(harm_simple, "simple HM", show=True)
        plot.relabelandplot(harm_up, "upsampling HM", show=True)
        plot.relabelandplot(harm_lowpass, "lowpass HM", show=True)
        print nlsp.harmonicsvsall_energyratio_nl(input_signal,
                                                 branch_simple.GetOutput(), i)
        print nlsp.harmonicsvsall_energyratio_nl(input_signal,
                                                 branch_up.GetOutput(), i)
        print nlsp.harmonicsvsall_energyratio_nl(input_signal,
                                                 branch_lp.GetOutput(), i)
        print
        print
def wgn_evaluation():
    degree = 5
    print "wgn evaluation"
    for degree in range(3, degree + 1):
        wgn = sumpf.modules.NoiseGenerator(
            sumpf.modules.NoiseGenerator.GaussianDistribution(
                mean=0.0, standard_deviation=1.0),
            samplingrate=sampling_rate,
            length=length)
        prp = sumpf.modules.ChannelDataProperties()
        prp.SetSignal(wgn.GetSignal())
        filter = sumpf.modules.RectangleFilterGenerator(
            resolution=prp.GetResolution(),
            length=prp.GetSpectrumLength()).GetSpectrum()
        ref = nlsp.HammersteinModel(
            input_signal=wgn.GetSignal(),
            nonlin_func=nlsp.function_factory.power_series(degree),
            filter_impulseresponse=sumpf.modules.InverseFourierTransform(
                filter).GetSignal()).GetOutput()
        model_simple = nlsp.HammersteinModel(
            input_signal=wgn.GetSignal(),
            nonlin_func=nlsp.function_factory.power_series(degree))
        model_up = nlsp.AliasCompensatingHammersteinModelUpandDown(
            input_signal=wgn.GetSignal(),
            nonlin_func=nlsp.function_factory.power_series(degree),
            max_harm=degree)
        model_lp = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=wgn.GetSignal(),
            nonlin_func=nlsp.function_factory.power_series(degree),
            max_harm=degree)
        print "degree %r" % degree
        print nlsp.snr(model_simple.GetOutput(), ref)
        print nlsp.snr(model_up.GetOutput(), ref)
        print nlsp.snr(model_lp.GetOutput(), ref)
        print
        print
        if Plot is True:
            plot.relabelandplot(sumpf.modules.FourierTransform(
                model_up.GetOutput()).GetSpectrum(),
                                "Upsampling HM",
                                show=False)
            plot.relabelandplot(
                sumpf.modules.FourierTransform(ref).GetSpectrum(),
                "Upsampling HM Ref")
            plot.relabelandplot(sumpf.modules.FourierTransform(
                model_lp.GetOutput()).GetSpectrum(),
                                "Lowpass HM",
                                show=False)
            plot.relabelandplot(
                sumpf.modules.FourierTransform(ref).GetSpectrum(),
                "Lowpass HM Ref")
def linearity_evaluation():
    print "linearity evaluation"
    for i in range(3, degree + 1):
        max_harm = i
        nl_degree = i
        sweep_start_freq = 20.0
        sweep_stop_freq = 4000.0
        ip_sweep_signal = sumpf.modules.SweepGenerator(
            samplingrate=sampling_rate,
            length=length,
            start_frequency=sweep_start_freq,
            stop_frequency=sweep_stop_freq).GetSignal()
        model_simple = nlsp.HammersteinModel(
            input_signal=ip_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(nl_degree))
        model_up = nlsp.AliasCompensatingHammersteinModelUpandDown(
            input_signal=ip_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(nl_degree),
            max_harm=max_harm)
        model_lp = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=ip_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(nl_degree),
            max_harm=max_harm)
        simple_ref = model_simple.GetOutput()
        up_ip = model_up.GetOutput()
        lp_ip = model_lp.GetOutput()
        print "degree %r" % i
        print nlsp.snr(simple_ref, simple_ref)
        print nlsp.snr(up_ip, simple_ref)
        print nlsp.snr(lp_ip, simple_ref)
        print
        print
        if Plot is True:
            plot.log()
            plot.relabelandplot(sumpf.modules.FourierTransform(
                model_simple.GetOutput()).GetSpectrum(),
                                "Reference",
                                show=False)
            plot.relabelandplot(sumpf.modules.FourierTransform(
                model_up.GetOutput()).GetSpectrum(),
                                "Upsampling HM",
                                show=False)
            plot.relabelandplot(sumpf.modules.FourierTransform(
                model_lp.GetOutput()).GetSpectrum(),
                                "Lowpass HM",
                                show=True)
def sweep_evaluation():
    """
    Evaluation of alias compensation of hammerstein branch using sweep.
    Sweep(20 to 20000Hz) of sampling rate 48000 samples/sec is upsampled to 96000 samples/sec and given to upsampling
    and lowpass filtering Alias compensation. And the maximum harmonic of the hammerstein branches is changed to two
    and the energy of the branches is evaluated.
    The energy of the desired band and undesired band should be equal for both upsampling and lowpass filtering alias
    compensation.
    """
    print "sweep evaluation"
    for i in range(1, degree + 1):
        up_sweep_signal = sumpf.modules.ResampleSignal(
            signal=input_sweep_signal,
            samplingrate=input_sweep_signal.GetSamplingRate() * i).GetOutput()
        branch = nlsp.HammersteinModel(
            input_signal=up_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(i))
        branch_up = nlsp.AliasCompensatingHammersteinModelUpandDown(
            input_signal=up_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(1),
            max_harm=i)
        branch_lp = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=up_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(1),
            max_harm=i,
            filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH(
                order=100))
        snr = nlsp.snr(
            input_sweep_signal,
            sumpf.modules.ResampleSignal(
                signal=branch.GetOutput(),
                samplingrate=input_sweep_signal.GetSamplingRate()).GetOutput())
        snr_up = nlsp.snr(
            input_sweep_signal,
            sumpf.modules.ResampleSignal(
                signal=branch_up.GetOutput(),
                samplingrate=input_sweep_signal.GetSamplingRate()).GetOutput())
        snr_lp = nlsp.snr(
            input_sweep_signal,
            sumpf.modules.ResampleSignal(
                signal=branch_lp.GetOutput(),
                samplingrate=input_sweep_signal.GetSamplingRate()).GetOutput())
        print snr
        print snr_up
        print snr_lp
        print
def predictharmonics_usingupsampling(frequency, max_harm, samplingrate):
    """
    :param frequency: list of frequencies, the superposition of this list of frequencies is given to the nonlinear
                        system whose harmonics is to be predicted
    :param max_harm:  the maximum harmonics which will be established by the nonlinear system
    :return: the list of frequencies which should appear in the output. These harmonics are not calculated
                mathematically but it uses the Upsampling model to predict the harmonics
    """
    length = samplingrate
    sine_signal_array = []
    sine_spec_array = []
    sine_combined_signal = sumpf.modules.ConstantSignalGenerator(
        value=0.0, samplingrate=samplingrate, length=length).GetSignal()
    for freq in range(0, len(frequency)):
        sine_signal = sumpf.modules.SineWaveGenerator(
            frequency=frequency[freq],
            phase=0.0,
            samplingrate=samplingrate,
            length=length)
        sine_spec = sumpf.modules.FourierTransform(
            signal=sine_signal.GetSignal())
        sine_signal_array.append(sine_signal.GetSignal())
        sine_spec_array.append(sine_spec.GetSpectrum())
        sine_combined_signal = sine_combined_signal + sine_signal.GetSignal()
    sine_combined_spec = sumpf.modules.FourierTransform(
        signal=sine_combined_signal).GetSpectrum()
    Reference_Model = nlsp.AliasCompensatingHammersteinModelUpandDown(
        input_signal=sine_combined_signal,
        nonlin_func=nlsp.function_factory.power_series(max_harm),
        max_harm=max_harm)
    Reference_Model_outputsignal = Reference_Model.GetOutput()
    Reference_Model_outputspec = sumpf.modules.FourierTransform(
        Reference_Model_outputsignal).GetSpectrum()
    Reference_Model_HarmonicFreq = []
    for c in Reference_Model_outputspec.GetChannels():
        channel_h = []
        for i, s in enumerate(c):
            if abs(s)**2 > 100:
                channel_h.append(i)
        Reference_Model_HarmonicFreq.append(channel_h)
    h = [item for sublist in Reference_Model_HarmonicFreq for item in sublist]
    return h
def reliability_evaluation_sweep():
    length = 2**15
    for max_harm in range(1, 6):
        sweep = nlsp.NovakSweepGenerator_Sine(sampling_rate=sampling_rate,
                                              length=length,
                                              start_frequency=20.0,
                                              stop_frequency=2000.0)
        ip_signal = sweep.GetOutput()
        nl_degree = max_harm
        model_simple = nlsp.HammersteinModel(
            input_signal=ip_signal,
            nonlin_func=nlsp.function_factory.power_series(nl_degree))
        model_up = nlsp.AliasCompensatingHammersteinModelUpandDown(
            input_signal=ip_signal,
            nonlin_func=nlsp.function_factory.power_series(nl_degree),
            max_harm=max_harm)
        model_lp = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=ip_signal,
            nonlin_func=nlsp.function_factory.power_series(nl_degree),
            max_harm=max_harm)
        # plot.relabelandplot(sumpf.modules.FourierTransform(model_simple.GetOutput()).GetSpectrum(),"simple",show=False)
        # plot.relabelandplot(sumpf.modules.FourierTransform(model_up.GetOutput()).GetSpectrum(),"upsampling",show=False)
        # plot.relabelandplot(sumpf.modules.FourierTransform(model_lp.GetOutput()).GetSpectrum(),"lowpass",show=True)

        plot.relabelandplot(model_simple.GetOutput(),
                            "Uncompensated",
                            show=False)
        plot.relabelandplot(model_up.GetOutput(), "Upsampling", show=False)
        plot.relabelandplot(model_lp.GetOutput(),
                            "Lowpass filtering",
                            show=True)

        print "maxharmonics: %r" % max_harm
        print "snr between simple HGM and upsampling HGM: %r" % nlsp.snr(
            model_simple.GetOutput(), model_up.GetOutput())
        print "snr between simple HGM and lowpass HGM: %r" % nlsp.snr(
            model_simple.GetOutput(), model_lp.GetOutput())
        print "snr between simple HGM and upsampling HGM magnitude: %r" % nlsp.snr_magnitude(
            model_simple.GetOutput(), model_up.GetOutput())
        print "snr between simple HGM and lowpass HGM magnitude: %r" % nlsp.snr_magnitude(
            model_simple.GetOutput(), model_lp.GetOutput())
        print
def reliability_evaluation_noise():
    length = 2**18
    normal = sumpf.modules.NoiseGenerator.GaussianDistribution(
        mean=0.0, standard_deviation=1.0)
    wgn_normal = nlsp.WhiteGaussianGenerator(sampling_rate=sampling_rate,
                                             length=length,
                                             start_frequency=20.0,
                                             stop_frequency=2000.0,
                                             distribution=normal)
    # wgn_normal = sumpf.modules.NoiseGenerator(distribution=normal,samplingrate=sampling_rate,length=length)
    ip_signal = wgn_normal.GetOutput()
    for max_harm in range(2, 3):
        nl_degree = max_harm
        model_simple = nlsp.HammersteinModel(
            input_signal=ip_signal,
            nonlin_func=nlsp.function_factory.power_series(nl_degree))
        model_up = nlsp.AliasCompensatingHammersteinModelUpandDown(
            input_signal=ip_signal,
            nonlin_func=nlsp.function_factory.power_series(nl_degree),
            max_harm=max_harm)
        model_lp = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=ip_signal,
            nonlin_func=nlsp.function_factory.power_series(nl_degree),
            max_harm=max_harm)
        # plot.relabelandplot(sumpf.modules.FourierTransform(model_simple.GetOutput()).GetSpectrum(),"simple",show=False)
        # plot.relabelandplot(sumpf.modules.FourierTransform(model_up.GetOutput()).GetSpectrum(),"upsampling",show=False)
        # plot.relabelandplot(sumpf.modules.FourierTransform(model_lp.GetOutput()).GetSpectrum(),"lowpass",show=True)

        # plot.relabelandplot(model_simple.GetOutput(),"simple",show=False)
        # plot.relabelandplot(model_up.GetOutput(),"upsampling",show=False)
        # plot.relabelandplot(model_lp.GetOutput(),"lowpass",show=True)

        print "maxharmonics: %r" % max_harm
        print "snr between simple HGM and upsampling HGM: %r" % nlsp.snr(
            model_simple.GetOutput(), model_up.GetOutput())
        print "snr between simple HGM and lowpass HGM: %r" % nlsp.snr(
            model_simple.GetOutput(), model_lp.GetOutput())
        print
Exemple #13
0
def db_evaluation_all(branches=3, Plot=False):

    branches = branches
    sampling_rate = 48000.0
    length = 2**18
    start_freq = 100.0
    stop_freq = 20000.0
    fade_out = 0.02
    fade_in = 0.02
    filter_taps = 2**10
    iterations = 5
    source_dir = "C:/Users/diplomand.8/Desktop/nl_recordings/rec_4_db/"

    # input generator
    sine_g = nlsp.NovakSweepGenerator_Sine(sampling_rate=sampling_rate,
                                           length=length,
                                           start_frequency=start_freq,
                                           stop_frequency=stop_freq,
                                           fade_out=fade_out,
                                           fade_in=fade_in)
    cos_g = nlsp.NovakSweepGenerator_Cosine(sampling_rate=sampling_rate,
                                            length=length,
                                            start_frequency=start_freq,
                                            stop_frequency=stop_freq,
                                            fade_out=fade_out,
                                            fade_in=fade_in)

    # real world reference system, load input and output noise and sweeps
    load_noise = sumpf.modules.SignalFile(
        filename=os.path.join(source_dir, "Noise18.npz"),
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    load_sine = sumpf.modules.SignalFile(
        filename=os.path.join(source_dir, "sine_f.npz"),
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    load_cosine = sumpf.modules.SignalFile(
        filename=os.path.join(source_dir, "cos_f.npz"),
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    load_sample = sumpf.modules.SignalFile(
        filename=os.path.join(source_dir, "Speech1.npz"),
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    output_sample = sumpf.modules.SplitSignal(data=load_sample.GetSignal(),
                                              channels=[1]).GetOutput()
    input_sample = sumpf.modules.SplitSignal(data=load_sample.GetSignal(),
                                             channels=[0]).GetOutput()
    output_sample = nlsp.change_length_signal(output_sample, 2**18)
    input_sample = nlsp.change_length_signal(input_sample, 2**18)
    output_noise = sumpf.modules.SplitSignal(data=load_noise.GetSignal(),
                                             channels=[1]).GetOutput()
    input_noise = sumpf.modules.SplitSignal(data=load_noise.GetSignal(),
                                            channels=[0]).GetOutput()
    output_sine = sumpf.modules.SplitSignal(data=load_sine.GetSignal(),
                                            channels=[1]).GetOutput()
    output_cos = sumpf.modules.SplitSignal(data=load_cosine.GetSignal(),
                                           channels=[1]).GetOutput()
    impulse = sumpf.modules.ImpulseGenerator(
        length=filter_taps, samplingrate=sampling_rate).GetSignal()

    # linear identification
    kernel_linear = nlsp.linear_identification_temporalreversal(
        sine_g, output_sine)
    iden_nlsystem_linear = nlsp.AliasCompensatingHammersteinModelUpandDown(
        filter_impulseresponse=kernel_linear)

    # only sine based system identification
    # found_filter_spec_sine, nl_function_sine = nlsp.systemidentification("LS",nlsp.nonlinearconvolution_powerseries_temporalreversal,
    #                                                                      branches,sine_g,output_sine)
    found_filter_spec_sine, nl_function_sine = nlsp.nonlinearconvolution_powerseries_temporalreversal(
        sine_g, output_sine, branches)
    iden_nlsystem_sine = nlsp.HammersteinGroupModel_up(
        max_harmonics=range(1, branches + 1),
        nonlinear_functions=nl_function_sine,
        filter_irs=found_filter_spec_sine)

    # only cosine based system identification
    # found_filter_spec_cos, nl_function_cos = nlsp.systemidentification("LS",nlsp.nonlinearconvolution_chebyshev_temporalreversal,
    #                                                                    branches,cos_g,output_cos)
    found_filter_spec_cos, nl_function_cos = nlsp.nonlinearconvolution_chebyshev_temporalreversal(
        cos_g, output_cos, branches)
    iden_nlsystem_cos = nlsp.HammersteinGroupModel_up(
        max_harmonics=range(1, branches + 1),
        nonlinear_functions=nl_function_cos,
        filter_irs=found_filter_spec_cos)

    # only noise based system identification
    found_filter_spec_adapt, nl_function_adapt = nlsp.adaptive_identification_powerseries(
        input_generator=input_noise,
        outputs=output_noise,
        iterations=iterations,
        branches=branches,
        step_size=0.1,
        filtertaps=filter_taps,
        algorithm=nlsp.multichannel_nlms,
        Plot=False)
    iden_nlsystem_adapt = nlsp.HammersteinGroupModel_up(
        max_harmonics=range(1, branches + 1),
        nonlinear_functions=nl_function_adapt,
        filter_irs=found_filter_spec_adapt)

    # # sine based as init coeff for noise based system identification
    found_filter_spec_sine_reducedlength = nlsp.change_length_filterkernels(
        found_filter_spec_sine, length=filter_taps)
    found_filter_spec_sineadapt, nl_function_sineadapt = nlsp.adaptive_identification_powerseries(
        input_generator=input_noise,
        outputs=output_noise,
        iterations=iterations,
        branches=branches,
        step_size=0.1,
        filtertaps=filter_taps,
        algorithm=nlsp.multichannel_nlms,
        Plot=False,
        init_coeffs=found_filter_spec_sine_reducedlength)
    iden_nlsystem_sineadapt = nlsp.HammersteinGroupModel_up(
        max_harmonics=range(1, branches + 1),
        nonlinear_functions=nl_function_sineadapt,
        filter_irs=found_filter_spec_sineadapt)

    # cos based as init coeff for noise based system identification
    found_filter_spec_cos_reducedlength = nlsp.change_length_filterkernels(
        found_filter_spec_cos, length=filter_taps)
    found_filter_spec_cosadapt, nl_function_cosadapt = nlsp.adaptive_identification_powerseries(
        input_generator=input_noise,
        outputs=output_noise,
        iterations=iterations,
        branches=branches,
        step_size=0.1,
        filtertaps=filter_taps,
        algorithm=nlsp.multichannel_nlms,
        Plot=False,
        init_coeffs=found_filter_spec_cos_reducedlength,
        nonlinear_func=nlsp.function_factory.chebyshev1_polynomial)
    iden_nlsystem_cosadapt = nlsp.HammersteinGroupModel_up(
        max_harmonics=range(1, branches + 1),
        nonlinear_functions=nl_function_cosadapt,
        filter_irs=found_filter_spec_cosadapt)

    # set excitation as input
    iden_nlsystem_linear.SetInput(sine_g.GetOutput())
    iden_nlsystem_sine.SetInput(sine_g.GetOutput())
    iden_nlsystem_cos.SetInput(cos_g.GetOutput())
    iden_nlsystem_adapt.SetInput(input_noise)
    iden_nlsystem_sineadapt.SetInput(input_noise)
    iden_nlsystem_cosadapt.SetInput(input_noise)

    if Plot is True:
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_sine.GetOutput()).GetSpectrum(),
                            "Identified power",
                            show=False)
        plot.relabelandplot(
            sumpf.modules.FourierTransform(output_sine).GetSpectrum(),
            "Reference power",
            show=True)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_cos.GetOutput()).GetSpectrum(),
                            "Identified cheby",
                            show=False)
        plot.relabelandplot(
            sumpf.modules.FourierTransform(output_cos).GetSpectrum(),
            "Reference cheby",
            show=True)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_adapt.GetOutput()).GetSpectrum(),
                            "Identified Adapt noise",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_sineadapt.GetOutput()).GetSpectrum(),
                            "Identified power Adapt noise",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_cosadapt.GetOutput()).GetSpectrum(),
                            "Identified chebyshev Adapt noise",
                            show=False)
        plot.relabelandplot(
            sumpf.modules.FourierTransform(output_noise).GetSpectrum(),
            "Reference Adapt noise",
            show=True)

    print "SNR between Reference and Identified output linear: %r" % nlsp.snr(
        output_sine, iden_nlsystem_linear.GetOutput())
    print "SNR between Reference and Identified output Powerseries NL convolution: %r" % nlsp.snr(
        output_sine, iden_nlsystem_sine.GetOutput())
    print "SNR between Reference and Identified output Chebyshev NL convolution: %r" % nlsp.snr(
        output_cos, iden_nlsystem_cos.GetOutput())
    print "SNR between Reference and Identified output Adaptive: %r" % nlsp.snr(
        output_noise, iden_nlsystem_adapt.GetOutput())
    print "SNR between Reference and Identified output Powerseries Adaptive: %r" % nlsp.snr(
        output_noise, iden_nlsystem_sineadapt.GetOutput())
    print "SNR between Reference and Identified output Chebyshev Adaptive: %r" % nlsp.snr(
        output_noise, iden_nlsystem_cosadapt.GetOutput())

    # set sample as input
    iden_nlsystem_linear.SetInput(input_sample)
    iden_nlsystem_sine.SetInput(input_sample)
    iden_nlsystem_cos.SetInput(input_sample)
    iden_nlsystem_adapt.SetInput(input_sample)
    # iden_nlsystem_sineadapt.SetInput(input_sample)
    # iden_nlsystem_cosadapt.SetInput(input_sample)

    if Plot is True:
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_sine.GetOutput()).GetSpectrum(),
                            "Identified power",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_cos.GetOutput()).GetSpectrum(),
                            "Identified cheby",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_adapt.GetOutput()).GetSpectrum(),
                            "Identified Adapt noise",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_sineadapt.GetOutput()).GetSpectrum(),
                            "Identified power Adapt noise",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_cosadapt.GetOutput()).GetSpectrum(),
                            "Identified chebyshev Adapt noise",
                            show=False)
        plot.relabelandplot(
            sumpf.modules.FourierTransform(output_sample).GetSpectrum(),
            "Reference Adapt noise",
            show=True)

    print "SNR between Reference and Identified output linear: %r" % nlsp.snr(
        output_sample, iden_nlsystem_linear.GetOutput())
    print "SNR between Reference and Identified output Powerseries NL convolution: %r" % nlsp.snr(
        output_sample, iden_nlsystem_sine.GetOutput())
    print "SNR between Reference and Identified output Chebyshev NL convolution: %r" % nlsp.snr(
        output_sample, iden_nlsystem_cos.GetOutput())
    print "SNR between Reference and Identified output Adaptive: %r" % nlsp.snr(
        output_sample, iden_nlsystem_adapt.GetOutput())
    print "SNR between Reference and Identified output Powerseries Adaptive: %r" % nlsp.snr(
        output_sample, iden_nlsystem_sineadapt.GetOutput())
    print "SNR between Reference and Identified output Chebyshev Adaptive: %r" % nlsp.snr(
        output_sample, iden_nlsystem_cosadapt.GetOutput())
Exemple #14
0
def linearmodel_evaluation(input_generator,
                           branches,
                           nlfunction,
                           Plot,
                           reference=None):
    input_signal = input_generator.GetOutput()
    prp = sumpf.modules.ChannelDataProperties()
    prp.SetSignal(input_signal)
    filter_ir = sumpf.modules.FilterGenerator(
        filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH(order=10),
        frequency=10000.0,
        transform=False,
        resolution=prp.GetResolution(),
        length=prp.GetSpectrumLength()).GetSpectrum()
    ref_nlsystem = nlsp.AliasCompensatingHammersteinModelUpandDown(
        filter_impulseresponse=sumpf.modules.InverseFourierTransform(
            filter_ir).GetSignal())

    sweep = nlsp.NovakSweepGenerator_Sine(
        sampling_rate=input_signal.GetSamplingRate(), length=len(input_signal))
    ref_nlsystem.SetInput(sweep.GetOutput())
    init_coeffs, non = nlsp.nonlinearconvolution_powerseries_temporalreversal(
        sweep, ref_nlsystem.GetOutput(), branches=branches)
    init_coeffs = nlsp.change_length_filterkernels(init_coeffs, filter_length)
    ref_nlsystem.SetInput(input_signal)
    found_filter_spec, nl_functions = nlsp.adaptive_identification_legendre(
        input_generator=input_generator,
        outputs=ref_nlsystem.GetOutput(),
        branches=branches,
        init_coeffs=init_coeffs)
    iden_nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input_signal,
        nonlinear_functions=nl_functions,
        filter_irs=found_filter_spec,
        max_harmonics=range(1, branches + 1))

    # linear system identification
    sweep = nlsp.NovakSweepGenerator_Sine(
        length=len(input_signal), sampling_rate=input_signal.GetSamplingRate())
    ref_nlsystem.SetInput(sweep.GetOutput())
    kernel_linear = nlsp.linear_identification_temporalreversal(
        sweep, ref_nlsystem.GetOutput())
    iden_linsystem = nlsp.AliasCompensatingHammersteinModelUpandDown(
        filter_impulseresponse=kernel_linear)

    if reference is not None:
        reference = nlsp.change_length_signal(reference,
                                              length=len(input_signal))
        ref_nlsystem.SetInput(reference)
        iden_linsystem.SetInput(reference)
        iden_nlsystem.SetInput(reference)
    if Plot is True:
        plot.relabelandplot(sumpf.modules.FourierTransform(
            ref_nlsystem.GetOutput()).GetSpectrum(),
                            "Reference System",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem.GetOutput()).GetSpectrum(),
                            "Identified System",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_linsystem.GetOutput()).GetSpectrum(),
                            "Identified linear System",
                            show=True)
    print "SNR between Reference and Identified output for linear systems: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
    print "SNR between Reference and Identified output for linear systems(linear identification): %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_linsystem.GetOutput())
def linearmodel_evaluation(input_generator,
                           branches,
                           nlfunction,
                           iden_method,
                           Plot,
                           reference=None):
    """
    Evaluation of System Identification method by linear amplification
    nonlinear system - no nonlinearity, linear amplifier as linear system
    inputsignal - signal signal
    plot - the virtual linear system output and the identified linear system output
    expectation - utmost similarity between the two outputs
    """
    input_signal = input_generator.GetOutput()
    prp = sumpf.modules.ChannelDataProperties()
    prp.SetSignal(input_signal)
    filter_ir = sumpf.modules.FilterGenerator(
        filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH(order=10),
        frequency=10000.0,
        transform=False,
        resolution=prp.GetResolution(),
        length=prp.GetSpectrumLength()).GetSpectrum()
    ref_nlsystem = nlsp.AliasingCompensatedHM_upsampling(
        filter_impulseresponse=sumpf.modules.InverseFourierTransform(
            filter_ir).GetSignal())
    ref_nlsystem.SetInput(input_signal)

    # nonlinear system identification
    found_filter_spec, nl_functions = iden_method(input_generator,
                                                  ref_nlsystem.GetOutput(),
                                                  branches)
    iden_nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input_signal,
        nonlinear_functions=nl_functions,
        filter_irs=found_filter_spec,
        max_harmonics=range(1, branches + 1))

    # linear system identification
    sweep = nlsp.NovakSweepGenerator_Sine(
        length=len(input_signal), sampling_rate=input_signal.GetSamplingRate())
    ref_nlsystem.SetInput(sweep.GetOutput())
    kernel_linear = nlsp.linear_identification_temporalreversal(
        sweep, ref_nlsystem.GetOutput())
    iden_linsystem = nlsp.AliasCompensatingHammersteinModelUpandDown(
        filter_impulseresponse=kernel_linear)

    if reference is not None:
        reference = nlsp.change_length_signal(reference,
                                              length=len(input_signal))
        ref_nlsystem.SetInput(reference)
        iden_linsystem.SetInput(reference)
        iden_nlsystem.SetInput(reference)
    if Plot is True:
        plot.relabelandplot(sumpf.modules.FourierTransform(
            ref_nlsystem.GetOutput()).GetSpectrum(),
                            "Reference System",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem.GetOutput()).GetSpectrum(),
                            "Identified System",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_linsystem.GetOutput()).GetSpectrum(),
                            "Identified linear System",
                            show=True)
    print "SNR between Reference and Identified output for linear systems: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
    print "SNR between Reference and Identified output for linear systems(linear identification): %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_linsystem.GetOutput())