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)
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())
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 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())
def adaptive_identification(
        input_generator,
        outputs,
        branches=5,
        iterations=1,
        step_size=0.1,
        filtertaps=2**11,
        algorithm=nlsp.miso_nlms_multichannel,
        init_coeffs=None,
        Plot_SERvsIteration=False,
        Print_SER=False,
        nonlinear_func=nlsp.function_factory.legrendre_polynomial):
    """
    Adaptive system identification.
    :param input_generator: the input generator object or the input signal
    :param outputs: the response of the system
    :param branches: total number of branches
    :param iterations: total number of iterations
    :param step_size: the step size for adaptive filtering
    :param filtertaps: the total number of filter taps
    :param algorithm: the adaptation algorithm
    :param init_coeffs: initial coefficients
    :param Plot_SERvsIteration: plot ser vs iteration graph
    :param Print_SER: print SER value for each iteration
    :param nonlinear_func: the nonlinear function of the resulting model
    :return: the array of filter kernels and the nonlinear functions
    """
    if hasattr(input_generator, "GetOutput"):
        input = input_generator.GetOutput()
    else:
        input = input_generator
    impulse = sumpf.modules.ImpulseGenerator(
        samplingrate=outputs.GetSamplingRate(), length=len(input)).GetSignal()
    iden_nlsystem = nlsp.HammersteinGroupModel_up(
        input_signal=input,
        nonlinear_functions=nlsp.nl_branches(nonlinear_func, branches),
        filter_irs=[
            impulse,
        ] * branches,
        max_harmonics=range(1, branches + 1))
    input_signal = []
    for i in range(branches):
        input_signal.append(
            iden_nlsystem.GetHammersteinBranchNLOutput(i + 1).GetChannels()[0])
    desired_signal = outputs.GetChannels()[0]
    if init_coeffs is None:
        w = numpy.zeros((len(input_signal), filtertaps))
    else:
        w = []
        for k in init_coeffs:
            w.append(numpy.asarray(k.GetChannels()[0]))
    error_energy = numpy.zeros(iterations)
    SNR = numpy.zeros(iterations)
    iteration = numpy.zeros(iterations)
    for i in range(iterations):
        w = algorithm(input_signal,
                      desired_signal,
                      filtertaps,
                      step_size,
                      initCoeffs=w,
                      plot=Plot_SERvsIteration)
        kernel = []
        for k in w:
            iden_filter = sumpf.Signal(channels=(k, ),
                                       samplingrate=outputs.GetSamplingRate(),
                                       labels=("filter", ))
            kernel.append(iden_filter)
        iden_nlsystem.SetFilterIRS(kernel)
        error = sumpf.modules.SubtractSignals(
            signal1=outputs, signal2=iden_nlsystem.GetOutput()).GetOutput()
        SNR[i] = nlsp.snr(outputs, iden_nlsystem.GetOutput())[0]
        error_energy[i] = nlsp.calculateenergy_time(error)[0]
        iteration[i] = (i + 1) * (len(input) - filtertaps + 1)
        if Print_SER is True:
            print "SNR          %r, iteration %r" % (SNR[i], iteration[i])
            print "Error energy %r, iteration %r" % (error_energy[i],
                                                     iteration[i])
            print
    nl_func = nlsp.nl_branches(nonlinear_func, branches)
    return kernel, nl_func
Esempio n. 6
0
def adaptive_polynomial_evaluation_realworld():
    # real world reference system, load input and output noise
    load_wgn_normal = sumpf.modules.SignalFile(
        filename=os.path.join(source_dir, "wgn_normal_16.npz"),
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    load_wgn_uniform = sumpf.modules.SignalFile(
        filename=os.path.join(source_dir, "wgn_uniform_16.npz"),
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    load_wgn_gamma = sumpf.modules.SignalFile(
        filename=os.path.join(source_dir, "wgn_gamma_16.npz"),
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    load_wgn_pink = sumpf.modules.SignalFile(
        filename=os.path.join(source_dir, "wgn_pink_16.npz"),
        format=sumpf.modules.SignalFile.WAV_FLOAT)
    input_wgn_normal = sumpf.modules.SplitSignal(
        data=load_wgn_normal.GetSignal(), channels=[0]).GetOutput()
    output_wgn_normal = sumpf.modules.SplitSignal(
        data=load_wgn_normal.GetSignal(), channels=[1]).GetOutput()
    input_wgn_uniform = sumpf.modules.SplitSignal(
        data=load_wgn_uniform.GetSignal(), channels=[0]).GetOutput()
    output_wgn_uniform = sumpf.modules.SplitSignal(
        data=load_wgn_uniform.GetSignal(), channels=[1]).GetOutput()
    input_wgn_gamma = sumpf.modules.SplitSignal(
        data=load_wgn_gamma.GetSignal(), channels=[0]).GetOutput()
    output_wgn_gamma = sumpf.modules.SplitSignal(
        data=load_wgn_gamma.GetSignal(), channels=[1]).GetOutput()
    input_wgn_pink = sumpf.modules.SplitSignal(data=load_wgn_pink.GetSignal(),
                                               channels=[0]).GetOutput()
    output_wgn_pink = sumpf.modules.SplitSignal(data=load_wgn_pink.GetSignal(),
                                                channels=[1]).GetOutput()

    for nlfunc in nl_functions:
        print nlfunc
        found_filter_spec_normal, nl_func_normal = nlsp.adaptive_identification(
            input_wgn_normal,
            output_wgn_normal,
            branches,
            nonlinear_func=nlfunc)
        found_filter_spec_uniform, nl_func_uniform = nlsp.adaptive_identification(
            input_wgn_uniform,
            output_wgn_uniform,
            branches,
            nonlinear_func=nlfunc)
        found_filter_spec_gamma, nl_func_gamma = nlsp.adaptive_identification(
            input_wgn_gamma, output_wgn_gamma, branches, nonlinear_func=nlfunc)
        iden_nlsystem_normal = nlsp.HammersteinGroupModel_up(
            input_signal=input_wgn_normal,
            nonlinear_functions=nl_func_normal,
            filter_irs=found_filter_spec_normal,
            max_harmonics=range(1, branches + 1))
        iden_nlsystem_uniform = nlsp.HammersteinGroupModel_up(
            input_signal=input_wgn_uniform,
            nonlinear_functions=nl_func_uniform,
            filter_irs=found_filter_spec_uniform,
            max_harmonics=range(1, branches + 1))
        iden_nlsystem_gamma = nlsp.HammersteinGroupModel_up(
            input_signal=input_wgn_gamma,
            nonlinear_functions=nl_func_gamma,
            filter_irs=found_filter_spec_gamma,
            max_harmonics=range(1, branches + 1))
        iden_nlsystem_normal.SetInput(input_wgn_pink)
        iden_nlsystem_gamma.SetInput(input_wgn_pink)
        iden_nlsystem_uniform.SetInput(input_wgn_pink)
        if Plot is True:
            plot.relabelandplot(
                sumpf.modules.FourierTransform(output_wgn_pink).GetSpectrum(),
                "Reference Output",
                show=False)
            plot.relabelandplot(sumpf.modules.FourierTransform(
                iden_nlsystem_normal.GetOutput()).GetSpectrum(),
                                "Identified Output",
                                show=True)
        print "SNR between Reference and Identified output without overlapping filters Normal: %r" % nlsp.snr(
            output_wgn_pink, iden_nlsystem_normal.GetOutput())
        if Plot is True:
            plot.relabelandplot(
                sumpf.modules.FourierTransform(output_wgn_pink).GetSpectrum(),
                "Reference Output",
                show=False)
            plot.relabelandplot(sumpf.modules.FourierTransform(
                iden_nlsystem_uniform.GetOutput()).GetSpectrum(),
                                "Identified Output",
                                show=True)
        print "SNR between Reference and Identified output without overlapping filters Uniform: %r" % nlsp.snr(
            output_wgn_pink, iden_nlsystem_uniform.GetOutput())
        if Plot is True:
            plot.relabelandplot(
                sumpf.modules.FourierTransform(output_wgn_pink).GetSpectrum(),
                "Reference Output",
                show=False)
            plot.relabelandplot(sumpf.modules.FourierTransform(
                iden_nlsystem_gamma.GetOutput()).GetSpectrum(),
                                "Identified Output",
                                show=True)
        print "SNR between Reference and Identified output without overlapping filters Gamma: %r" % nlsp.snr(
            output_wgn_pink, iden_nlsystem_gamma.GetOutput())
Esempio n. 7
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
    iden_hgm = nlsp.HammersteinGroupModel_up(nonlinear_functions=function,
                                             filter_irs=kernel,
                                             max_harmonics=range(
                                                 1, branches + 1))
    return iden_hgm, ref_hgm


def nlechocancellation(input):
    iden_nl_system, ref_nl_system = nlsystem(
        branches=branches,
        excitation=nl_system_iden_excitation,
        system_identification_alg=system_identification_alg)
    iden_nl_system.SetInput(input)
    echoic_op = echoic_nonlinear_chamber(input)
    desired_op = echoic_op - iden_nl_system.GetOutput()
    kernel, nlfunction = nlsp.adaptive_identification_legendre(
        iden_nl_system.GetOutput(), desired_op, branches=1, filtertaps=2**10)
    iden_lin_system = nlsp.HammersteinGroupModel_up(
        nonlinear_functions=nlfunction, filter_irs=kernel)
    iden_lin_system.SetInput(iden_nl_system.GetOutput())
    determined_output = iden_lin_system.GetOutput() + iden_nl_system.GetOutput(
    )
    speech = determined_output - desired_op
    return speech


iden_speech = nlechocancellation(input=wgn_normal.GetOutput())
nlsp.common.plots.plot(iden_speech, show=False)
nlsp.common.plots.plot(speech, show=True)
print nlsp.snr(iden_speech, speech)