Exemplo n.º 1
0
def clippingHGMevaluation(input_generator, branches, Plot, reference=None):
    for t in range(8, 11):
        t = t / 10.0
        thresholds = [-t, t]
        input_signal = input_generator.GetOutput()
        nl_functions = [
            nlsp.function_factory.hardclip(thresholds),
        ] * branches
        filter_spec_tofind = nlsp.log_bpfilter(branches=branches,
                                               input=input_signal)
        ref_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_signal,
            nonlinear_functions=nl_functions,
            filter_irs=filter_spec_tofind,
            max_harmonics=range(1, branches + 1))

        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))
        # sine = sumpf.modules.SineWaveGenerator(frequency=5000.0,phase=0.0,samplingrate=input_signal.GetSamplingRate(),length=len(input_signal)).GetSignal()
        sine = sumpf.modules.SweepGenerator(
            samplingrate=input_signal.GetSamplingRate(),
            length=len(input_signal)).GetSignal()
        ref_nlsystem.SetInput(sine)
        iden_nlsystem.SetInput(sine)
        if reference is not None:
            reference = nlsp.change_length_signal(reference,
                                                  length=len(input_signal))
            ref_nlsystem.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)
        print "SNR between Reference and Identified output for symmetric hardclipping HGM(thresholds:%r): %r" % (
            thresholds,
            nlsp.snr(ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput()))
Exemplo n.º 2
0
def adaptive_differentlengthanditerations():
    # generate virtual nonlinear system using HGM
    ref_nlsystem = nlsp.HammersteinGroupModel_up(nonlinear_functions=nlsp.nl_branches(nlsp.function_factory.power_series,branches),
                                                 filter_irs=filter_spec_tofind_noise,
                                                 max_harmonics=range(1,branches+1))

    # give input and get output from the virtual nlsystem
    ref_nlsystem.SetInput(input)
    output_noise = ref_nlsystem.GetOutput()
    input_noise = input

    # only noise based system identification
    found_filter_spec_adapt_hermite, nl_function_adapt_hermite = nlsp.adaptive_identification_hermite(input_generator=input_noise,outputs=output_noise,
                                                                                                      branches=branches,iterations=i,filtertaps=filter_taps,Print=True)

    iden_nlsystem_adapt_hermite = nlsp.HammersteinGroupModel_up(max_harmonics=range(1,branches+1),nonlinear_functions=nl_function_adapt_hermite,
                                                  filter_irs=found_filter_spec_adapt_hermite)


    # set reference input to virtual nlsystem and identified nl system
    ref_nlsystem.SetInput(reference)
    iden_nlsystem_adapt_hermite.SetInput(reference)

    # calculate snr value
    adaptive_hermite_snr = nlsp.snr(ref_nlsystem.GetOutput(),iden_nlsystem_adapt_hermite.GetOutput())

    # print snr value
    print "adaptive_hermite_snr: %r" %adaptive_hermite_snr
Exemplo n.º 3
0
def differentlength_evaluation(input_generator,
                               branches,
                               Plot,
                               reference=None):
    length_ref = [2**15, 2**16, 2**17]
    length_iden = [2**15, 2**16, 2**17]
    input_generator_ref = input_generator
    input_generator_iden = input_generator
    for signal_length, ref_length in zip(length_iden, length_ref):
        input_generator_ref.SetLength(ref_length)
        input_ref = input_generator_ref.GetOutput()
        filter_spec_tofind = nlsp.log_weightingfilter(branches=branches,
                                                      input=input_ref)
        ref_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_ref,
            nonlinear_functions=nlsp.nl_branches(
                nlsp.function_factory.power_series, branches),
            filter_irs=filter_spec_tofind,
            max_harmonics=range(1, branches + 1))

        sweep = nlsp.NovakSweepGenerator_Sine(
            sampling_rate=input_ref.GetSamplingRate(), length=len(input_ref))
        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_ref)
        found_filter_spec, nl_functions = nlsp.adaptive_identification_legendre(
            input_generator=input_generator_ref,
            outputs=ref_nlsystem.GetOutput(),
            branches=branches,
            init_coeffs=init_coeffs)

        input_generator_iden.SetLength(signal_length)
        input_iden = input_generator_iden.GetOutput()
        iden_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_iden,
            nonlinear_functions=nl_functions,
            filter_irs=found_filter_spec,
            max_harmonics=range(1, branches + 1))
        if reference is not None:
            reference = nlsp.change_length_signal(reference,
                                                  length=len(input_ref))
            ref_nlsystem.SetInput(reference)
            iden_nlsystem.SetInput(reference)

        if Plot is True:
            plot.relabelandplotphase(
                sumpf.modules.FourierTransform(
                    ref_nlsystem.GetOutput()).GetSpectrum(),
                "Reference Output", False)
            plot.relabelandplotphase(
                sumpf.modules.FourierTransform(
                    iden_nlsystem.GetOutput()).GetSpectrum(),
                "Identified Output", True)
        print "SNR between Reference(length:%r) and Identified output(length:%r) : %r" % (
            len(input_ref), len(input_iden),
            nlsp.snr(ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput()))
Exemplo n.º 4
0
def clippingHGMevaluation(input_generator,
                          branches,
                          iden_method,
                          Plot,
                          reference=None):
    """
    Evaluation of System Identification method by hard clipping system
    nonlinear system - virtual clipping systems which hard clips the signal amplitute which are not in the threshold range
    plot - the virtual nl system output and the identified nl system output
    expectation - utmost similarity between the two outputs
    """
    for t in range(8, 11):
        t = t / 10.0
        thresholds = [-t, t]
        input_signal = input_generator.GetOutput()
        nl_functions = [
            nlsp.function_factory.hardclip(thresholds),
        ] * branches
        filter_spec_tofind = nlsp.log_bpfilter(branches=branches,
                                               input=input_signal)
        ref_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_signal,
            nonlinear_functions=nl_functions,
            filter_irs=filter_spec_tofind,
            max_harmonics=range(1, branches + 1))

        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))
        # sine = sumpf.modules.SineWaveGenerator(frequency=5000.0,phase=0.0,samplingrate=input_signal.GetSamplingRate(),length=len(input_signal)).GetSignal()
        sine = sumpf.modules.SweepGenerator(
            samplingrate=input_signal.GetSamplingRate(),
            length=len(input_signal)).GetSignal()
        ref_nlsystem.SetInput(sine)
        iden_nlsystem.SetInput(sine)
        if reference is not None:
            reference = nlsp.change_length_signal(reference,
                                                  length=len(input_signal))
            ref_nlsystem.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)
        print "SNR between Reference and Identified output for symmetric hardclipping HGM(thresholds:%r): %r" % (
            thresholds,
            nlsp.snr(ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput()))
Exemplo n.º 5
0
def robustness_excitation_evaluation(input_generator,
                                     branches,
                                     Plot,
                                     reference=None):

    excitation_signal_amp = [0.5, 1.0]
    sample_signal_amp = [0.5, 1.0, 2.0]
    input = input_generator.GetOutput()
    for excitation_amp, sample_amp in itertools.product(
            excitation_signal_amp, sample_signal_amp):
        input_signal = sumpf.modules.AmplifySignal(
            input=input, factor=excitation_amp).GetOutput()
        sample_signal = nlsp.WhiteGaussianGenerator(
            sampling_rate=input_signal.GetSamplingRate(),
            length=len(input_signal),
            distribution=sumpf.modules.NoiseGenerator.UniformDistribution(
                minimum=-sample_amp, maximum=sample_amp))
        sample_signal = sample_signal.GetOutput()
        filter_spec_tofind = nlsp.log_weightingfilter(branches=branches,
                                                      input=input_signal)
        ref_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_signal,
            nonlinear_functions=nlsp.nl_branches(
                nlsp.function_factory.power_series, branches),
            filter_irs=filter_spec_tofind,
            max_harmonics=range(1, branches + 1))
        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=sample_signal,
            nonlinear_functions=nl_functions,
            filter_irs=found_filter_spec,
            max_harmonics=range(1, branches + 1))
        ref_nlsystem.SetInput(sample_signal)
        if Plot is True:
            nlsp.relabelandplotphase(
                sumpf.modules.FourierTransform(
                    ref_nlsystem.GetOutput()).GetSpectrum(),
                "Reference Output Scaled", False)
            nlsp.relabelandplot(
                sumpf.modules.FourierTransform(
                    iden_nlsystem.GetOutput()).GetSpectrum(),
                "Identified Output", True)
        print "SNR between Scaled Identified with(amp:%r) and Tested with(amp:%r) output: %r" % (
            excitation_amp, sample_amp,
            nlsp.snr(ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput()))
def differentlength_evaluation(input_generator,
                               branches,
                               iden_method,
                               Plot,
                               reference=None):
    """
    Evaluation of System Identification method by hgm virtual nl system
    nonlinear system - virtual hammerstein group model with power series polynomials as nl function and bandpass filters
                        as linear functions
    inputsignal - signal signal
    plot - the original filter spectrum and the identified filter spectrum, the reference output and identified output
    expectation - utmost similarity between the two spectrums
    """
    length_ref = [2**15, 2**16, 2**17]
    length_iden = [2**15, 2**16, 2**17]
    input_generator_ref = input_generator
    input_generator_iden = input_generator
    for signal_length, ref_length in zip(length_iden, length_ref):
        input_generator_ref.SetLength(ref_length)
        input_ref = input_generator_ref.GetOutput()
        filter_spec_tofind = nlsp.log_weightingfilter(branches=branches,
                                                      input=input_ref)
        ref_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_ref,
            nonlinear_functions=nlsp.nl_branches(
                nlsp.function_factory.power_series, branches),
            filter_irs=filter_spec_tofind,
            max_harmonics=range(1, branches + 1))
        found_filter_spec, nl_functions = iden_method(input_generator,
                                                      ref_nlsystem.GetOutput(),
                                                      branches)

        input_generator_iden.SetLength(signal_length)
        input_iden = input_generator_iden.GetOutput()
        iden_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_iden,
            nonlinear_functions=nl_functions,
            filter_irs=found_filter_spec,
            max_harmonics=range(1, branches + 1))
        if reference is not None:
            reference = nlsp.change_length_signal(reference,
                                                  length=len(input_ref))
            ref_nlsystem.SetInput(reference)
            iden_nlsystem.SetInput(reference)

        if Plot is True:
            plot.relabelandplotphase(
                sumpf.modules.FourierTransform(
                    ref_nlsystem.GetOutput()).GetSpectrum(),
                "Reference Output", False)
            plot.relabelandplotphase(
                sumpf.modules.FourierTransform(
                    iden_nlsystem.GetOutput()).GetSpectrum(),
                "Identified Output", True)
        print "SNR between Reference(length:%r) and Identified output(length:%r) : %r" % (
            len(input_ref), len(input_iden),
            nlsp.snr(ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput()))
def robustness_excitation_evaluation(input_generator,
                                     branches,
                                     iden_method,
                                     Plot,
                                     reference=None):

    excitation_signal_amp = [0.5, 1.0, 2.0]
    sample_signal_amp = [0.5, 1.0, 2.0]
    input_s = input_generator.GetOutput()
    sample_signal = sumpf.modules.NoiseGenerator(
        distribution=sumpf.modules.NoiseGenerator.UniformDistribution(),
        samplingrate=input_s.GetSamplingRate(),
        length=len(input_s)).GetSignal()
    sample_signal = nlsp.RemoveOutliers(thresholds=[-1.0, 1.0],
                                        signal=sample_signal,
                                        value=0.0)
    sample_signal = sample_signal.GetOutput()
    for excitation_amp, sample_amp in itertools.product(
            excitation_signal_amp, sample_signal_amp):
        input_generator.SetFactor(excitation_amp)
        input_signal = input_generator.GetOutput()
        sample_signal = sumpf.modules.AmplifySignal(
            input=sample_signal, factor=sample_amp).GetOutput()
        filter_spec_tofind = nlsp.log_bpfilter(branches=branches,
                                               input=input_signal)
        filter_spec_tofind = [i for i in reversed(filter_spec_tofind)]
        filter_length = len(filter_spec_tofind[0])
        ref_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_signal,
            nonlinear_functions=nlsp.nl_branches(
                nlsp.function_factory.power_series, branches),
            filter_irs=filter_spec_tofind,
            max_harmonics=range(1, branches + 1))
        found_filter_spec, nl_functions = iden_method(input_generator,
                                                      ref_nlsystem.GetOutput(),
                                                      branches)
        found_filter_spec = nlsp.change_length_filterkernels(
            found_filter_spec, length=filter_length)
        iden_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=sample_signal,
            nonlinear_functions=nl_functions,
            filter_irs=found_filter_spec,
            max_harmonics=range(1, branches + 1))
        ref_nlsystem.SetInput(sample_signal)
        if Plot is True:
            nlsp.relabelandplotphase(
                sumpf.modules.FourierTransform(
                    ref_nlsystem.GetOutput()).GetSpectrum(),
                "Reference Output Scaled", False)
            nlsp.relabelandplot(
                sumpf.modules.FourierTransform(
                    iden_nlsystem.GetOutput()).GetSpectrum(),
                "Identified Output", True)
        print "SNR between Scaled Identified with(amp:%r) and Tested with(amp:%r) output: %r" % (
            excitation_amp, sample_amp,
            nlsp.snr(ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput()))
Exemplo n.º 8
0
def computationtime_evaluation(input_generator,
                               branches,
                               iden_method,
                               Plot,
                               rangevalue=10,
                               save=False):

    inputgenerator = input_generator
    branch = reversed(range(2, branches + 1))
    length = reversed([2**14, 2**15, 2**16])
    for branches, signal_length in itertools.product(branch, length):
        sim = []
        iden = []
        reference = nlsp.WhiteGaussianGenerator(
            sampling_rate=input_generator.GetOutput().GetSamplingRate(),
            length=signal_length,
            distribution=sumpf.modules.NoiseGenerator.LaplaceDistribution())
        reference = reference.GetOutput()
        for i in range(rangevalue):
            inputgenerator.SetLength(signal_length)
            input_signal = inputgenerator.GetOutput()
            filter_spec_tofind = nlsp.log_bpfilter(branches=branches,
                                                   input=input_signal)
            nl_func = nlsp.nl_branches(nlsp.function_factory.power_series,
                                       branches)
            ref_nlsystem = nlsp.HammersteinGroupModel_up(
                input_signal=input_signal,
                nonlinear_functions=nl_func,
                filter_irs=filter_spec_tofind,
                max_harmonics=range(1, branches + 1))
            identification_time_start = time.clock()
            found_filter_spec, nl_functions = iden_method(
                input_generator, ref_nlsystem.GetOutput(), branches)
            identification_time_stop = time.clock()
            if save is True:
                found_filter_spec_save, nl_functions_save = nlsp.systemidentification(
                    "powerhgmweight", iden_method, branches, inputgenerator,
                    ref_nlsystem.GetOutput())
            iden_nlsystem = nlsp.HammersteinGroupModel_up(
                input_signal=input_signal,
                nonlinear_functions=nl_functions,
                filter_irs=found_filter_spec,
                max_harmonics=range(1, branches + 1))
            iden_nlsystem.GetOutput()
            simulation_time_start = time.clock()
            iden_nlsystem.SetInput(reference)
            iden_nlsystem.GetOutput()
            simulation_time_stop = time.clock()
            simulation_time = simulation_time_stop - simulation_time_start
            identification_time = identification_time_stop - identification_time_start
            iden.append(identification_time)
            sim.append(simulation_time)

        print "Signal length: %r, branches: %r, simulation time: %r, identification time: %r" % (
            signal_length, branches, numpy.average(sim), numpy.average(iden))
Exemplo n.º 9
0
def hgmallpass_evaluation(input_generator,
                          branches,
                          nlfunction,
                          Plot,
                          reference=None):
    input_signal = input_generator.GetOutput()
    allpass = sumpf.modules.ImpulseGenerator(
        samplingrate=input_signal.GetSamplingRate(),
        length=len(input_signal)).GetSignal()
    filter_spec_tofind = [
        allpass,
    ] * branches
    ref_nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input_signal,
        nonlinear_functions=nlsp.nl_branches(nlfunction, branches),
        filter_irs=filter_spec_tofind,
        max_harmonics=range(1, branches + 1))

    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))
    if reference is not None:
        reference = nlsp.change_length_signal(reference,
                                              length=len(input_signal))
        ref_nlsystem.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=True)
    print "SNR between Reference and Identified output with all pass filters: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
def puretone_evaluation(input_generator,
                        branches,
                        iden_method,
                        Plot,
                        reference=None):
    input_signal = input_generator.GetOutput()

    filter_spec_tofind = nlsp.log_bpfilter(branches=branches,
                                           input=input_signal)
    ref_nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input_signal,
        nonlinear_functions=nlsp.nl_branches(
            nlsp.function_factory.power_series, branches),
        filter_irs=filter_spec_tofind,
        max_harmonics=range(1, branches + 1))

    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))
    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=True)
    print "SNR between Reference and Identified output sweep: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
    pure_tones = nlsp.generate_puretones([200, 1000, 3000, 5000, 10000, 20000],
                                         input_signal.GetSamplingRate(),
                                         length=len(input_signal))
    ref_nlsystem.SetInput(pure_tones)
    iden_nlsystem.SetInput(pure_tones)
    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=True)
    print "SNR between Reference and Identified output puretone: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
def hgmallpass_evaluation(input_generator,
                          branches,
                          nlfunction,
                          iden_method,
                          Plot,
                          reference=None):
    """
    Evaluation of System Identification method by hgm virtual nl system
    nonlinear system - virtual hammerstein group model with power series polynomials as nl function and allpass filters
                        as linear functions
    plot - the original filter spectrum and the identified filter spectrum, the reference output and identified output
    expectation - utmost similarity between the two spectrums
    """
    input_signal = input_generator.GetOutput()
    allpass = sumpf.modules.ImpulseGenerator(
        samplingrate=input_signal.GetSamplingRate(),
        length=len(input_signal)).GetSignal()
    filter_spec_tofind = [
        allpass,
    ] * branches
    ref_nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input_signal,
        nonlinear_functions=nlsp.nl_branches(nlfunction, branches),
        filter_irs=filter_spec_tofind,
        max_harmonics=range(1, branches + 1))

    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))
    if reference is not None:
        reference = nlsp.change_length_signal(reference,
                                              length=len(input_signal))
        ref_nlsystem.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=True)
    print "SNR between Reference and Identified output with all pass filters: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
def hgmwithoverlapfilter_evaluation(input_generator,
                                    branches,
                                    nlfunction,
                                    iden_method,
                                    Plot,
                                    reference=None):
    """
    Evaluation of System Identification method by hgm virtual nl system
    nonlinear system - virtual hammerstein group model with power series polynomials as nl function and overlapping
                       bandpass filters as linear functions
    plot - the original filter spectrum and the identified filter spectrum, the reference output and identified output
    expectation - utmost similarity between the two spectrums
    """
    frequencies = [500, 3000, 5000, 7000, 20000]
    input_signal = input_generator.GetOutput()
    filter_spec_tofind = nlsp.create_bpfilter(frequencies, input_signal)
    length_kernel = len(filter_spec_tofind[0])
    ref_nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input_signal,
        nonlinear_functions=nlsp.nl_branches(nlfunction, branches),
        filter_irs=filter_spec_tofind,
        max_harmonics=range(1, branches + 1))

    found_filter_spec, nl_functions = iden_method(input_generator,
                                                  ref_nlsystem.GetOutput(),
                                                  branches)
    found_filter_spec = nlsp.change_length_filterkernels(found_filter_spec,
                                                         length=length_kernel)
    iden_nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input_signal,
        nonlinear_functions=nl_functions,
        filter_irs=found_filter_spec,
        max_harmonics=range(1, branches + 1))
    if reference is not None:
        reference = nlsp.change_length_signal(reference,
                                              length=len(input_signal))
        ref_nlsystem.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=True)
    print "SNR between Reference and Identified output with overlapping filters: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
def comparereversedandsi():
    sampling_rate = 48000
    length = 2**16
    branches = 5
    excitation = nlsp.NovakSweepGenerator_Sine(start_frequency=20.0,stop_frequency=20000.0,
                                              length=length,sampling_rate=sampling_rate)
    input_signal = excitation.GetOutput()
    input_spec = sumpf.modules.FourierTransform(input_signal).GetSpectrum()
    prp = sumpf.modules.ChannelDataProperties()
    prp.SetSignal(input_signal)
    filter_spec_tofind = sumpf.modules.FilterGenerator(filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH(order=100),
                                                   frequency=20.0,transform=True,resolution=prp.GetResolution(),
                                                   length=prp.GetSpectrumLength()).GetSpectrum()
    filter_spec_tofind = [sumpf.modules.InverseFourierTransform(filter_spec_tofind).GetSignal(),]*branches
    ref_nlsystem = nlsp.HammersteinGroupModel_up(input_signal=input_signal,
                                                 nonlinear_functions=nlsp.nl_branches(nlsp.function_factory.power_series,branches),
                                                 filter_irs=filter_spec_tofind,
                                                 max_harmonics=range(1,branches+1))
    nlsp.plotstft(ref_nlsystem.GetOutput())
    nlsp.plotstft(input_signal)
    response = ref_nlsystem.GetOutput()
    response_spec = sumpf.modules.FourierTransform(response).GetSpectrum()
    reg_inv = sumpf.modules.RegularizedSpectrumInversion(spectrum=input_spec,start_frequency=20.0,stop_frequency=6000.0,epsilon_max=0.1).GetOutput()
    reg_specdivision = sumpf.modules.MultiplySpectrums(spectrum1=reg_inv, spectrum2=response_spec)
    reg_tf = reg_specdivision.GetOutput()
    nl_ir_si = sumpf.modules.InverseFourierTransform(spectrum=reg_tf).GetSignal()

    rev = excitation.GetReversedOutput()
    rev_spec = sumpf.modules.FourierTransform(rev).GetSpectrum()
    out_spec = response_spec / response.GetSamplingRate()
    tf = rev_spec * out_spec
    ir_sweep = sumpf.modules.InverseFourierTransform(tf).GetSignal()

    plot.relabelandplot(nl_ir_si,"spectralinversion_ir",show=False)
    plot.relabelandplot(ir_sweep,"reverse_ir",show=True)
Exemplo n.º 14
0
def test_energy():
    """
    Test of the relation between the energy of hammerstein group model and simple hammerstein model.
    (a + b)^2 <= 2a^2 + 2b^2
    The above relation is proved for the model using this test
    """
    max_harm = [1] * 2
    freq = 5000
    s_rate = 48000
    length = s_rate
    ip_sine_signal = sumpf.modules.SineWaveGenerator(
        frequency=freq, phase=0.0, samplingrate=s_rate,
        length=length).GetSignal()
    imp = sumpf.modules.ImpulseGenerator(samplingrate=s_rate,
                                         length=length).GetSignal()
    e = []
    for harm in max_harm:
        Test_Model_Hammerstein = nlsp.AliasingCompensatedHM_upsampling(
            input_signal=ip_sine_signal,
            nonlin_func=nlsp.function_factory.power_series(harm),
            filter_impulseresponse=imp,
            max_harm=harm)
        Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput()
        e.append(
            numpy.multiply(nlsp.calculateenergy_freq(Test_Model_outputsignal),
                           2))
    hammerstein_group = nlsp.HammersteinGroupModel_up(
        input_signal=ip_sine_signal,
        nonlinear_functions=(nlsp.function_factory.power_series(
            max_harm[0]), ) * len(max_harm),
        filter_irs=(imp, ) * len(max_harm),
        max_harmonics=max_harm)
    e_g = nlsp.calculateenergy_freq(hammerstein_group.GetOutput())
    assert float(numpy.sum(e_g)) <= float(numpy.sum(e))
Exemplo n.º 15
0
def doublehgm_samenl_evaluation(input_generator,
                                branches,
                                iden_method,
                                Plot,
                                reference=None):
    input_signal = input_generator.GetOutput()
    filter_spec_tofind1 = nlsp.log_bpfilter(branches=branches,
                                            input=input_signal)
    filter_spec_tofind2 = nlsp.log_chebyfilter(branches=branches,
                                               input=input_signal)
    ref_nlsystem = nlsp.HammersteinGroup_Series(
        input_signal=input_signal,
        nonlinear_functions=(nlsp.nl_branches(
            nlsp.function_factory.power_series, branches),
                             nlsp.nl_branches(
                                 nlsp.function_factory.power_series,
                                 branches)),
        filter_irs=(filter_spec_tofind1, filter_spec_tofind2),
        max_harmonics=(range(1, branches + 1), range(1, branches + 1)),
        hgm_type=(nlsp.HammersteinGroupModel_up,
                  nlsp.HammersteinGroupModel_up))

    found_filter_spec, nl_functions = iden_method(input_generator,
                                                  ref_nlsystem.GetOutput(2),
                                                  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))
    if reference is not None:
        reference = nlsp.change_length_signal(reference,
                                              length=len(input_signal))
        ref_nlsystem = nlsp.HammersteinGroup_Series(
            input_signal=reference,
            nonlinear_functions=(nlsp.nl_branches(
                nlsp.function_factory.power_series, branches),
                                 nlsp.nl_branches(
                                     nlsp.function_factory.power_series,
                                     branches)),
            filter_irs=(filter_spec_tofind1, filter_spec_tofind2),
            max_harmonics=(range(1, branches + 1), range(1, branches + 1)),
            hgm_type=(nlsp.HammersteinGroupModel_up,
                      nlsp.HammersteinGroupModel_up))
        iden_nlsystem.SetInput(reference)

    if Plot is True:
        plot.relabelandplotphase(sumpf.modules.FourierTransform(
            ref_nlsystem.GetOutput(2)).GetSpectrum(),
                                 "Reference System",
                                 show=False)
        plot.relabelandplotphase(sumpf.modules.FourierTransform(
            iden_nlsystem.GetOutput()).GetSpectrum(),
                                 "Identified System",
                                 show=True)
    print "SNR between Reference and Identified output for double hgm same nl: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(2), iden_nlsystem.GetOutput())
def uniqueness_evaluation_adaptive():
    for input_generator in excitation:
        print "adaptive identification"
        print input_generator
        for nlfunc_ref, nlfunc_iden in itertools.product(
                nl_functions_all, nl_functions_all):
            print "ref nl function %r" % nlfunc_ref
            print "iden nl function %r" % nlfunc_iden
            input_signal = input_generator.GetOutput()
            filter_spec_tofind = nlsp.log_bpfilter(branches=branches,
                                                   input=input_signal)
            ref_nlsystem = nlsp.HammersteinGroupModel_up(
                input_signal=input_signal,
                nonlinear_functions=nlsp.nl_branches(nlfunc_ref, branches),
                filter_irs=filter_spec_tofind,
                max_harmonics=range(1, branches + 1))
            found_filter_spec, nl_functions = nlsp.adaptive_identification(
                input_generator,
                ref_nlsystem.GetOutput(),
                branches,
                nonlinear_func=nlfunc_iden)
            iden_nlsystem = nlsp.HammersteinGroupModel_up(
                input_signal=input_signal,
                nonlinear_functions=nl_functions,
                filter_irs=found_filter_spec,
                max_harmonics=range(1, branches + 1))
            nlsp.filterkernel_evaluation_sum(filter_spec_tofind,
                                             found_filter_spec)
            nlsp.filterkernel_evaluation_plot(filter_spec_tofind,
                                              found_filter_spec)
            if Plot is True:
                plot.relabelandplot(sumpf.modules.FourierTransform(
                    ref_nlsystem.GetOutput()).GetSpectrum(),
                                    "Reference Output",
                                    show=False)
                plot.relabelandplot(sumpf.modules.FourierTransform(
                    iden_nlsystem.GetOutput()).GetSpectrum(),
                                    "Identified Output",
                                    show=True)
            print "SNR between Reference and Identified output without overlapping filters: %r" % nlsp.snr(
                ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
            print
        print
        print
Exemplo n.º 17
0
def computationtime_adaptive_evaluation(input_generator, branches):
    identification = [nlsp.adaptive_identification_powerseries]
    nlfunctions = [
        nlsp.function_factory.power_series,
        nlsp.function_factory.chebyshev1_polynomial,
        nlsp.function_factory.hermite_polynomial,
        nlsp.function_factory.legrendre_polynomial
    ]
    for identification_alg, nl_function in itertools.product(
            identification, nlfunctions):
        sim = []
        iden = []
        print identification_alg
        print nl_function
        for i in range(5):
            input_signal = input_generator.GetOutput()
            filter_spec_tofind = nlsp.log_bpfilter(branches=branches,
                                                   input=input_signal)
            nl_func = nlsp.nl_branches(nlsp.function_factory.power_series,
                                       branches)
            ref_nlsystem = nlsp.HammersteinGroupModel_up(
                input_signal=input_signal,
                nonlinear_functions=nl_func,
                filter_irs=filter_spec_tofind,
                max_harmonics=range(1, branches + 1))
            simulation_time_start = time.clock()
            identification_time_start = time.clock()
            found_filter_spec, nl_functions = identification_alg(
                input_generator, ref_nlsystem.GetOutput(), branches,
                nl_function)
            identification_time_stop = time.clock()
            iden_nlsystem = nlsp.HammersteinGroupModel_up(
                input_signal=input_signal,
                nonlinear_functions=nl_functions,
                filter_irs=found_filter_spec,
                max_harmonics=range(1, branches + 1))
            iden_nlsystem.GetOutput()
            simulation_time_stop = time.clock()
            simulation_time = simulation_time_stop - simulation_time_start
            identification_time = identification_time_stop - identification_time_start
            sim.append(simulation_time)
            iden.append(identification_time)
        print "simulation time: %r, identification time: %r" % (
            numpy.average(sim), numpy.average(iden))
def differentbranches_evaluation(input_generator,
                                 branches,
                                 iden_method,
                                 Plot,
                                 reference=None):
    ref_branches = 3
    for branches in range(1, branches):
        input_signal = input_generator.GetOutput()

        filter_spec_tofind = nlsp.log_weightingfilter(branches=ref_branches,
                                                      input=input_signal)
        ref_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_signal,
            nonlinear_functions=nlsp.nl_branches(
                nlsp.function_factory.power_series, ref_branches),
            filter_irs=filter_spec_tofind,
            max_harmonics=range(1, ref_branches + 1))

        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))
        if reference is not None:
            reference = nlsp.change_length_signal(reference,
                                                  length=len(input_signal))
            ref_nlsystem.SetInput(reference)
            iden_nlsystem.SetInput(reference)
        if Plot is True:
            plot.relabelandplotphase(
                sumpf.modules.FourierTransform(
                    ref_nlsystem.GetOutput()).GetSpectrum(),
                "Reference Output", False)
            plot.relabelandplotphase(
                sumpf.modules.FourierTransform(
                    iden_nlsystem.GetOutput()).GetSpectrum(),
                "Identified Output", True)
        print "SNR between Reference and Identified output : %r, with number of ref_branches: %r and iden_branches: %r" % (
            nlsp.snr(ref_nlsystem.GetOutput(),
                     iden_nlsystem.GetOutput()), ref_branches, branches)
def nlsystem(branches, excitation, system_identification_alg):
    ref_hgm = nlsp.construct_hgm(kernelfile=nl_system)
    ref_hgm.SetInput(excitation.GetOutput())
    ref_output = ref_hgm.GetOutput()
    kernel, function = system_identification_alg(excitation, ref_output,
                                                 branches)
    iden_hgm = nlsp.HammersteinGroupModel_up(nonlinear_functions=function,
                                             filter_irs=kernel,
                                             max_harmonics=range(
                                                 1, branches + 1))
    return iden_hgm, ref_hgm
def uniqueness_evaluation_allexceptadaptive():
    for method, input_generator, label in zip(iden_method, excitation, labels):
        print method, input_generator
        for nlfunc in nl_functions_all:
            print nlfunc
            input_signal = input_generator.GetOutput()
            filter_spec_tofind = nlsp.log_bpfilter(branches=branches,
                                                   input=input_signal)
            ref_nlsystem = nlsp.HammersteinGroupModel_up(
                input_signal=input_signal,
                nonlinear_functions=nlsp.nl_branches(nlfunc, branches),
                filter_irs=filter_spec_tofind,
                max_harmonics=range(1, branches + 1))
            found_filter_spec, nl_functions = 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))
            nlsp.filterkernel_evaluation_sum(filter_spec_tofind,
                                             found_filter_spec)
            nlsp.filterkernel_evaluation_plot(filter_spec_tofind,
                                              found_filter_spec)
            if Plot is True:
                plot.relabelandplot(sumpf.modules.FourierTransform(
                    ref_nlsystem.GetOutput()).GetSpectrum(),
                                    "Reference Output",
                                    show=False)
                plot.relabelandplot(sumpf.modules.FourierTransform(
                    iden_nlsystem.GetOutput()).GetSpectrum(),
                                    "Identified Output",
                                    show=True)
            print "SNR between Reference and Identified output without overlapping filters: %r" % nlsp.snr(
                ref_nlsystem.GetOutput(),
                iden_nlsystem.GetOutput(),
                label=label)
            print
        print
        print
def hgmwithalphafilter_evaluation(input_generator,
                                  branches,
                                  nlfunction,
                                  iden_method,
                                  Plot,
                                  label=None,
                                  reference=None):
    input_signal = input_generator.GetOutput()
    filter_spec_tofind = nlsp.log_weightingfilter(branches=branches,
                                                  input=input_signal)
    ref_nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input_signal,
        nonlinear_functions=nlsp.nl_branches(nlfunction, branches),
        filter_irs=filter_spec_tofind,
        max_harmonics=range(1, branches + 1))
    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))
    if reference is not None:
        reference = nlsp.change_length_signal(reference,
                                              length=len(input_signal))
        ref_nlsystem.SetInput(reference)
        iden_nlsystem.SetInput(reference)
    if Plot is True:
        plot.relabelandplot(sumpf.modules.FourierTransform(
            ref_nlsystem.GetOutput()).GetSpectrum(),
                            "Reference Output",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem.GetOutput()).GetSpectrum(),
                            "Identified Output",
                            show=True)
    print "SNR between Reference and Identified output with weighted filtering: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
Exemplo n.º 22
0
def adaptive_polynomial_evaluation_virtual():
    for input_generator, nlfunc in itertools.product(excitation, nl_functions):
        print input_generator
        print nlfunc
        input_signal = input_generator.GetOutput()
        filter_spec_tofind = nlsp.log_weightingfilter(branches=branches,
                                                      input=input_signal)
        ref_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_signal,
            nonlinear_functions=nlsp.nl_branches(
                nlsp.function_factory.power_series, branches),
            filter_irs=filter_spec_tofind,
            max_harmonics=range(1, branches + 1))
        found_filter_spec, nl_func = nlsp.adaptive_identification(
            input_generator,
            ref_nlsystem.GetOutput(),
            branches,
            nonlinear_func=nlfunc)
        iden_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_signal,
            nonlinear_functions=nl_func,
            filter_irs=found_filter_spec,
            max_harmonics=range(1, branches + 1))
        ref_nlsystem.SetInput(wgn_pink.GetOutput())
        iden_nlsystem.SetInput(wgn_pink.GetOutput())
        if Plot is True:
            plot.relabelandplot(sumpf.modules.FourierTransform(
                ref_nlsystem.GetOutput()).GetSpectrum(),
                                "Reference Output",
                                show=False)
            plot.relabelandplot(sumpf.modules.FourierTransform(
                iden_nlsystem.GetOutput()).GetSpectrum(),
                                "Identified Output",
                                show=True)
        print "SNR between Reference and Identified output without overlapping filters: %r" % nlsp.snr(
            ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
        print
        print
def nlechocancellation(input):
    iden_nl_system, ref_nl_system = nlsystem(
        branches=branches,
        excitation=nl_system_iden_excitation,
        system_identification_alg=system_identification_alg)
    iden_nl_system.SetInput(input)
    echoic_op = echoic_nonlinear_chamber(input)
    desired_op = echoic_op - iden_nl_system.GetOutput()
    kernel, nlfunction = nlsp.adaptive_identification_legendre(
        iden_nl_system.GetOutput(), desired_op, branches=1, filtertaps=2**10)
    iden_lin_system = nlsp.HammersteinGroupModel_up(
        nonlinear_functions=nlfunction, filter_irs=kernel)
    iden_lin_system.SetInput(iden_nl_system.GetOutput())
    determined_output = iden_lin_system.GetOutput() + iden_nl_system.GetOutput(
    )
    speech = determined_output - desired_op
    return speech
Exemplo n.º 24
0
def nonsymmetric_hardclipping_evaluation(input_generator,
                                         branches,
                                         iden_method,
                                         Plot,
                                         reference=None):
    """
    Evaluation of System Identification method by hard clipping system
    nonlinear system - virtual clipping systems which hard clips the signal amplitute which are not in the threshold range
    plot - the virtual nl system output and the identified nl system output
    expectation - utmost similarity between the two outputs
    """
    t1 = range(8, 11)
    t2 = range(8, 11)
    for th1, th2 in itertools.product(t1, t2):
        th1 = th1 / 10.0
        th2 = th2 / 10.0
        thresholds = [-th1, th2]
        input_signal = input_generator.GetOutput()
        ref_nlsystem = sumpf.modules.ClipSignal(thresholds=thresholds)
        ref_nlsystem.SetInput(input_signal)

        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))
        if reference is not None:
            ref_nlsystem.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)
        print "SNR between Reference and Identified output for non symmetric hardclipping(thresholds:%r): %r" % (
            thresholds,
            nlsp.snr(ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput()))
def sweep_aliasing_evaluation():
    input_signal = input_generator.GetOutput()
    lin = sumpf.modules.ImpulseGenerator(
        samplingrate=input_signal.GetSamplingRate(), length=100).GetSignal()
    nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input_signal,
        filter_irs=[
            lin,
        ] * degree,
        nonlinear_functions=nlsp.nl_branches(
            nlsp.function_factory.power_series, degree),
        max_harmonics=range(1, degree + 1))
    harmonics_ir = nlsp.get_nl_impulse_response(input_generator,
                                                nlsystem.GetOutput())
    harmonics = nlsp.get_nl_harmonics(input_generator, nlsystem.GetOutput(),
                                      degree)
    print "Harmonics to total harmonics ratio %r" % nlsp.harmonicsvsall_energyratio_nl(
        input_generator, nlsystem.GetOutput(), degree)
    plot.plot(harmonics_ir)
    plot.plot(harmonics)
Exemplo n.º 26
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)
Exemplo n.º 27
0
def softclipping_evaluation(input_generator,
                            branches,
                            iden_method,
                            Plot,
                            reference=None):
    """
    Evaluation of System Identification method by soft clipping system
    nonlinear system - virtual clipping systems which soft clips the signal amplitute which are not in the threshold range
    plot - the virtual nl system output and the identified nl system output
    expectation - utmost similarity between the two outputs
    """
    thresholds = [-1.0, 1.0]
    power = 1.0 / 3.0
    input_signal = input_generator.GetOutput()
    ref_nlsystem = nlsp.NLClipSignal(thresholds=thresholds, power=power)
    ref_nlsystem.SetInput(input_signal)

    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))
    if reference is not None:
        ref_nlsystem.SetInput(reference)
        iden_nlsystem.SetInput(reference)

    if Plot is True:
        plot.relabelandplotphase(sumpf.modules.FourierTransform(
            ref_nlsystem.GetOutput()).GetSpectrum(),
                                 "Reference System",
                                 show=False)
        plot.relabelandplotphase(sumpf.modules.FourierTransform(
            iden_nlsystem.GetOutput()).GetSpectrum(),
                                 "Identified System",
                                 show=True)
    print "SNR between Reference and Identified output for soft clipping: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
Exemplo n.º 28
0
def construct_hgm(kernelfile):
    path, file = os.path.split(kernelfile)
    filter_impulse_response = sumpf.modules.SignalFile(
        filename=kernelfile,
        format=sumpf.modules.SignalFile.NUMPY_NPZ).GetSignal()
    filter_ir = nlsp.multichanneltoarray(filter_impulse_response)
    branches = len(filter_ir)
    if "powerseries" in file:
        nl_functions = nlsp.nl_branches(nlsp.function_factory.power_series,
                                        branches)
    elif "chebyshev" in file:
        nl_functions = nlsp.nl_branches(
            nlsp.function_factory.chebyshev1_polynomial, branches)
    elif "legendre" in file:
        nl_functions = nlsp.nl_branches(
            nlsp.function_factory.legrendre_polynomial, branches)
    elif "hermite" in file:
        nl_functions = nlsp.nl_branches(
            nlsp.function_factory.hermite_polynomial, branches)
    hgm = nlsp.HammersteinGroupModel_up(nonlinear_functions=nl_functions,
                                        filter_irs=filter_ir)
    return hgm
Exemplo n.º 29
0
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.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_freq = nlsp.find_frequencies(hammerstein_group.GetOutput())
    assert Test_groupmodel_freq == Test_model_freq
Exemplo n.º 30
0
def wgnasreference():

    branches = 3
    sampling_rate = 48000.0
    length = 2**18
    start_freq = 100.0
    stop_freq = 20000.0
    fade_out = 0.00
    fade_in = 0.00
    filter_taps = 2**11

    # 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=
        "C:/Users/diplomand.8/Desktop/nl_recordings/rec_4_ls/Noise18.npz",
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    load_sine = sumpf.modules.SignalFile(
        filename="C:/Users/diplomand.8/Desktop/nl_recordings/rec_4_ls/sine.npz",
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    load_cosine = sumpf.modules.SignalFile(
        filename="C:/Users/diplomand.8/Desktop/nl_recordings/rec_4_ls/cos.npz",
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    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()

    # initialize hgm
    iden_nlsystem = nlsp.HammersteinGroupModel_up(
        max_harmonics=range(1, branches + 1),
        nonlinear_functions=nlsp.nl_branches(
            nlsp.function_factory.power_series, branches),
        filter_irs=[
            impulse,
        ] * branches)

    # only sine based system identification
    found_filter_spec_sine, nl_function_sine = nlsp.nonlinearconvolution_powerseries_temporalreversal(
        sine_g, output_sine, branches)
    iden_nlsystem.SetInput(signal=input_noise)
    iden_nlsystem.SetNLFunctions(nl_function_sine)
    iden_nlsystem.SetFilterIRS(found_filter_spec_sine)
    plot.relabelandplotphase(sumpf.modules.FourierTransform(
        iden_nlsystem.GetOutput()).GetSpectrum(),
                             "Identified power noise",
                             show=False)
    print "SNR between Reference and Identified output Sine: %r" % nlsp.snr(
        output_noise, iden_nlsystem.GetOutput())

    # only cosine based system identification
    found_filter_spec_cos, nl_function_cos = nlsp.nonlinearconvolution_chebyshev_temporalreversal(
        cos_g, output_cos, branches)
    iden_nlsystem.SetInput(signal=input_noise)
    iden_nlsystem.SetNLFunctions(nl_function_cos)
    iden_nlsystem.SetFilterIRS(found_filter_spec_cos)
    plot.relabelandplotphase(sumpf.modules.FourierTransform(
        iden_nlsystem.GetOutput()).GetSpectrum(),
                             "Identified cheby noise",
                             show=False)
    print "SNR between Reference and Identified output Cos: %r" % nlsp.snr(
        output_noise, iden_nlsystem.GetOutput())

    # only noise based system identification
    found_filter_spec_adapt, nl_function_adapt = nlsp.adaptive_identification(
        input_generator=input_noise,
        outputs=output_noise,
        iterations=1,
        branches=branches,
        step_size=0.1,
        filtertaps=filter_taps,
        algorithm=nlsp.multichannel_nlms,
        Plot=False)
    iden_nlsystem.SetInput(signal=input_noise)
    iden_nlsystem.SetNLFunctions(nl_function_adapt)
    iden_nlsystem.SetFilterIRS(found_filter_spec_adapt)
    plot.relabelandplotphase(sumpf.modules.FourierTransform(
        iden_nlsystem.GetOutput()).GetSpectrum(),
                             "Identified Adapt noise",
                             show=False)
    print "SNR between Reference and Identified output Adapt: %r" % nlsp.snr(
        output_noise, iden_nlsystem.GetOutput())

    # 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(
        input_generator=input_noise,
        outputs=output_noise,
        iterations=1,
        branches=branches,
        step_size=0.1,
        filtertaps=filter_taps,
        algorithm=nlsp.multichannel_nlms,
        Plot=False,
        init_coeffs=found_filter_spec_sine_reducedlength)
    iden_nlsystem.SetInput(signal=input_noise)
    iden_nlsystem.SetNLFunctions(nl_function_sineadapt)
    iden_nlsystem.SetFilterIRS(found_filter_spec_sineadapt)
    plot.relabelandplotphase(sumpf.modules.FourierTransform(
        iden_nlsystem.GetOutput()).GetSpectrum(),
                             "Identified power Adapt noise",
                             show=False)
    print "SNR between Reference and Identified output Sine Adapt: %r" % nlsp.snr(
        output_noise, iden_nlsystem.GetOutput())

    # 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(
        input_generator=input_noise,
        outputs=output_noise,
        iterations=1,
        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.SetInput(signal=input_noise)
    iden_nlsystem.SetNLFunctions(nl_function_cosadapt)
    iden_nlsystem.SetFilterIRS(found_filter_spec_cosadapt)
    plot.relabelandplotphase(sumpf.modules.FourierTransform(
        iden_nlsystem.GetOutput()).GetSpectrum(),
                             "Identified chebyshev Adapt noise",
                             show=False)
    print "SNR between Reference and Identified output Cos Adapt: %r" % nlsp.snr(
        output_noise, iden_nlsystem.GetOutput())

    plot.relabelandplotphase(
        sumpf.modules.FourierTransform(output_noise).GetSpectrum(),
        "Reference Noise",
        show=True)