H = np.r_[np.real(Hc), np.imag(Hc)] # best estimate in real, imag UH = np.cov(np.c_[np.real(HMC), np.imag(HMC)], rowvar=0) # covariance of real, imag UH = make_semiposdef(UH, verbose=True) # correct for numerical errors bF, UbF = model_est.invLSFIR_unc( H, UH, N, tau, f, Fs) # Calculation of FIR deconvolution filter and its assoc. unc. CbF = UbF / (np.tile(np.sqrt(np.diag(UbF))[:, np.newaxis], (1, N + 1)) * np.tile(np.sqrt(np.diag(UbF))[:, np.newaxis].T, (N + 1, 1))) # correlation of filter coefficients # Deconvolution Step1: lowpass filter for noise attenuation fcut = f0 + 20e3 low_order = 100 # cut-off frequency and filter order blow, lshift = kaiser_lowpass(low_order, fcut, Fs) # FIR low pass filter coefficients shift = tau + lshift # delay of low-pass plus that of the FIR deconv filter # Deconvolution Step2: Application of deconvolution filter xhat, Uxhat = FIRuncFilter(yn, noise, bF, UbF, shift, blow) # apply low-pass and FIR deconv filter # Plot of results fplot = np.linspace(0, 80e3, 1000) Hc = dsp.freqz(b, a, 2 * np.pi * fplot / Fs)[1] Hif = dsp.freqz(bF, 1.0, 2 * np.pi * fplot / Fs)[1] Hl = dsp.freqz(blow, 1.0, 2 * np.pi * fplot / Fs)[1] plt.figure(1) plt.clf() plt.plot(fplot * 1e-3, db(Hc), fplot * 1e-3, db(Hif * Hl), fplot * 1e-3, db(Hc * Hif * Hl))
import numpy as np from PyDynamic.misc.testsignals import rect from PyDynamic.uncertainty.propagate_filter import FIRuncFilter from PyDynamic.misc.tools import col_hstack, make_semiposdef from PyDynamic.misc.filterstuff import kaiser_lowpass import PyDynamic.uncertainty.propagate_MonteCarlo as MC # parameters of simulated measurement Fs = 100e3 Ts = 1 / Fs # nominal system parameters fcut = 20e3 L = 100 b = kaiser_lowpass(L, fcut, Fs)[0] # uncertain knowledge: cutoff between 19.5kHz and 20.5kHz runs = 1000 FC = fcut + (2 * np.random.rand(runs) - 1) * 0.5e3 B = np.zeros((runs, L + 1)) for k in range(runs): B[k, :] = kaiser_lowpass(L, FC[k], Fs)[0] Ub = make_semiposdef(np.cov(B, rowvar=0)) # simulate input and output signals time = np.arange(0, 499 * Ts, Ts) noise = 1e-3 x = rect(time, 100 * Ts, 250 * Ts, 1.0, noise=noise)
import matplotlib.pyplot as plt ##### some definitions for all tests # parameters of simulated measurement Fs = 100e3 # sampling frequency (in Hz) Ts = 1 / Fs # sampling interval length (in s) # nominal system parameters fcut = 20e3 # low-pass filter cut-off frequency (6 dB) L = 100 # filter order b1 = kaiser_lowpass(L, fcut,Fs)[0] b2 = kaiser_lowpass(L-20,fcut,Fs)[0] # uncertain knowledge: cutoff between 19.5kHz and 20.5kHz runs = 20 FC = fcut + (2*np.random.rand(runs)-1)*0.5e3 B = np.zeros((runs,L+1)) for k in range(runs): # Monte Carlo for filter coefficients of low-pass filter B[k,:] = kaiser_lowpass(L,FC[k],Fs)[0] Ub = make_semiposdef(np.cov(B,rowvar=0)) # covariance matrix of MC result # simulate input and output signals nTime = 500 time = np.arange(nTime)*Ts # time values
import numpy as np from PyDynamic.misc.testsignals import rect from PyDynamic.uncertainty.propagate_filter import FIRuncFilter from PyDynamic.misc.tools import col_hstack, make_semiposdef from PyDynamic.misc.filterstuff import kaiser_lowpass import PyDynamic.uncertainty.propagate_MonteCarlo as MC # parameters of simulated measurement Fs = 100e3 # sampling frequency (in Hz) Ts = 1 / Fs # sampling interval length (in s) # nominal system parameters fcut = 20e3 # low-pass filter cut-off frequency (6 dB) L = 100 # filter order b = kaiser_lowpass(L,fcut,Fs)[0] # uncertain knowledge: cutoff between 19.5kHz and 20.5kHz runs = 1000 FC = fcut + (2*np.random.rand(runs)-1)*0.5e3 B = np.zeros((runs,L+1)) for k in range(runs): # Monte Carlo for filter coefficients of low-pass filter B[k,:] = kaiser_lowpass(L,FC[k],Fs)[0] Ub = make_semiposdef(np.cov(B,rowvar=0)) # covariance matrix of MC result # simulate input and output signals time = np.arange(0,499*Ts,Ts) # time values noise = 1e-5 # std of white noise x = rect(time,100*Ts,250*Ts,1.0,noise=noise) # input signal
Hc = np.mean(HMC, dtype=complex, axis=0) H = np.r_[np.real(Hc), np.imag(Hc)] UH = np.cov(np.c_[np.real(HMC), np.imag(HMC)], rowvar=0) UH = make_semiposdef(UH) # Calculation of FIR deconvolution filter and its assoc. unc. bF, UbF = deconv.LSFIR_unc(H, UH, N, tau, f, Fs) # correlation of filter coefficients CbF = UbF / (np.tile(np.sqrt(np.diag(UbF))[:, np.newaxis], (1, N + 1)) * np.tile(np.sqrt(np.diag(UbF))[:, np.newaxis].T, (N + 1, 1))) # Deconvolution Step1: lowpass filter for noise attenuation fcut = f0 + 20e3 low_order = 100 blow, lshift = kaiser_lowpass(low_order, fcut, Fs) shift = -tau - lshift # Deconvolution Step2: Application of deconvolution filter xhat, Uxhat = FIRuncFilter(yn, noise, bF, UbF, shift, blow) # Plot of results fplot = np.linspace(0, 80e3, 1000) Hc = dsp.freqz(b, a, 2 * np.pi * fplot / Fs)[1] Hif = dsp.freqz(bF, 1.0, 2 * np.pi * fplot / Fs)[1] Hl = dsp.freqz(blow, 1.0, 2 * np.pi * fplot / Fs)[1] plt.figure(1) plt.clf() plt.plot(fplot * 1e-3, db(Hc), fplot * 1e-3, db(Hif * Hl), fplot * 1e-3, db(Hc * Hif * Hl)) plt.legend(
def conduct_validation_of_FIRuncFilter(): # parameters of simulated measurement Fs = 100e3 # sampling frequency (in Hz) Ts = 1 / Fs # sampling interval length (in s) # nominal system parameters fcut = 20e3 # low-pass filter cut-off frequency (6 dB) L = 100 # filter order b1 = kaiser_lowpass(L, fcut, Fs)[0] b2 = kaiser_lowpass(L - 20, fcut, Fs)[0] # uncertain knowledge: cutoff between 19.5kHz and 20.5kHz runs = 1000 FC = fcut + (2 * np.random.rand(runs) - 1) * 0.5e3 B = np.zeros((runs, L + 1)) for k in range( runs): # Monte Carlo for filter coefficients of low-pass filter B[k, :] = kaiser_lowpass(L, FC[k], Fs)[0] Ub = make_semiposdef(np.cov( B, rowvar=False)) # covariance matrix of MC result # simulate input and output signals nTime = 500 time = np.arange(nTime) * Ts # time values # different cases sigma_noise = 1e-2 # 1e-5 for kind in ["float", "corr", "diag"]: for blow in [None, b2]: print(kind, type(blow)) if kind == "float": # input signal + run methods x = rect(time, 100 * Ts, 250 * Ts, 1.0, noise=sigma_noise) Uy, UyMC, y, yMC = _conduct_FIRuncFilter_and_MonteCarlo( Ub, b1, blow, kind, runs, sigma_noise, x) elif kind == "corr": # get an instance of noise, the covariance and the covariance-matrix # with # the specified color color = "red" noise = power_law_noise(N=nTime, color_value=color, std=sigma_noise) Ux = power_law_acf(nTime, color_value=color, std=sigma_noise) # input signal x = rect(time, 100 * Ts, 250 * Ts, 1.0, noise=noise) # build Ux_matrix from autocorrelation Ux Ux_matrix = toeplitz(trimOrPad(Ux, nTime)) # run methods y, Uy = FIRuncFilter( x, Ux, b1, Ub, blow=blow, kind=kind) # apply uncertain FIR filter (GUM formula) yMC, UyMC = MC( x, Ux_matrix, b1, np.ones(1), Ub, runs=runs, blow=blow) # apply uncertain FIR filter (Monte Carlo) elif kind == "diag": sigma_diag = sigma_noise * ( 1 + np.heaviside(np.arange(len(time)) - len(time) // 2.5, 0) ) # std doubles after half of the time noise = sigma_diag * white_gaussian(len(time)) # input signal + run methods x = rect(time, 100 * Ts, 250 * Ts, 1.0, noise=noise) Uy, UyMC, y, yMC = _conduct_FIRuncFilter_and_MonteCarlo( Ub, b1, blow, kind, runs, sigma_diag, x) # compare FIR and MC results plt.figure(1) plt.cla() plt.plot(time, x, label="input") plt.plot(time, y, label="output FIR direct") plt.plot(time, yMC, label="output FIR MC") plt.xlabel("time [s]") plt.ylabel("signal amplitude [1]") plt.legend() plt.figure(2) plt.cla() plt.plot(time, Uy, label="FIR formula") plt.plot(time, np.sqrt(np.diag(UyMC)), label="Monte Carlo") plt.xlabel("time [s]") plt.ylabel("signal uncertainty [1]") plt.legend() plt.show()
b_,a_ = dsp.bilinear(bc_,ac_,Fs) # transform to digital filter coefficients HMC[k,:] = dsp.freqz(b_,a_,2*np.pi*f/Fs)[1] # calculate DFT frequency response Hc = np.mean(HMC,dtype=complex,axis=0) # best estimate (complex-valued) H = np.r_[np.real(Hc), np.imag(Hc)] # best estimate in real, imag UH= np.cov(np.c_[np.real(HMC),np.imag(HMC)],rowvar=0) # covariance of real, imag UH= make_semiposdef(UH, verbose=True) # correct for numerical errors bF, UbF = deconv.LSFIR_unc(H,UH,N,tau,f,Fs) # Calculation of FIR deconvolution filter and its assoc. unc. CbF = UbF/(np.tile(np.sqrt(np.diag(UbF))[:,np.newaxis],(1,N+1))* np.tile(np.sqrt(np.diag(UbF))[:,np.newaxis].T,(N+1,1))) # correlation of filter coefficients # Deconvolution Step1: lowpass filter for noise attenuation fcut = f0+20e3; low_order = 100 # cut-off frequency and filter order blow, lshift = kaiser_lowpass(low_order, fcut, Fs) # FIR low pass filter coefficients shift = tau + lshift # delay of low-pass plus that of the FIR deconv filter # Deconvolution Step2: Application of deconvolution filter xhat,Uxhat = FIRuncFilter(yn,noise,bF,UbF,shift,blow) # apply low-pass and FIR deconv filter # Plot of results fplot = np.linspace(0, 80e3, 1000) Hc = dsp.freqz(b,a, 2*np.pi*fplot/Fs)[1] Hif = dsp.freqz(bF, 1.0, 2 * np.pi * fplot / Fs)[1] Hl = dsp.freqz(blow, 1.0, 2 * np.pi * fplot / Fs)[1] plt.figure(1); plt.clf() plt.plot(fplot*1e-3, db(Hc), fplot*1e-3, db(Hif*Hl), fplot*1e-3, db(Hc*Hif*Hl)) plt.legend(('System freq. resp.', 'compensation filter','compensation result')) # plt.title('Amplitude of frequency responses')