Beispiel #1
0
def load_systemidentification(method, nlsystem, branches, length):
    str_method = str(method)
    f, func, rest, rest2 = str_method.split()
    filename = '_'.join((nlsystem, func, str(branches), str(length)))
    filename = ''.join((filename, ".npz"))
    file = os.path.join(location, filename)
    if os.path.isfile(file):
        filter_impulse_response = sumpf.modules.SignalFile(
            filename=file,
            format=sumpf.modules.SignalFile.NUMPY_NPZ).GetSignal()
        filter_ir = nlsp.multichanneltoarray(filter_impulse_response)
        if "power" in func:
            nl_functions = nlsp.nl_branches(nlsp.function_factory.power_series,
                                            branches)
        elif "cheby" in func:
            nl_functions = nlsp.nl_branches(
                nlsp.function_factory.chebyshev1_polynomial, branches)
        elif "legendre" in func:
            nl_functions = nlsp.nl_branches(
                nlsp.function_factory.legrendre_polynomial, branches)
        elif "hermite" in func:
            nl_functions = nlsp.nl_branches(
                nlsp.function_factory.hermite_polynomial, branches)
    else:
        filter_ir = None
        nl_functions = None
    return filter_ir, nl_functions
Beispiel #2
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())
Beispiel #3
0
def linear_identification_temporalreversal(sweep_generator,
                                           output_sweep,
                                           branches,
                                           length_ir=2**12):
    """
    Linear identification of a system using temporal reversal method.
    :param sweep_generator: the sweep generator object
    :param output_sweep: the output sweep of the nonlinear system
    :return: the filter kernels and the nonlinear functions of the HGM
    """
    srate = output_sweep.GetSamplingRate()
    rev = sweep_generator.GetReversedOutput()
    rev_spec = sumpf.modules.FourierTransform(rev).GetSpectrum()
    out_spec = sumpf.modules.FourierTransform(output_sweep).GetSpectrum()
    out_spec = out_spec / output_sweep.GetSamplingRate()
    tf = rev_spec * out_spec
    ir_sweep = sumpf.modules.InverseFourierTransform(tf).GetSignal()
    ir_sweep = sumpf.modules.CutSignal(signal=ir_sweep,
                                       start=0,
                                       stop=length_ir).GetOutput()
    ir_sweep = [
        ir_sweep,
    ]
    nl_func = nlsp.nl_branches(nlsp.function_factory.power_series, branches)
    for i in range(1, branches):
        ir_sweep.append(
            sumpf.modules.ConstantSignalGenerator(
                value=0.0, samplingrate=srate, length=length_ir).GetSignal())
    return ir_sweep, nl_func
Beispiel #4
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
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)
Beispiel #6
0
def miso_identification(input_generator, output_wgn, branches):
    """
    MISO approach of system identification.
    :param input_generator: the input generator or the input signal
    :param output_wgn: the response of the nonlinear system
    :param branches: the number of branches
    :return: the filter kernels and the nonlinear function of a HGM
    """
    if hasattr(input_generator,"GetOutput"):
        input_wgn = input_generator.GetOutput()
    else:
        input_wgn = input_generator
    l = []
    L = []
    signal_matrix, k_matrix, mu_matrix = wgn_hgm_decorrelate(input_wgn,branches)
    for branch in range(1,branches+1):
        input_decorrelated = signal_matrix[branch-1]
        cross_corr = sumpf.modules.CorrelateSignals(signal1=input_decorrelated,signal2=output_wgn,mode=sumpf.modules.CorrelateSignals.SPECTRUM).GetOutput()
        num = sumpf.modules.FourierTransform(cross_corr).GetSpectrum()
        den = sumpf.modules.FourierTransform(sumpf.modules.CorrelateSignals(signal1=input_decorrelated,
                                                                            signal2=input_decorrelated,mode=sumpf.modules.CorrelateSignals.SPECTRUM).GetOutput()).GetSpectrum()
        linear = sumpf.modules.DivideSpectrums(spectrum1=num, spectrum2=den).GetOutput()
        kernel = sumpf.modules.InverseFourierTransform(linear).GetSignal()
        signal = sumpf.Signal(channels=kernel.GetChannels(),samplingrate=input_wgn.GetSamplingRate(),labels=kernel.GetLabels())
        l.append(signal)
        L.append(sumpf.modules.FourierTransform(signal).GetSpectrum())
    G = []
    for row in range(0,branches):
        A = sumpf.modules.ConstantSpectrumGenerator(value=0.0,resolution=L[0].GetResolution(),length=len(L[0])).GetSpectrum()
        for column in range(0,branches):
            temp = sumpf.modules.AmplifySpectrum(input=L[column],factor=k_matrix[row][column]).GetOutput()
            A = A + temp
        G.append(sumpf.modules.InverseFourierTransform(A + mu_matrix[row]).GetSignal())
    nl_func = nlsp.nl_branches(nlsp.function_factory.power_series,branches)
    return G,nl_func
Beispiel #7
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()))
Beispiel #8
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()))
Beispiel #11
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))
Beispiel #12
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())
Beispiel #13
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
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 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
Beispiel #18
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 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)
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())
Beispiel #23
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 wiener_g_identification(input_gen, output, branches):
    """
    System identification using Wiener G functionals.
    :param input_gen: the input generator or the input signal
    :param output: the response of the nonlinear system
    :param branches: the total number of branches
    :return: the filter kernels and the nonlinear functions
    """
    kernel_length = len(output) / 2
    if hasattr(input_gen, "GetOutput"):
        excitation = input_gen.GetOutput()
    else:
        excitation = input_gen
    response = output
    variance = sumpf.modules.SignalMean(input_gen.GetOutput() *
                                        input_gen.GetOutput()).GetMean()[0]
    kernels = []
    for branch in range(1, branches + 1):
        k = []
        for i in range(0, kernel_length):
            shifted = sumpf.modules.ShiftSignal(signal=excitation,
                                                shift=-i,
                                                circular=False).GetOutput()
            power = nlsp.NonlinearFunction(
                signal=shifted,
                nonlin_func=nlsp.function_factory.hermite_polynomial(branch))
            product = response * power.GetOutput()
            mean = sumpf.modules.SignalMean(signal=product).GetMean()[0]
            factor = 1.0 / (math.factorial(branch) * (variance**branch))
            k.append(mean * factor)
        k = sumpf.Signal(channels=(k, ),
                         samplingrate=output.GetSamplingRate(),
                         labels=())
        kernels.append(k)
    nl_func = nlsp.nl_branches(nlsp.function_factory.power_series, branches)
    return kernels, nl_func
Beispiel #25
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())
Beispiel #26
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())
Beispiel #27
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())
Beispiel #28
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)
Beispiel #29
0
def loudspeaker_model_adaptive():
    """
    Realworld nonlinear system(Loudspeaker) Model using adaptive system identification
    """
    branches = 5
    filter_taps = 2**11

    # real world reference system, load input and output noise
    load_noise = sumpf.modules.SignalFile(
        filename=
        "C:/Users/diplomand.8/Desktop/nl_recordings/rec_4_db/Noise18.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()
    impulse = sumpf.modules.ImpulseGenerator(
        length=filter_taps,
        samplingrate=input_noise.GetSamplingRate()).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 noise based adaptive 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(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 sine",
                             show=False)
    print "SNR between Reference and Identified output Noise signal: %r" % nlsp.snr(
        output_noise, iden_nlsystem.GetOutput())
    plot.relabelandplotphase(
        sumpf.modules.FourierTransform(output_noise).GetSpectrum(),
        "Reference Noise",
        show=True)

    load_sample = sumpf.modules.SignalFile(
        filename=
        "C:/Users/diplomand.8/Desktop/nl_recordings/rec_4_db/Speech1.npz",
        format=sumpf.modules.SignalFile.WAV_FLOAT)

    output_sample = sumpf.modules.SplitSignal(data=load_sample.GetSignal(),
                                              channels=[1]).GetOutput()
    input_sample = sumpf.modules.SplitSignal(data=load_sample.GetSignal(),
                                             channels=[0]).GetOutput()

    iden_nlsystem.SetInput(input_sample)

    # save the output to the directory
    iden = sumpf.modules.SignalFile(
        filename=
        "C:/Users/diplomand.8/Desktop/nl_recordings/rec_4_db/sim/identified",
        signal=iden_nlsystem.GetOutput(),
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    ref = sumpf.modules.SignalFile(
        filename=
        "C:/Users/diplomand.8/Desktop/nl_recordings/rec_4_db/sim/reference",
        signal=output_sample,
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    inp = sumpf.modules.SignalFile(
        filename=
        "C:/Users/diplomand.8/Desktop/nl_recordings/rec_4_db/sim/input",
        signal=input_sample,
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    print "Distortion box, SNR between Reference and Identified output Sample,nl: %r" % nlsp.snr(
        output_sample, iden_nlsystem.GetOutput())
def sine_sweepbased_spectralinversion(sweep_generator,
                                      output_sweep,
                                      branches=5):
    """
    Sweep-based system identification using spectral inversion technique and using sine sweep signal.
    :param sweep_generator: the sweep generator object
    :param output_sweep: the output sweep of the nonlinear system
    :param branches: the total number of output branches
    :return: the parameters of HGM (filter kernels and nonlinear functions)
    """
    sweep_length = sweep_generator.GetLength()
    sweep_start_freq = sweep_generator.GetStartFrequency()
    sweep_stop_freq = sweep_generator.GetStopFrequency()
    input_sweep = sweep_generator.GetOutput()

    if isinstance(input_sweep, (sumpf.Signal)):
        ip_signal = input_sweep
        ip_spectrum = sumpf.modules.FourierTransform(
            signal=input_sweep).GetSpectrum()
    else:
        ip_signal = sumpf.modules.InverseFourierTransform(
            spectrum=input_sweep).GetSignal()
        ip_spectrum = input_sweep
    if isinstance(output_sweep, (sumpf.Signal)):
        op_spectrum = sumpf.modules.FourierTransform(
            signal=output_sweep).GetSpectrum()
    else:
        op_spectrum = output_sweep
    inversed_ip = sumpf.modules.RegularizedSpectrumInversion(
        spectrum=ip_spectrum,
        start_frequency=sweep_start_freq + 50,
        stop_frequency=sweep_stop_freq - 100).GetOutput()
    tf_sweep = sumpf.modules.MultiplySpectrums(
        spectrum1=inversed_ip, spectrum2=op_spectrum).GetOutput()
    ir_sweep = sumpf.modules.InverseFourierTransform(
        spectrum=tf_sweep).GetSignal()
    # nlsp.common.plots.plot(ir_sweep)
    ir_sweep_direct = sumpf.modules.CutSignal(signal=ir_sweep,
                                              start=0,
                                              stop=int(sweep_length /
                                                       4)).GetOutput()
    ir_sweep_direct = nlsp.append_zeros(ir_sweep_direct)
    ir_merger = sumpf.modules.MergeSignals(
        on_length_conflict=sumpf.modules.MergeSignals.FILL_WITH_ZEROS)
    ir_merger.AddInput(ir_sweep_direct)
    for i in range(branches - 1):
        split_harm = nlsp.FindHarmonicImpulseResponse_Novak(
            impulse_response=ir_sweep,
            harmonic_order=i + 2,
            sweep_generator=sweep_generator).GetHarmonicImpulseResponse()
        ir_merger.AddInput(
            sumpf.Signal(channels=split_harm.GetChannels(),
                         samplingrate=ir_sweep.GetSamplingRate(),
                         labels=split_harm.GetLabels()))
    ir_merger = ir_merger.GetOutput()

    tf_harmonics_all = sumpf.modules.FourierTransform(
        signal=ir_merger).GetSpectrum()
    harmonics_tf = []
    for i in range(len(tf_harmonics_all.GetChannels())):
        tf_harmonics = sumpf.modules.SplitSpectrum(data=tf_harmonics_all,
                                                   channels=[i]).GetOutput()
        harmonics_tf.append(tf_harmonics)
    A_matrix = numpy.zeros((branches, branches), dtype=numpy.complex128)
    for n in range(0, branches):
        for m in range(0, branches):
            if ((n >= m) and ((n + m) % 2 == 0)):
                A_matrix[m][n] = (((-1 + 0j)**(2 * (n + 1) - m / 2)) /
                                  (2**n)) * nlsp.binomial((n + 1), (n - m) / 2)
            else:
                A_matrix[m][n] = 0
    A_inverse = numpy.linalg.inv(A_matrix)
    for row in range(0, len(A_inverse)):
        if row % 2 != 0.0:
            A_inverse[row] = A_inverse[row] * (0 + 1j)
    B = []
    for row in range(0, branches):
        A = sumpf.modules.ConstantSpectrumGenerator(
            value=0.0,
            resolution=harmonics_tf[0].GetResolution(),
            length=len(harmonics_tf[0])).GetSpectrum()
        for column in range(0, branches):
            temp = sumpf.modules.AmplifySpectrum(
                input=harmonics_tf[column],
                factor=A_inverse[row][column]).GetOutput()
            A = A + temp
        B_temp = nlsp.relabel(
            sumpf.modules.InverseFourierTransform(A).GetSignal(),
            "%r harmonic identified psi" % str(row + 1))
        B.append(B_temp)
    nl_func = nlsp.nl_branches(nlsp.function_factory.power_series, branches)
    return B, nl_func