def theoretical_evaluation(): puretone_freq = 10000 input_tone = sumpf.modules.SineWaveGenerator(frequency=puretone_freq, phase=0.0, samplingrate=sampling_rate, length=length) sine = input_tone.GetSignal() input_tone.SetFrequency(2 * puretone_freq) sine_2 = input_tone.GetSignal() input_tone.SetFrequency(3 * puretone_freq) sine_3 = input_tone.GetSignal() input_tone.SetPhaseInDegrees(90) cos = input_tone.GetSignal() input_tone.SetFrequency(2 * puretone_freq) cos_2 = input_tone.GetSignal() input_tone.SetFrequency(3 * puretone_freq) cos_3 = input_tone.GetSignal() theoreticl_op_2 = sumpf.modules.ConstantSignalGenerator(value=0.5,samplingrate=sine.GetSamplingRate(),length=len(sine)).GetSignal() - \ sumpf.modules.AmplifySignal(factor=0.5,input=cos_2).GetOutput() theoreticl_op_3 = sumpf.modules.AmplifySignal(factor=3.0/4.0,input=sine).GetOutput() - \ sumpf.modules.AmplifySignal(factor=3.0/4.0,input=sine_3).GetOutput() branch_simple_2 = nlsp.HammersteinModel( input_signal=sine, nonlin_func=nlsp.function_factory.power_series(2)) branch_simple_3 = nlsp.HammersteinModel( input_signal=sine, nonlin_func=nlsp.function_factory.power_series(3)) branch_up_2 = nlsp.AliasCompensatingHammersteinModelUpandDown( input_signal=sine, nonlin_func=nlsp.function_factory.power_series(2), max_harm=2) branch_up_3 = nlsp.AliasCompensatingHammersteinModelUpandDown( input_signal=sine, nonlin_func=nlsp.function_factory.power_series(3), max_harm=3) branch_lp_2 = nlsp.AliasCompensatingHammersteinModelLowpass( input_signal=sine, nonlin_func=nlsp.function_factory.power_series(2), max_harm=2) branch_lp_3 = nlsp.AliasCompensatingHammersteinModelLowpass( input_signal=sine, nonlin_func=nlsp.function_factory.power_series(3), max_harm=3) snr_simple_2 = nlsp.snr(theoreticl_op_2, branch_simple_2.GetOutput()) snr_simple_3 = nlsp.snr(theoreticl_op_3, branch_simple_3.GetOutput()) snr_up_2 = nlsp.snr(theoreticl_op_2, branch_up_2.GetOutput()) snr_up_3 = nlsp.snr(theoreticl_op_3, branch_up_3.GetOutput()) snr_lp_2 = nlsp.snr(theoreticl_op_2, branch_lp_2.GetOutput()) snr_lp_3 = nlsp.snr(theoreticl_op_3, branch_lp_3.GetOutput()) # plot.relabelandplot(sumpf.modules.FourierTransform(branch_simple.GetOutput()).GetSpectrum(),"simpleout",show=False) plot.relabelandplot(branch_up_2.GetOutput(), "upout", show=False) # plot.relabelandplot(sumpf.modules.FourierTransform(branch_lp.GetOutput()).GetSpectrum(),"lpout",show=False) plot.relabelandplot(theoreticl_op_2, "theoryout", show=True) # plot.relabelandplot(sumpf.modules.FourierTransform(ip_sine).GetSpectrum(),"input",show=True) # print "simple,2nd degree:%r" %snr_simple_2 # print "simple,3rd degree:%r" %snr_simple_3 print "up,2nd degree:%r" % snr_up_2 print "up,3rd degree:%r" % snr_up_3 print "lp,2nd degree:%r" % snr_lp_2 print "lp,3rd degree:%r" % snr_lp_3
def test_additioninHGM(): """ Construct different HM's and construct a HGM with the same parameters of the HM's. The output of the sum of HM's and the output of the HGM should be equal. """ sweep = sumpf.modules.SweepGenerator(start_frequency=20.0, stop_frequency=20000.0, samplingrate=48000.0, length=2 ** 14).GetSignal() nonlinear_functions = [nlsp.nonlinear_function.Power(degree=1), nlsp.nonlinear_function.Power(degree=2), nlsp.nonlinear_function.Power(degree=3)] nonlinear_functions1 = [nlsp.nonlinear_function.Power(degree=1), nlsp.nonlinear_function.Power(degree=2), nlsp.nonlinear_function.Power(degree=3)] aliasing_compensation1 = nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation() aliasing_compensation2 = nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation() aliasing_compensation3 = nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation() aliasing_compensation4 = nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation() model1 = nlsp.HammersteinModel(input_signal=sweep, nonlinear_function=nonlinear_functions[0], aliasing_compensation=aliasing_compensation1) model2 = nlsp.HammersteinModel(input_signal=sweep, nonlinear_function=nonlinear_functions[1], aliasing_compensation=aliasing_compensation2) model3 = nlsp.HammersteinModel(input_signal=sweep, nonlinear_function=nonlinear_functions[2], aliasing_compensation=aliasing_compensation3) total_output = model1.GetOutput() + model2.GetOutput() + model3.GetOutput() HGM = nlsp.HammersteinGroupModel(input_signal=sweep, nonlinear_functions=nonlinear_functions1, aliasing_compensation=aliasing_compensation4) HGM_output = HGM.GetOutput() assert nlsp.common.helper_functions_private.calculateenergy_timedomain(total_output) == \ nlsp.common.helper_functions_private.calculateenergy_timedomain(HGM_output)
def wgn_evaluation(): degree = 5 print "wgn evaluation" for degree in range(3, degree + 1): wgn = sumpf.modules.NoiseGenerator( sumpf.modules.NoiseGenerator.GaussianDistribution( mean=0.0, standard_deviation=1.0), samplingrate=sampling_rate, length=length) prp = sumpf.modules.ChannelDataProperties() prp.SetSignal(wgn.GetSignal()) filter = sumpf.modules.RectangleFilterGenerator( resolution=prp.GetResolution(), length=prp.GetSpectrumLength()).GetSpectrum() ref = nlsp.HammersteinModel( input_signal=wgn.GetSignal(), nonlin_func=nlsp.function_factory.power_series(degree), filter_impulseresponse=sumpf.modules.InverseFourierTransform( filter).GetSignal()).GetOutput() model_simple = nlsp.HammersteinModel( input_signal=wgn.GetSignal(), nonlin_func=nlsp.function_factory.power_series(degree)) model_up = nlsp.AliasCompensatingHammersteinModelUpandDown( input_signal=wgn.GetSignal(), nonlin_func=nlsp.function_factory.power_series(degree), max_harm=degree) model_lp = nlsp.AliasCompensatingHammersteinModelLowpass( input_signal=wgn.GetSignal(), nonlin_func=nlsp.function_factory.power_series(degree), max_harm=degree) print "degree %r" % degree print nlsp.snr(model_simple.GetOutput(), ref) print nlsp.snr(model_up.GetOutput(), ref) print nlsp.snr(model_lp.GetOutput(), ref) print print if Plot is True: plot.relabelandplot(sumpf.modules.FourierTransform( model_up.GetOutput()).GetSpectrum(), "Upsampling HM", show=False) plot.relabelandplot( sumpf.modules.FourierTransform(ref).GetSpectrum(), "Upsampling HM Ref") plot.relabelandplot(sumpf.modules.FourierTransform( model_lp.GetOutput()).GetSpectrum(), "Lowpass HM", show=False) plot.relabelandplot( sumpf.modules.FourierTransform(ref).GetSpectrum(), "Lowpass HM Ref")
def test_aliasingtest_comparewithupsampling(): """ Test the Aliasing effect in the model. The output freq are calculated by applying the signal to the upsampling hammerstein model and is compared with the model. If input freq is greater than nyquist freq then aliasing occurs because of which frequencies which are not in input signal appears in the output. This test tests the aliasing effect. if it fails then there should be freq in the output other than the theoretically calculated freq, then there should be some problem in the signal processing block """ max_harm = 2 frequency = [1000, 23000, 5222, 5698] s_rate = 48000 length = s_rate for freq in frequency: sine_signal = sumpf.modules.SineWaveGenerator(frequency=freq, phase=0.0, samplingrate=s_rate, length=length) sine_spec = sumpf.modules.FourierTransform( signal=sine_signal.GetSignal()) Test_Model_Hammerstein = nlsp.HammersteinModel( input_signal=sine_signal.GetSignal(), nonlin_func=nlsp.function_factory.power_series(max_harm)) Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput() Test_Model_outputspec = sumpf.modules.FourierTransform( Test_Model_outputsignal).GetSpectrum() Test_Model_HarmonicFreq = [] frequencies = nlsp.find_frequencies(Test_Model_outputsignal) predict_freq = nlsp.predictharmonics_usingupsampling([freq], max_harm, s_rate) if freq * max_harm < s_rate / 2: assert frequencies == predict_freq else: assert frequencies != predict_freq
def test_aliasingtest(): """ Test the Aliasing effect in the model. The output freq are calculated theoretically and is compared with the model. If input freq is greater than nyquist freq then aliasing occurs because of which frequencies which are not in input signal appears in the output. This test tests the aliasing effect. if it fails then there should be freq in the output other than the theoretically calculated freq, then there should be some problem in the signal processing block """ max_harm = 5 freq = 20000 s_rate = 48000 length = s_rate sine_signal = sumpf.modules.SineWaveGenerator(frequency=freq, phase=0.0, samplingrate=s_rate, length=length) sine_spec = sumpf.modules.FourierTransform(signal=sine_signal.GetSignal()) Test_Model_Hammerstein = nlsp.HammersteinModel( input_signal=sine_signal.GetSignal(), nonlin_func=nlsp.function_factory.power_series(max_harm)) Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput() Test_Model_outputspec = sumpf.modules.FourierTransform( Test_Model_outputsignal).GetSpectrum() Test_Model_HarmonicFreq = [] h = nlsp.find_frequencies(Test_Model_outputsignal) predicted_freq = nlsp.predictoutputfreq_usingsamplingtheory( freq, max_harm, s_rate) assert predicted_freq == h
def test_energy(): """ Test of the relation between the energy of hammerstein group model and simple hammerstein model. (a + b)^2 <= 2a^2 + 2b^2 The above relation is proved for the model using this test """ max_harm = [1] * 2 freq = 5000 s_rate = 48000 length = s_rate ip_sine_signal = sumpf.modules.SineWaveGenerator( frequency=freq, phase=0.0, samplingrate=s_rate, length=length).GetSignal() imp = sumpf.modules.ImpulseGenerator(samplingrate=s_rate, length=length).GetSignal() e = [] for harm in max_harm: Test_Model_Hammerstein = nlsp.HammersteinModel( input_signal=ip_sine_signal, nonlin_func=nlsp.function_factory.power_series(harm), filter_impulseresponse=imp) Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput() e.append( numpy.multiply(nlsp.calculateenergy_freq(Test_Model_outputsignal), 2)) hammerstein_group = nlsp.HammersteinGroupModel( input_signal=ip_sine_signal, nonlinear_functions=(nlsp.function_factory.power_series( max_harm[0]), ) * len(max_harm), filter_irs=(imp, ) * len(max_harm)) e_g = nlsp.calculateenergy_freq(hammerstein_group.GetOutput()) assert float(numpy.sum(e_g)) <= float(numpy.sum(e))
def test_modelquality(): """ Tests the quality of the model. On observing the models it is found that the upsampling hammerstein model doesnot produce aliasing effect. But other models produces aliasing. This test makes sure that aliasing will be produced in the model if frequency*max_harm is greater than sampling rate """ max_harm = [1, 2, 3, 4, 5] frequency = [1000, 5000, 10000, 15000, 23000] s_rate = 48000 length = s_rate combinations = list(itertools.product(frequency, max_harm)) for comb in combinations: freq = comb[0] harm = comb[1] sine_signal = sumpf.modules.SineWaveGenerator(frequency=freq, phase=0.0, samplingrate=s_rate, length=length) sine_spec = sumpf.modules.FourierTransform( signal=sine_signal.GetSignal()) Test_Model_Hammerstein = nlsp.HammersteinModel( input_signal=sine_signal.GetSignal(), nonlin_func=nlsp.function_factory.power_series(harm)) Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput() e = nlsp.calculateenergy_freq(Test_Model_outputsignal) h = nlsp.predictharmonics_usingupsampling([freq], harm, s_rate) f = nlsp.calculateenergy_atparticularfrequencies( Test_Model_outputsignal, h) quality = numpy.sum(f) / numpy.sum(e) if freq * harm > s_rate / 2: assert quality <= 1 else: assert quality == 1
def test_create_modified_method(): """ Test the Create_Modified method of the polynomial nonlinear functions class. """ input_signal = sumpf.modules.SweepGenerator(length=2**14).GetSignal() nl_function1 = nlsp.nonlinear_function.Power(degree=2) nl_function2 = nl_function1.CreateModified() model1 = nlsp.HammersteinModel(nonlinear_function=nl_function1) model2 = nlsp.HammersteinModel(nonlinear_function=nl_function2) model1.SetInput(input_signal) model2.SetInput(input_signal) energy1 = nlsp.common.helper_functions_private.calculateenergy_timedomain( model1.GetOutput()) energy2 = nlsp.common.helper_functions_private.calculateenergy_timedomain( model2.GetOutput()) assert energy1 == energy2
def reliability_evaluation_puretone(): length = 2**15 for max_harm in range(3, 6): frequencies = [100, 500, 1000, 2000, 4000] puretones = nlsp.generate_puretones(frequencies, sampling_rate, length) ip_signal = puretones nl_degree = max_harm model_simple = nlsp.HammersteinModel( input_signal=puretones, nonlin_func=nlsp.function_factory.power_series(nl_degree)) model_up = nlsp.AliasCompensatingHammersteinModelUpandDown( input_signal=ip_signal, nonlin_func=nlsp.function_factory.power_series(nl_degree), max_harm=max_harm) model_lp = nlsp.AliasCompensatingHammersteinModelLowpass( input_signal=ip_signal, nonlin_func=nlsp.function_factory.power_series(nl_degree), max_harm=max_harm) # plot.relabelandplot(sumpf.modules.FourierTransform(model_simple.GetOutput()).GetSpectrum(),"Uncompensated",show=False) # plot.relabelandplot(sumpf.modules.FourierTransform(model_up.GetOutput()).GetSpectrum(),"Upsampling",show=False) # plot.relabelandplot(sumpf.modules.FourierTransform(model_lp.GetOutput()).GetSpectrum(),"Lowpass filtering",show=True) plot.relabelandplot(model_simple.GetOutput(), "Uncompensated", show=False) plot.relabelandplot(model_up.GetOutput(), "upsampling", show=False) plot.relabelandplot(model_lp.GetOutput(), "lowpass", show=True) print "maxharmonics: %r" % max_harm print "snr between simple HGM and upsampling HGM: %r" % nlsp.snr( model_simple.GetOutput(), model_up.GetOutput()) print "snr between simple HGM and lowpass HGM: %r" % nlsp.snr( model_simple.GetOutput(), model_lp.GetOutput()) print
def test_inputandoutputmethods_HMandHGM(): """ Test the input and output methods of HM and HGM. """ input_signal_sweep = sumpf.modules.SweepGenerator(samplingrate=48000.0, length=2 ** 14).GetSignal() nonlinear_function = nlsp.nonlinear_function.Power(degree=5) HM = nlsp.HammersteinModel(nonlinear_function=nonlinear_function, aliasing_compensation=nlsp.aliasing_compensation.NoAliasingCompensation()) HM.SetInput(input_signal_sweep) output1 = HM.GetOutput() energy1 = nlsp.common.helper_functions_private.calculateenergy_timedomain(output1) input_signal_sine = sumpf.modules.SineWaveGenerator(samplingrate=48000.0, length=2 ** 14).GetSignal() HM.SetInput(input_signal_sine) output2 = HM.GetOutput() energy2 = nlsp.common.helper_functions_private.calculateenergy_timedomain(output2) assert energy1 != energy2 nonlinear_function = nonlinear_function.CreateModified() HGM = nlsp.HammersteinGroupModel(nonlinear_functions=[nonlinear_function, ], aliasing_compensation=nlsp.aliasing_compensation.NoAliasingCompensation()) HGM.SetInput(input_signal_sweep) energy3 = nlsp.common.helper_functions_private.calculateenergy_timedomain(HGM.GetOutput()) assert energy1 == energy3 HGM.SetInput(input_signal_sine) energy4 = nlsp.common.helper_functions_private.calculateenergy_timedomain(HGM.GetOutput()) assert energy2 == energy4
def test_puretone(): """ Tests whether hammerstein group model produces relaiable result for pure sine tone. The pure sine tone is given to hammerstein model of different maximum harmonic alias compensation and the output freq are found for each branch. Then the same pure sine tone is given to hammerstein group model and the output is observed. It should have the same frequencies which we get from individual hammerstein model. """ max_harm = [1, 2, 3, 4, 5] freq = 8000 s_rate = 48000 length = s_rate ip_sine_signal = sumpf.modules.SineWaveGenerator( frequency=freq, phase=0.0, samplingrate=s_rate, length=length).GetSignal() h = [] for harm in max_harm: Test_Model_Hammerstein = nlsp.HammersteinModel( input_signal=ip_sine_signal, nonlin_func=nlsp.function_factory.power_series(harm)) Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput() h.append(nlsp.find_frequencies(Test_Model_outputsignal)) Test_model_freq = sorted( list(set([item for sublist in h for item in sublist]))) imp = sumpf.modules.ImpulseGenerator(samplingrate=s_rate, length=length).GetSignal() hammerstein_group = nlsp.HammersteinGroupModel( input_signal=ip_sine_signal, nonlinear_functions=(nlsp.function_factory.power_series(max_harm[0]), nlsp.function_factory.power_series(max_harm[1]), nlsp.function_factory.power_series(max_harm[2]), nlsp.function_factory.power_series(max_harm[3]), nlsp.function_factory.power_series(max_harm[4])), filter_irs=(imp, ) * len(max_harm)) Test_groupmodel_freq = nlsp.find_frequencies(hammerstein_group.GetOutput()) assert Test_groupmodel_freq == Test_model_freq
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 harmonics_evaluation(): degree = 5 length = 2**18 input_signal = nlsp.NovakSweepGenerator_Sine( sampling_rate=sampling_rate, length=length, start_frequency=sweep_start_freq, stop_frequency=sweep_stop_freq) input_sweep_signal = input_signal.GetOutput() for i in range(degree, degree + 1): prp = sumpf.modules.ChannelDataProperties() prp.SetSignal(input_sweep_signal) filter = sumpf.modules.FilterGenerator( filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH( order=100), frequency=24000.0, transform=False, resolution=prp.GetResolution(), length=prp.GetSpectrumLength()).GetSpectrum() filter_ir = sumpf.modules.InverseFourierTransform(filter).GetSignal() branch_simple = nlsp.HammersteinModel( input_signal=input_sweep_signal, nonlin_func=nlsp.function_factory.power_series(i), filter_impulseresponse=filter_ir) branch_up = nlsp.AliasCompensatingHammersteinModelUpandDown( input_signal=input_sweep_signal, nonlin_func=nlsp.function_factory.power_series(i), max_harm=i, filter_impulseresponse=filter_ir) branch_lp = nlsp.AliasCompensatingHammersteinModelLowpass( input_signal=input_sweep_signal, nonlin_func=nlsp.function_factory.power_series(i), max_harm=i, filter_impulseresponse=filter_ir) harm_simple = nlsp.get_nl_impulse_response(input_signal, branch_simple.GetOutput()) harm_up = nlsp.get_nl_impulse_response(input_signal, branch_up.GetOutput()) harm_lowpass = nlsp.get_nl_impulse_response(input_signal, branch_lp.GetOutput()) plot.relabelandplot(harm_simple, "simple HM", show=True) plot.relabelandplot(harm_up, "upsampling HM", show=True) plot.relabelandplot(harm_lowpass, "lowpass HM", show=True) print nlsp.harmonicsvsall_energyratio_nl(input_signal, branch_simple.GetOutput(), i) print nlsp.harmonicsvsall_energyratio_nl(input_signal, branch_up.GetOutput(), i) print nlsp.harmonicsvsall_energyratio_nl(input_signal, branch_lp.GetOutput(), i) print print
def test_nonlinearfunction(): """ test whether the factory function and the function for nonlinearity generation works fine. the nonlinear function is called in three different methods with same input signal and same degree of nonlinearity. Hence the output of the model should have same output. The comparision is made by comaparing the energy of the output signals """ sweep_samplingrate = 48000 sweep_length = 2**18 ip_sweep_signal = sumpf.modules.SweepGenerator(samplingrate=sweep_samplingrate,length=sweep_length) hmodel = nlsp.HammersteinModel(input_signal=ip_sweep_signal.GetSignal()) hmodel.SetNLFunction(nonlin_func=nlsp.function_factory.power_series(5)) hmodel_1 = nlsp.HammersteinModel(input_signal=ip_sweep_signal.GetSignal(), nonlin_func=nlsp.function_factory.power_series(5)) energy = nlsp.calculateenergy_freq(hmodel.GetOutput()) energy_1 = nlsp.calculateenergy_freq(hmodel_1.GetOutput()) hmodel_2 = nlsp.NonlinearFunction.power_series(5,signal=ip_sweep_signal.GetSignal()) energy_2 = nlsp.calculateenergy_freq(hmodel_2.GetOutput()) energy = map(int,energy) energy_1 = map(int,energy_1) energy_2 = map(int,energy_2) assert energy == energy_1 == energy_2
def test_linearity_of_model(): """ Test the Hammerstein model for linearity using first order nonlinear function and all pass filter. """ gen_sine = sumpf.modules.SineWaveGenerator(frequency=10000.0, phase=0.0, samplingrate=48000, length=48000).GetSignal() model = nlsp.HammersteinModel(input_signal=gen_sine, nonlinear_function=nlsp.nonlinear_function.Power(degree=1), aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation()) energy_ip = nlsp.common.helper_functions_private.calculateenergy_freqdomain(gen_sine) energy_op = nlsp.common.helper_functions_private.calculateenergy_freqdomain(model.GetOutput()) assert int(energy_ip[0]) == int(energy_op[0])
def test_linearity_of_model(): """ Test the Hammerstein model for linearity for first order nonlinearity block and all pass filter linear block """ gen_sine = sumpf.modules.SineWaveGenerator(frequency=10000.0, phase=0.0, samplingrate=48000, length=48000).GetSignal() model = nlsp.HammersteinModel( input_signal=gen_sine, nonlin_func=nlsp.function_factory.power_series(1)) energy_ip = nlsp.calculateenergy_freq(gen_sine) energy_op = nlsp.calculateenergy_freq(model.GetOutput()) assert int(energy_ip[0]) == int(energy_op[0])
def test_filterlength_signallength(): """ Test whether the HM when the signal length is less than or equal to the filter length. """ length = 2 ** 16 sampling_rate = 48000 sample_signal = sumpf.modules.NoiseGenerator(samplingrate=sampling_rate, length=length, seed="signal").GetSignal() filter_kernel = nlsp.helper_functions.create_arrayof_bpfilter(branches=1, filter_length=length)[0] nl_function = nlsp.nonlinear_function.Power(degree=1) HM = nlsp.HammersteinModel(input_signal=sample_signal, nonlinear_function=nl_function, filter_impulseresponse=filter_kernel, aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation()) ser = nlsp.evaluations.CompareWithReference(reference_signal=sample_signal, signal_to_be_evaluated=HM.GetOutput()) assert ser.GetSignaltoErrorRatio()[0] >= 55
def __init__(self, input_signal=None, nonlinear_functions=(nlsp.function_factory.power_series(1), ), filter_irs=None, max_harmonics=None): """ :param signal: the input signal :param nonlinear_functions: the tuple of nonlinear functions eg. (nlsp.function_factory.power_series(1),...) :param filter_irs: the tuple of filter impulse responses eg. (IR1,...) :param max_harmonics: the tuple of maximum harmonics eg. (1,...) """ if input_signal is None: self.__signal = sumpf.Signal() else: self.__signal = input_signal self.inputstage = sumpf.modules.AmplifySignal(input=self.__signal) self.__nlfunctions = nonlinear_functions if filter_irs is None: self.__filter_irs = (sumpf.modules.ImpulseGenerator( length=len(input_signal)).GetSignal(), ) else: self.__filter_irs = filter_irs if len(self.__nlfunctions) == len(self.__filter_irs): self.__branches = len(self.__nlfunctions) else: print "the given arguments dont have same length" self.hmodels = [] self.__sums = [None] * self.__branches for nl, ir in zip(self.__nlfunctions, self.__filter_irs): h = nlsp.HammersteinModel(input_signal=self.inputstage.GetOutput(), nonlin_func=nl, filter_impulseresponse=ir) self.hmodels.append(h) for i in reversed(range(len(self.hmodels) - 1)): self.__a = sumpf.modules.AddSignals() # print "connecting hammerstein model %i to adder %i" % (i, i) sumpf.connect(self.hmodels[i].GetOutput, self.__a.SetInput1) if i == len(self.hmodels) - 2: # print "connecting hammerstein model %i to adder %i" % (i+1, i) sumpf.connect(self.hmodels[i + 1].GetOutput, self.__a.SetInput2) else: # print "connecting adder %i to adder %i" % (i+1, i) sumpf.connect(self.__sums[i + 1].GetOutput, self.__a.SetInput2) self.__sums[i] = self.__a self.GetOutput = self.__sums[0].GetOutput
def test_attenuatorofHM(): """ Test the full and reduced upsampling aliasing compensation technique for uniformity. """ sweep = sumpf.modules.SweepGenerator(start_frequency=20.0, stop_frequency=20000.0, samplingrate=48000.0, length=2 ** 14).GetSignal() model1 = nlsp.HammersteinModel(input_signal=sweep, nonlinear_function=nlsp.nonlinear_function.Power(degree=2), aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(), downsampling_position=nlsp.HammersteinModel.AFTER_NONLINEAR_BLOCK) model2 = nlsp.HammersteinModel(input_signal=sweep, nonlinear_function=nlsp.nonlinear_function.Power(degree=2), aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(), downsampling_position=nlsp.HammersteinModel.AFTER_LINEAR_BLOCK) model3 = nlsp.HammersteinModel(input_signal=sweep, nonlinear_function=nlsp.nonlinear_function.Power(degree=2), aliasing_compensation=nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation(), downsampling_position=nlsp.HammersteinModel.AFTER_LINEAR_BLOCK) model4 = nlsp.HammersteinModel(input_signal=sweep, nonlinear_function=nlsp.nonlinear_function.Power(degree=2), aliasing_compensation=nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation(), downsampling_position=nlsp.HammersteinModel.AFTER_NONLINEAR_BLOCK) model1_outputenergy = nlsp.common.helper_functions_private.calculateenergy_timedomain(model1.GetOutput())[0] model2_outputenergy = nlsp.common.helper_functions_private.calculateenergy_timedomain(model2.GetOutput())[0] model3_outputenergy = nlsp.common.helper_functions_private.calculateenergy_timedomain(model3.GetOutput())[0] model4_outputenergy = nlsp.common.helper_functions_private.calculateenergy_timedomain(model4.GetOutput())[0] assert int(model1_outputenergy) == int(model4_outputenergy) assert int(model2_outputenergy) == int(model3_outputenergy)
def test_samplingrate(): """ Test the HGM with different sampling rate signals. """ input_signal = sumpf.modules.SweepGenerator(samplingrate=96000.0, length=2 ** 10).GetSignal() aliasing_compensation = nlsp.aliasing_compensation.FullUpsamplingAliasingCompensation() nonlinear_functions = nlsp.nonlinear_function.Power(degree=5) nl_system = nlsp.HammersteinModel(aliasing_compensation=aliasing_compensation, nonlinear_function=nonlinear_functions, downsampling_position=nlsp.HammersteinModel.AFTER_LINEAR_BLOCK, input_signal=input_signal) input_signal = sumpf.modules.SineWaveGenerator(samplingrate=48000, length=2 ** 11) nl_system.SetInput(input_signal.GetSignal()) print nl_system.GetOutput()
def linearity_evaluation(): print "linearity evaluation" for i in range(3, degree + 1): max_harm = i nl_degree = i sweep_start_freq = 20.0 sweep_stop_freq = 4000.0 ip_sweep_signal = sumpf.modules.SweepGenerator( samplingrate=sampling_rate, length=length, start_frequency=sweep_start_freq, stop_frequency=sweep_stop_freq).GetSignal() model_simple = nlsp.HammersteinModel( input_signal=ip_sweep_signal, nonlin_func=nlsp.function_factory.power_series(nl_degree)) model_up = nlsp.AliasCompensatingHammersteinModelUpandDown( input_signal=ip_sweep_signal, nonlin_func=nlsp.function_factory.power_series(nl_degree), max_harm=max_harm) model_lp = nlsp.AliasCompensatingHammersteinModelLowpass( input_signal=ip_sweep_signal, nonlin_func=nlsp.function_factory.power_series(nl_degree), max_harm=max_harm) simple_ref = model_simple.GetOutput() up_ip = model_up.GetOutput() lp_ip = model_lp.GetOutput() print "degree %r" % i print nlsp.snr(simple_ref, simple_ref) print nlsp.snr(up_ip, simple_ref) print nlsp.snr(lp_ip, simple_ref) print print if Plot is True: plot.log() plot.relabelandplot(sumpf.modules.FourierTransform( model_simple.GetOutput()).GetSpectrum(), "Reference", show=False) plot.relabelandplot(sumpf.modules.FourierTransform( model_up.GetOutput()).GetSpectrum(), "Upsampling HM", show=False) plot.relabelandplot(sumpf.modules.FourierTransform( model_lp.GetOutput()).GetSpectrum(), "Lowpass HM", show=True)
def sweep_evaluation(): """ Evaluation of alias compensation of hammerstein branch using sweep. Sweep(20 to 20000Hz) of sampling rate 48000 samples/sec is upsampled to 96000 samples/sec and given to upsampling and lowpass filtering Alias compensation. And the maximum harmonic of the hammerstein branches is changed to two and the energy of the branches is evaluated. The energy of the desired band and undesired band should be equal for both upsampling and lowpass filtering alias compensation. """ print "sweep evaluation" for i in range(1, degree + 1): up_sweep_signal = sumpf.modules.ResampleSignal( signal=input_sweep_signal, samplingrate=input_sweep_signal.GetSamplingRate() * i).GetOutput() branch = nlsp.HammersteinModel( input_signal=up_sweep_signal, nonlin_func=nlsp.function_factory.power_series(i)) branch_up = nlsp.AliasCompensatingHammersteinModelUpandDown( input_signal=up_sweep_signal, nonlin_func=nlsp.function_factory.power_series(1), max_harm=i) branch_lp = nlsp.AliasCompensatingHammersteinModelLowpass( input_signal=up_sweep_signal, nonlin_func=nlsp.function_factory.power_series(1), max_harm=i, filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH( order=100)) snr = nlsp.snr( input_sweep_signal, sumpf.modules.ResampleSignal( signal=branch.GetOutput(), samplingrate=input_sweep_signal.GetSamplingRate()).GetOutput()) snr_up = nlsp.snr( input_sweep_signal, sumpf.modules.ResampleSignal( signal=branch_up.GetOutput(), samplingrate=input_sweep_signal.GetSamplingRate()).GetOutput()) snr_lp = nlsp.snr( input_sweep_signal, sumpf.modules.ResampleSignal( signal=branch_lp.GetOutput(), samplingrate=input_sweep_signal.GetSamplingRate()).GetOutput()) print snr print snr_up print snr_lp print
def reliability_evaluation_sweep(): length = 2**15 for max_harm in range(1, 6): sweep = nlsp.NovakSweepGenerator_Sine(sampling_rate=sampling_rate, length=length, start_frequency=20.0, stop_frequency=2000.0) ip_signal = sweep.GetOutput() nl_degree = max_harm model_simple = nlsp.HammersteinModel( input_signal=ip_signal, nonlin_func=nlsp.function_factory.power_series(nl_degree)) model_up = nlsp.AliasCompensatingHammersteinModelUpandDown( input_signal=ip_signal, nonlin_func=nlsp.function_factory.power_series(nl_degree), max_harm=max_harm) model_lp = nlsp.AliasCompensatingHammersteinModelLowpass( input_signal=ip_signal, nonlin_func=nlsp.function_factory.power_series(nl_degree), max_harm=max_harm) # plot.relabelandplot(sumpf.modules.FourierTransform(model_simple.GetOutput()).GetSpectrum(),"simple",show=False) # plot.relabelandplot(sumpf.modules.FourierTransform(model_up.GetOutput()).GetSpectrum(),"upsampling",show=False) # plot.relabelandplot(sumpf.modules.FourierTransform(model_lp.GetOutput()).GetSpectrum(),"lowpass",show=True) plot.relabelandplot(model_simple.GetOutput(), "Uncompensated", show=False) plot.relabelandplot(model_up.GetOutput(), "Upsampling", show=False) plot.relabelandplot(model_lp.GetOutput(), "Lowpass filtering", show=True) print "maxharmonics: %r" % max_harm print "snr between simple HGM and upsampling HGM: %r" % nlsp.snr( model_simple.GetOutput(), model_up.GetOutput()) print "snr between simple HGM and lowpass HGM: %r" % nlsp.snr( model_simple.GetOutput(), model_lp.GetOutput()) print "snr between simple HGM and upsampling HGM magnitude: %r" % nlsp.snr_magnitude( model_simple.GetOutput(), model_up.GetOutput()) print "snr between simple HGM and lowpass HGM magnitude: %r" % nlsp.snr_magnitude( model_simple.GetOutput(), model_lp.GetOutput()) print
def test_calculate_energy_of_filterkernels(): branches = 5 nl_system = nlsp.HammersteinModel( nonlinear_function=nlsp.nonlinear_function.HardClip( clipping_threshold=[-0.8, 0.8]), aliasing_compensation=nlsp.aliasing_compensation. ReducedUpsamplingAliasingCompensation(), filter_impulseresponse=nlsp.helper_functions.create_arrayof_bpfilter() [0]) system_identification = nlsp.system_identification.CosineSweep( select_branches=range(1, branches + 1), excitation_length=2**18) excitation = system_identification.GetExcitation() nl_system.SetInput(excitation) response = nl_system.GetOutput() system_identification.SetResponse(response) model = system_identification.GetOutputModel() analyze = nlsp.analyze_models.CalculateEnergyofFilterKernels() analyze.SetModel(model) energy = analyze.GetResult() diff = numpy.sum(energy) / energy[0][0] + energy[2][0] + energy[4][0] assert int(diff) == 1
def _GetFilterImpuleResponses(self): """ Get the identified filter impulse responses. :return: the filter impulse responses """ input_ex = self.__system_excitation nonlinear_functions = [ self.__nlfunction(degree=i) for i in self._select_branches ] input_signal = sumpf.modules.MergeSignals() for nonlinear_function in nonlinear_functions: aliasing_compensation = self._aliasing_compensation.CreateModified( ) model = nlsp.HammersteinModel( nonlinear_function=nonlinear_function, aliasing_compensation=aliasing_compensation, downsampling_position=self._downsampling_position) model.SetInput(input_ex) input_signal.AddInput(model.GetOutput()) input_signal = input_signal.GetOutput() desired_signal = self._system_response self.multichannel_algorithm.SetInput(input_signal=input_signal) self.multichannel_algorithm.SetDesiredOutput( desired_output=desired_signal) w = self.multichannel_algorithm.GetFilterKernel() kernel = [ sumpf.modules.SplitSignal(data=w, channels=[i]).GetOutput() for i in range(len(w.GetChannels())) ] if self._filter_length is not None: filter_kernels = [] for k in kernel: filter_kernels.append( nlsp.common.helper_functions_private.change_length_signal( signal=k, length=self._filter_length)) else: filter_kernels = kernel return filter_kernels
def test_aliasing(): """ Tests whether aliasing is present in the hammerstein group model. The pure sine tone is given to the model and the ouput frequencies found in all the branches are found. The energy of these frequencies in the output of the hammerstein group model is calculated. If there is no aliasing then this should be equal to the total energy of the output signal. """ max_harm = [1, 2, 3, 4, 5] freq = 5000 s_rate = 48000 length = s_rate ip_sine_signal = sumpf.modules.SineWaveGenerator( frequency=freq, phase=0.0, samplingrate=s_rate, length=length).GetSignal() h = [] for harm in max_harm: Test_Model_Hammerstein = nlsp.HammersteinModel( input_signal=ip_sine_signal, nonlin_func=nlsp.function_factory.power_series(harm)) Test_Model_outputsignal = Test_Model_Hammerstein.GetOutput() h.append(nlsp.find_frequencies(Test_Model_outputsignal)) Test_model_freq = sorted( list(set([item for sublist in h for item in sublist]))) imp = sumpf.modules.ImpulseGenerator(samplingrate=s_rate, length=length).GetSignal() hammerstein_group = nlsp.HammersteinGroupModel( input_signal=ip_sine_signal, nonlinear_functions=(nlsp.function_factory.power_series(max_harm[0]), nlsp.function_factory.power_series(max_harm[1]), nlsp.function_factory.power_series(max_harm[2]), nlsp.function_factory.power_series(max_harm[3]), nlsp.function_factory.power_series(max_harm[4])), filter_irs=(imp, ) * len(max_harm)) Test_groupmodel_energy_freq = nlsp.calculateenergy_atparticularfrequencies( hammerstein_group.GetOutput(), frequencies=Test_model_freq) Test_groupmodel_energy_all = nlsp.calculateenergy_freq( hammerstein_group.GetOutput()) assert numpy.sum(Test_groupmodel_energy_all) == numpy.sum( Test_groupmodel_energy_freq)
def reliability_evaluation_noise(): length = 2**18 normal = sumpf.modules.NoiseGenerator.GaussianDistribution( mean=0.0, standard_deviation=1.0) wgn_normal = nlsp.WhiteGaussianGenerator(sampling_rate=sampling_rate, length=length, start_frequency=20.0, stop_frequency=2000.0, distribution=normal) # wgn_normal = sumpf.modules.NoiseGenerator(distribution=normal,samplingrate=sampling_rate,length=length) ip_signal = wgn_normal.GetOutput() for max_harm in range(2, 3): nl_degree = max_harm model_simple = nlsp.HammersteinModel( input_signal=ip_signal, nonlin_func=nlsp.function_factory.power_series(nl_degree)) model_up = nlsp.AliasCompensatingHammersteinModelUpandDown( input_signal=ip_signal, nonlin_func=nlsp.function_factory.power_series(nl_degree), max_harm=max_harm) model_lp = nlsp.AliasCompensatingHammersteinModelLowpass( input_signal=ip_signal, nonlin_func=nlsp.function_factory.power_series(nl_degree), max_harm=max_harm) # plot.relabelandplot(sumpf.modules.FourierTransform(model_simple.GetOutput()).GetSpectrum(),"simple",show=False) # plot.relabelandplot(sumpf.modules.FourierTransform(model_up.GetOutput()).GetSpectrum(),"upsampling",show=False) # plot.relabelandplot(sumpf.modules.FourierTransform(model_lp.GetOutput()).GetSpectrum(),"lowpass",show=True) # plot.relabelandplot(model_simple.GetOutput(),"simple",show=False) # plot.relabelandplot(model_up.GetOutput(),"upsampling",show=False) # plot.relabelandplot(model_lp.GetOutput(),"lowpass",show=True) print "maxharmonics: %r" % max_harm print "snr between simple HGM and upsampling HGM: %r" % nlsp.snr( model_simple.GetOutput(), model_up.GetOutput()) print "snr between simple HGM and lowpass HGM: %r" % nlsp.snr( model_simple.GetOutput(), model_lp.GetOutput()) print
def test_SISO_NLMS_algorithm(): """ Test the SISO_NLMS_algorithm class by identifying a HGM. """ sampling_rate = 48000 length = 2 ** 15 branches = 2 input_signal = sumpf.modules.NoiseGenerator(samplingrate=sampling_rate, length=length, seed="signal").GetSignal() linear_kernels = nlsp.helper_functions.create_arrayof_bpfilter(sampling_rate=sampling_rate, branches=branches) nl_functions_1 = [nlsp.nonlinear_function.Power(i + 1) for i in range(branches)] nl_functions_2 = [nlsp.nonlinear_function.Power(i + 1) for i in range(branches)] nl_system = nlsp.HammersteinGroupModel(nonlinear_functions=nl_functions_1, filter_impulseresponses=linear_kernels, aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation()) nl_system.SetInput(input_signal=input_signal) outputs = [] nl_ = [nlsp.nonlinear_function.Power(degree=1), nlsp.nonlinear_function.Power(degree=2)] for nl in nl_: hm = nlsp.HammersteinModel( aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation(), nonlinear_function=nl) hm.SetInput(input_signal) outputs.append(hm.GetOutput()) outputs = sumpf.modules.MergeSignals(signals=outputs).GetOutput() adaptation_algorithm = nlsp.SISO_NLMS_algorithm(input_signal=outputs, filter_length=len(linear_kernels[0]), desired_output=nl_system.GetOutput()) filter_kernels = adaptation_algorithm.GetFilterKernel() filter_kernel_1 = sumpf.modules.SplitSignal(data=filter_kernels, channels=[0]).GetOutput() filter_kernel_2 = sumpf.modules.SplitSignal(data=filter_kernels, channels=[1]).GetOutput() identified_model = nlsp.HammersteinGroupModel(nonlinear_functions=nl_functions_2, filter_impulseresponses=[filter_kernel_1, filter_kernel_2], aliasing_compensation=nlsp.aliasing_compensation.ReducedUpsamplingAliasingCompensation()) identified_model.SetInput(input_signal=input_signal) evaluation = nlsp.evaluations.CompareWithReference(reference_signal=nl_system.GetOutput(), signal_to_be_evaluated=identified_model.GetOutput()) assert evaluation.GetSignaltoErrorRatio()[0][0] > 55
def test_connectors(): """ Check whether the input and output connectors are connected properly """ freq = 10000 s_rate = 48000 length = s_rate model = nlsp.HammersteinModel( nonlin_func=nlsp.function_factory.power_series(1)) energy1 = nlsp.calculateenergy_freq(model.GetOutput()) assert energy1 == [0] gen_sine = sumpf.modules.SineWaveGenerator(frequency=freq, phase=0.0, samplingrate=s_rate, length=length).GetSignal() prp = sumpf.modules.ChannelDataProperties(signal_length=length, samplingrate=s_rate) model.SetInput(gen_sine) energy2 = nlsp.calculateenergy_freq(model.GetOutput()) assert energy2 != energy1 model.SetInput(sumpf.Signal()) energy3 = nlsp.calculateenergy_freq(model.GetOutput()) assert energy3 == energy1 model.SetInput(gen_sine) model.SetNLFunction(nonlin_func=nlsp.function_factory.power_series(2)) energy4 = nlsp.calculateenergy_freq(model.GetOutput()) assert energy4 != energy3 model.SetFilterIR( sumpf.modules.InverseFourierTransform( sumpf.modules.FilterGenerator( sumpf.modules.FilterGenerator.BUTTERWORTH(order=5), frequency=freq, resolution=prp.GetResolution(), length=prp.GetSpectrumLength()).GetSpectrum()).GetSignal()) energy5 = nlsp.calculateenergy_freq(model.GetOutput()) assert energy5 != energy4
sumpf.connect(resample5_measured.GetOutput, merge_measured.AddInput) tf_measured_withharmonics = sumpf.modules.FourierTransform() sumpf.connect(merge_measured.GetOutput, tf_measured_withharmonics.SetSignal) tf_measured_fundamental = sumpf.modules.SplitSpectrum(channels=[0]) sumpf.connect(tf_measured_withharmonics.GetSpectrum, tf_measured_fundamental.SetInput) filter = sumpf.modules.FilterGenerator( filterfunction=sumpf.modules.FilterGenerator.BUTTERWORTH(order=2), frequency=20.0, transform=True, resolution=prp.GetResolution(), length=prp.GetSpectrumLength()).GetSpectrum() # model for extracting the harmonics of simulated signal model1 = nlsp.HammersteinModel( nonlin_func=nlsp.NonlinearFunction.power_series(2)) model1.SetNLFunction(nlsp.NonlinearFunction.power_series(3)) model1.SetInput(split_excitation.GetOutput()) common.plot.plot(model1.GetOutput()) # print model1._GetCutoffFrequency() spec = sumpf.modules.FourierTransform(model1.GetOutput()).GetSpectrum() common.plot.log() common.plot.plot(spec) # model1.SetFilterIR(resample1_measured.GetOutput()) # print model1._GetSamplingRate() # print model1._GetMaximumHarmonic() # model1.SetNLFunction(nlsp.NonlinearFunction.power_series(2)) # print model1._GetSamplingRate()