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 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.º 3
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.º 4
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 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()))
Exemplo n.º 6
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 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 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 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.º 11
0
def doublehgm_same_evaluation(input_generator,
                              branches,
                              iden_method,
                              Plot,
                              reference=None):
    """
    Evaluation of System Identification method by double hgm virtual nl system with same nonlinear degree and filters
    nonlinear system - two virtual hammerstein group model with power series polynomials as nl function and 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
    """
    input_signal = input_generator.GetOutput()
    filter_spec_tofind = nlsp.log_bpfilter(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_tofind, filter_spec_tofind),
        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_tofind, filter_spec_tofind),
            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 all same: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(2), iden_nlsystem.GetOutput())
Exemplo n.º 12
0
def doublehgm_different_evaluation(input_generator,
                                   branches,
                                   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)

    sweep = nlsp.NovakSweepGenerator_Sine(
        sampling_rate=input_signal.GetSamplingRate(), length=len(input_signal))
    ref_nlsystem = nlsp.HammersteinGroup_Series(
        input_signal=sweep.GetOutput(),
        nonlinear_functions=(nlsp.nl_branches(
            nlsp.function_factory.power_series, branches),
                             nlsp.nl_branches(
                                 nlsp.function_factory.chebyshev1_polynomial,
                                 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))
    init_coeffs, non = nlsp.nonlinearconvolution_powerseries_temporalreversal(
        sweep, ref_nlsystem.GetOutput(2), branches=branches)
    init_coeffs = nlsp.change_length_filterkernels(init_coeffs, filter_length)
    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.chebyshev1_polynomial,
                                 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 = nlsp.adaptive_identification_legendre(
        input_generator=input_generator,
        outputs=ref_nlsystem.GetOutput(2),
        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 = nlsp.HammersteinGroup_Series(
            input_signal=reference,
            nonlinear_functions=(
                nlsp.nl_branches(nlsp.function_factory.power_series, branches),
                nlsp.nl_branches(nlsp.function_factory.chebyshev1_polynomial,
                                 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 different: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(2), iden_nlsystem.GetOutput())
Exemplo n.º 13
0
def hgmwithfilter_evaluation_sweepadaptive(input_generator,
                                           branches,
                                           nlfuntion,
                                           Plot,
                                           reference=None):
    input_signal = input_generator.GetOutput()
    # filter_spec_tofind = nlsp.create_bpfilter([2000,8000,30000],input_signal)
    filter_spec_tofind = nlsp.log_bpfilter(branches=branches,
                                           input=input_signal)
    # filter_spec_tofind = nlsp.log_chebyfilter(branches=branches,input=input_signal)
    ref_nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input_signal,
        nonlinear_functions=nlsp.nl_branches(nlfuntion, 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())
    sweep_start = time.clock()
    init_coeffs, non = nlsp.nonlinearconvolution_powerseries_temporalreversal(
        sweep, ref_nlsystem.GetOutput(), branches=branches)
    sweep_stop = time.clock()
    init_coeffs = nlsp.change_length_filterkernels(init_coeffs, filter_length)
    ref_nlsystem.SetInput(input_signal)
    adapt_sweep_start = time.clock()
    found_filter_spec, nl_functions = nlsp.adaptive_identification_legendre(
        input_generator=input_generator,
        outputs=ref_nlsystem.GetOutput(),
        branches=branches,
        init_coeffs=init_coeffs,
        filtertaps=filter_length)
    adapt_sweep_stop = time.clock()
    adapt_start = time.clock()
    found_filter_spec, nl_functions = nlsp.adaptive_identification_legendre(
        input_generator=input_generator,
        outputs=ref_nlsystem.GetOutput(),
        branches=branches,
        filtertaps=filter_length)
    adapt_stop = time.clock()
    print "sweep_identification time %r" % (sweep_start - sweep_stop)
    print "sweep_adapt_identification time %r" % (adapt_sweep_start -
                                                  adapt_sweep_stop)
    print "adapt_identification time %r" % (adapt_start - adapt_stop)
    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_plot(filter_spec_tofind,found_filter_spec)
    # nlsp.filterkernel_evaluation_sum(filter_spec_tofind,found_filter_spec)
    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 without overlapping filters: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
Exemplo n.º 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 robustness_noise_evaluation(input_generator,
                                branches,
                                iden_method,
                                Plot,
                                reference=None):
    """
    Evaluation of System Identification method robustness by adding noise
    nonlinear system - virtual hammerstein group model with power series polynomials as nl function and 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
    """
    noise_mean = [0, 0.5]
    noise_sd = [0.5, 0.7]
    for mean, sd in itertools.product(noise_mean, noise_sd):
        input_signal = input_generator.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))
        ref_nlsystem_noise = nlsp.add_noise(
            ref_nlsystem.GetOutput(),
            sumpf.modules.NoiseGenerator.GaussianDistribution(mean, sd))
        found_filter_spec, nl_functions = iden_method(input_generator,
                                                      ref_nlsystem.GetOutput(),
                                                      branches)
        noise_filter_spec, nl_functions = iden_method(input_generator,
                                                      ref_nlsystem_noise,
                                                      branches)
        found_filter_spec = nlsp.change_length_filterkernels(
            found_filter_spec, length=filter_length)
        noise_filter_spec = nlsp.change_length_filterkernels(
            noise_filter_spec, length=filter_length)

        iden_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_signal,
            nonlinear_functions=nl_functions,
            filter_irs=found_filter_spec,
            max_harmonics=range(1, branches + 1))
        noise_iden_nlsystem = nlsp.HammersteinGroupModel_up(
            input_signal=input_signal,
            nonlinear_functions=nl_functions,
            filter_irs=noise_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)
            noise_iden_nlsystem.SetInput(reference)
        if Plot is True:
            nlsp.relabelandplotphase(
                sumpf.modules.FourierTransform(
                    ref_nlsystem.GetOutput()).GetSpectrum(),
                "Reference Output", False)
            nlsp.relabelandplotphase(
                sumpf.modules.FourierTransform(
                    iden_nlsystem.GetOutput()).GetSpectrum(),
                "Identified Output", False)
            nlsp.relabelandplotphase(
                sumpf.modules.FourierTransform(
                    noise_iden_nlsystem.GetOutput()).GetSpectrum(),
                "Noise Identified Output", True)
        print "SNR between Reference and Identified output without noise: %r" % nlsp.snr(
            ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
        print "SNR between Reference and Identified output with noise of sd %r and mean %r is %r" % (
            sd, mean,
            nlsp.snr(ref_nlsystem.GetOutput(),
                     noise_iden_nlsystem.GetOutput()))
def hgmwithfilter_evaluation(input_generator,
                             branches,
                             nlfuntion,
                             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
    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()
    # filter_spec_tofind = nlsp.create_bpfilter([2000,8000,30000],input_signal)
    filter_spec_tofind = nlsp.log_bpfilter(branches=branches,
                                           input=input_signal)
    # filter_spec_tofind = [i for i in reversed(filter_spec_tofind)]
    length_kernel = len(filter_spec_tofind[0])
    # filter_spec_tofind = nlsp.log_chebyfilter(branches=branches,input=input_signal)
    ref_nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input_signal,
        nonlinear_functions=nlsp.nl_branches(nlfuntion, 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))
    # nlsp.filterkernel_evaluation_plot(filter_spec_tofind,found_filter_spec)
    # nlsp.filterkernel_evaluation_sum(filter_spec_tofind,found_filter_spec)
    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(ref_nlsystem.GetOutput(),
                            "Reference Output",
                            show=False)
        plot.relabelandplot(iden_nlsystem.GetOutput(),
                            "Identified Output",
                            show=True)
    # nlsp.plot_array([sumpf.modules.FourierTransform(s).GetSpectrum() for s in filter_spec_tofind],label_array=["reference%d" %i for i in range(len(filter_spec_tofind))],Show=False)
    # nlsp.plot_array([sumpf.modules.FourierTransform(s).GetSpectrum() for s in found_filter_spec],label_array=["identified%d" %i for i in range(len(found_filter_spec))],Show=True)
    print "SNR between Reference and Identified output without overlapping filters: %r" % nlsp.snr(
        ref_nlsystem.GetOutput(), iden_nlsystem.GetOutput())
    sumpf.modules.SignalFile(
        filename=
        "C:/Users/diplomand.8/Desktop/linearHGM_explannation/cheby/noise/input",
        signal=reference,
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    sumpf.modules.SignalFile(
        filename=
        "C:/Users/diplomand.8/Desktop/linearHGM_explannation/cheby/noise/%s" %
        iden_method.__name__,
        signal=iden_nlsystem.GetOutput(),
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    sumpf.modules.SignalFile(
        filename=
        "C:/Users/diplomand.8/Desktop/linearHGM_explannation/cheby/noise/reference",
        signal=ref_nlsystem.GetOutput(),
        format=sumpf.modules.SignalFile.WAV_FLOAT)
Exemplo n.º 17
0
def loudspeaker_evaluation_all(branches=3, Plot=True):

    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, "Music1.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, nlfunc = nlsp.linear_identification(sine_g, output_sine,
                                                       branches)
    iden_nlsystem_linear = nlsp.HammersteinGroupModel_up(
        filter_irs=kernel_linear, nonlinear_functions=nlfunc)

    # linear identification
    kernel_linear_hgm, nlfunc_hgm = nlsp.linear_identification_powerhgm(
        sine_g, output_sine, branches)
    iden_nlsystem_linear_hgm = nlsp.HammersteinGroupModel_up(
        filter_irs=kernel_linear_hgm, nonlinear_functions=nlfunc_hgm)

    # 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_legendre(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_legendre(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_linear_hgm.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)
    plot.relabelandplot(
        sumpf.modules.FourierTransform(output_sine).GetSpectrum(),
        "Reference system",
        show=False)
    plot.relabelandplot(sumpf.modules.FourierTransform(
        iden_nlsystem_sine.GetOutput()).GetSpectrum(),
                        "Sweep based system identification",
                        show=False)
    plot.relabelandplot(sumpf.modules.FourierTransform(
        iden_nlsystem_linear.GetOutput()).GetSpectrum(),
                        "Linear identified system",
                        show=True)

    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 system",
            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_linear.GetOutput()).GetSpectrum(),
                            "Linear identified system",
                            show=False)
        plot.relabelandplot(sumpf.modules.FourierTransform(
            iden_nlsystem_sineadapt.GetOutput()).GetSpectrum(),
                            "sweep-adaptive output",
                            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 linear hgm: %r" % nlsp.snr(
        output_sine, iden_nlsystem_linear_hgm.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_linear_hgm.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 linear hgm: %r" % nlsp.snr(
        output_sample, iden_nlsystem_linear_hgm.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())
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())