def lowpass_evaluation():
    print "lowpass evaluation"
    for order in range(1, degree + 1):
        branch_lp_butter = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=input_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(1),
            max_harm=order,
            filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH(order=10))
        branch_lp_cheby1 = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=input_sweep_signal,
            nonlin_func=nlsp.function_factory.power_series(1),
            max_harm=order,
            filterfunction=sumpf.modules.FilterGenerator.CHEBYCHEV1(
                order=10, ripple=6.0))
        butter_spec = nlsp.relabel(
            sumpf.modules.FourierTransform(
                branch_lp_butter.GetOutput()).GetSpectrum(),
            "butterworth lp branch")
        chebyshev_spec = nlsp.relabel(
            sumpf.modules.FourierTransform(
                branch_lp_cheby1.GetOutput()).GetSpectrum(),
            "chebyshev lp branch")
        input_spec = nlsp.relabel(
            sumpf.modules.FourierTransform(input_sweep_signal).GetSpectrum(),
            "input spec")
        if Plot is True:
            plot.log()
            plot.plot(butter_spec, show=False)
            plot.plot(input_spec, show=False)
            plot.plot(chebyshev_spec, show=True)
def puretone_evaluation():
    """
    Evaluation of alias compensation of hammerstein branch using pure tones.
    Puretone of certain frequency is given to different hammerstein branches with different Aliasing compensation.
    The order of the harmonics produces is changed and the output of different models are plotted.
    We observe the lowpass aliasing compensation completely filters out the signal harmonics even when they are in the
    baseband freq.
    """
    print "puretone evaluation"
    for i in range(1, degree + 1):
        input_tone = nlsp.generate_puretones([10000], sampling_rate, length)
        branch_simple = nlsp.HammersteinModel(
            input_signal=input_tone,
            nonlin_func=nlsp.function_factory.power_series(i))
        branch_up = nlsp.AliasCompensatingHammersteinModelUpandDown(
            input_signal=input_tone,
            nonlin_func=nlsp.function_factory.power_series(i),
            max_harm=i)
        branch_lp = nlsp.AliasCompensatingHammersteinModelLowpass(
            input_signal=input_tone,
            nonlin_func=nlsp.function_factory.power_series(i),
            max_harm=i)
        print "Pure tone evaluation"
        print "Order: %r" % i
        print "SNR of simple h. branch: %r" % nlsp.snr(
            input_tone, branch_simple.GetOutput())
        print "SNR of upsample h. branch: %r" % nlsp.snr(
            input_tone, branch_up.GetOutput())
        print "SNR of lowpass h. branch: %r" % nlsp.snr(
            input_tone, branch_lp.GetOutput())
        print "MSE of simple h. branch: %r" % nlsp.mean_squared_error_time(
            input_tone, branch_simple.GetOutput())
        print "MSE of upsample h. branch: %r" % nlsp.mean_squared_error_time(
            input_tone, branch_up.GetOutput())
        print "MSE of lowpass h. branch: %r" % nlsp.mean_squared_error_time(
            input_tone, branch_lp.GetOutput())
        if Plot is True:
            plot.log()
            branch_simple_spectrum = nlsp.relabel(
                sumpf.modules.FourierTransform(
                    branch_simple.GetOutput()).GetSpectrum(),
                "%d Simple Hammerstein Branch" % i)
            plot.plot(branch_simple_spectrum, show=False)
            branch_up_spectrum = nlsp.relabel(
                sumpf.modules.FourierTransform(
                    branch_up.GetOutput()).GetSpectrum(),
                "%d Upsampling Hammerstein Branch" % i)
            plot.plot(branch_up_spectrum, show=False)
            branch_lp_spectrum = nlsp.relabel(
                sumpf.modules.FourierTransform(
                    branch_lp.GetOutput()).GetSpectrum(),
                "%d Lowpass Hammerstein Branch" % i)
            plot.plot(branch_lp_spectrum, show=True)
def get_nl_harmonics_iden(sweep_generator, response, harmonics):
    sweep_length = sweep_generator.GetLength()
    rev = sweep_generator.GetReversedOutput()
    rev_spec = sumpf.modules.FourierTransform(rev).GetSpectrum()
    out_spec = sumpf.modules.FourierTransform(response).GetSpectrum()
    out_spec = out_spec / response.GetSamplingRate()
    tf = rev_spec * out_spec
    ir_sweep = sumpf.modules.InverseFourierTransform(tf).GetSignal()
    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_sweep_direct = nlsp.relabel(ir_sweep_direct, "Identified Harmonics 1")
    ir_merger = sumpf.modules.MergeSignals(
        on_length_conflict=sumpf.modules.MergeSignals.FILL_WITH_ZEROS)
    ir_merger.AddInput(ir_sweep_direct)
    for i in range(harmonics - 1):
        split_harm = nlsp.FindHarmonicImpulseResponse_Novak(
            impulse_response=ir_sweep,
            harmonic_order=i + 2,
            sweep_generator=sweep_generator).GetHarmonicImpulseResponse()
        split_harm = sumpf.modules.CutSignal(
            signal=split_harm,
            stop=len(sweep_generator.GetOutput())).GetOutput()
        ir_merger.AddInput(
            sumpf.Signal(channels=split_harm.GetChannels(),
                         samplingrate=ir_sweep.GetSamplingRate(),
                         labels=("Identified Harmonics %r" % (i + 2), )))
    tf = sumpf.modules.FourierTransform(ir_merger.GetOutput()).GetSpectrum()
    return tf
def get_sweep_harmonics_ir(sweep_generator, response, max_harm=None):
    """
    Calculate the harmonics of the sweep based on nonconvolution
    :param excitation: the excitation sweep of the system
    :param response: the response of the system
    :param sweep_start_freq: start frequency of the sweep signal
    :param sweep_stop_freq: stop frequency of the sweep signal
    :param max_harm: the maximum harmonics upto which the harmomics should be calculated
    :return: the sumpf signal of merged harmonic spectrums
    """
    sweep_length = sweep_generator.GetLength()
    excitation = sweep_generator.GetOutput()
    sweep_start_freq = sweep_generator.GetStartFrequency()
    sweep_stop_freq = sweep_generator.GetStopFrequency()
    if sweep_length is None:
        sweep_length = len(excitation)
    if max_harm is None:
        max_harm = 5
    impulse_response = get_impulse_response(excitation, response,
                                            sweep_start_freq, sweep_stop_freq)
    linear = sumpf.modules.CutSignal(signal=impulse_response,
                                     start=0,
                                     stop=len(impulse_response) /
                                     4).GetOutput()
    linear = nlsp.relabel(nlsp.append_zeros(linear), "1 hamonic")
    merger = sumpf.modules.MergeSignals(
        on_length_conflict=sumpf.modules.MergeSignals.FILL_WITH_ZEROS)
    merger.AddInput(linear)
    for i in range(2, max_harm + 1):
        harmonics = sumpf.modules.FindHarmonicImpulseResponse(
            impulse_response=impulse_response,
            harmonic_order=i,
            sweep_start_frequency=sweep_start_freq,
            sweep_stop_frequency=sweep_stop_freq,
            sweep_duration=(
                sweep_length /
                excitation.GetSamplingRate())).GetHarmonicImpulseResponse()
        harmonics = nlsp.relabel(harmonics, "%d harmonic" % i)
        merger.AddInput(
            sumpf.Signal(channels=harmonics.GetChannels(),
                         samplingrate=excitation.GetSamplingRate(),
                         labels=harmonics.GetLabels()))
    harmonics_ir = merger.GetOutput()
    return harmonics_ir
Example #5
0
def relabelandplotphase(input, label, show=True, save=False, name=None):
    """
    Relabel the input signal or spectrum and plot
    :param input: the input signal or spectrum
    :param label: the label text
    :param show: True or False
    :return: plots the given input with label
    """
    relabelled = nlsp.relabel(input, label)
    if isinstance(relabelled, sumpf.Spectrum):
        log()
    plot_groupdelayandmagnitude(relabelled, show=show)
Example #6
0
def plot_sdrvsfreq(input_signalorspectrum,
                   output_signalorspectrum,
                   label=None,
                   show=True):
    if isinstance(input_signalorspectrum, list) != True:
        observed_l = []
        observed_l.append(input_signalorspectrum)
    else:
        observed_l = input_signalorspectrum
    if isinstance(output_signalorspectrum, list) != True:
        identified_l = []
        identified_l.append(output_signalorspectrum)
    else:
        identified_l = output_signalorspectrum
    for observed, identified in zip(observed_l, identified_l):
        if isinstance(observed, (sumpf.Signal, sumpf.Spectrum)) and isinstance(
                observed, (sumpf.Signal, sumpf.Spectrum)):
            if isinstance(observed, sumpf.Signal):
                observed = sumpf.modules.FourierTransform(
                    observed).GetSpectrum()
            if isinstance(identified, sumpf.Signal):
                identified = sumpf.modules.FourierTransform(
                    identified).GetSpectrum()
            if len(observed) != len(identified):
                merged_spectrum = sumpf.modules.MergeSpectrums(
                    spectrums=[observed, identified],
                    on_length_conflict=sumpf.modules.MergeSpectrums.
                    FILL_WITH_ZEROS).GetOutput()
                observed = sumpf.modules.SplitSpectrum(
                    data=merged_spectrum, channels=[0]).GetOutput()
                identified = sumpf.modules.SplitSpectrum(
                    data=merged_spectrum, channels=[1]).GetOutput()

            # observed = nlsp.cut_spectrum(observed,[100,19000])
            # identified = nlsp.cut_spectrum(identified,[100,19000])
            identified = sumpf.modules.InverseFourierTransform(
                identified).GetSignal()
            observed = sumpf.modules.InverseFourierTransform(
                observed).GetSignal()
            noise = identified - observed
            print nlsp.calculateenergy_time(noise)
            if label is None:
                pass
            else:
                noise = nlsp.relabel(noise, labels=label)
            nlsp.common.plots.plot(noise, show=show)
        else:
            print "The given arguments is not a sumpf.Signal or sumpf.Spectrum"
Example #7
0
def relabelandplot(input,
                   label=None,
                   show=True,
                   save=False,
                   name=None,
                   line="-"):
    """
    Relabel the input signal or spectrum and plot
    :param input: the input signal or spectrum
    :param label: the label text
    :param show: True or False
    :return: plots the given input with label
    """
    relabelled = nlsp.relabel(input, label)
    if isinstance(relabelled, sumpf.Spectrum):
        log()
    plot(relabelled, show=show, save=save, name=name, line=line)
Example #8
0
def generate_excitation():
    sines = nlsp.relabel(sine.GetOutput(), "sine_sweep")
    coss = nlsp.relabel(cos.GetOutput(), "cos_sweep")
    wgn_normals = nlsp.relabel(wgn_normal.GetOutput(), "white_normal")
    wgn_uniforms = nlsp.relabel(wgn_uniform.GetOutput(), "white_uniform")
    wgn_pinks = nlsp.relabel(wgn_pink.GetOutput(), "white_pink")
    wgn_laplaces = nlsp.relabel(wgn_laplace.GetOutput(), "white_laplace")
    inputs = [sines, coss, wgn_normals, wgn_uniforms, wgn_pinks, wgn_laplaces]
    for input in inputs:
        sumpf.modules.SignalFile(
            filename=
            "C:/Users/diplomand.8/Desktop/evaluation/filter_kernel/mp3/%s" %
            str(input.GetLabels()[0]),
            signal=input,
            format=sumpf.modules.SignalFile.WAV_FLOAT)
        print input
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