Ejemplo n.º 1
0
def test_FIR_IIR_identity(kind, fir_filter, input_signal):

    # run signal through both implementations
    y_iir, Uy_iir, _ = IIRuncFilter(*input_signal.values(), **fir_filter, kind=kind)
    y_fir, Uy_fir = FIRuncFilter(
        *input_signal.values(),
        theta=fir_filter["b"],
        Utheta=fir_filter["Uab"],
        kind=kind,
    )

    assert_allclose(y_fir, y_iir)
    assert_allclose(Uy_fir, Uy_iir)
Ejemplo n.º 2
0
def run_IIRuncFilter_in_chunks(iir_filter, input_signal):

    # slice x into smaller chunks and process them in batches
    # this tests the internal state options
    y_list = []
    Uy_list = []
    state = None
    for x_batch, Ux_batch in zip(
        np.array_split(input_signal["x"], 200), np.array_split(input_signal["Ux"], 200)
    ):
        yi, Uyi, state = IIRuncFilter(
            x_batch,
            Ux_batch,
            **iir_filter,
            kind="diag",
            state=state,
        )
        y_list.append(yi)
        Uy_list.append(Uyi)
    y = np.concatenate(y_list, axis=0)
    Uy = np.concatenate(Uy_list, axis=0)

    return y, Uy
Ejemplo n.º 3
0
for k in range(runs):  # Monte Carlo for uncertainty of filter coefficients
    bb, aa = dsp.butter(L, 2 * FC[k] / Fs, btype='lowpass')
    AB[k, :] = np.hstack((aa[1:], bb))

Uab = make_semiposdef(np.cov(AB, rowvar=0))  # correct for numerical errors

# Apply filter
time = np.arange(0, 499 * Ts, Ts)  # time values
t0 = 100 * Ts
t1 = 300 * Ts  # left and right boundary of rect signal
height = 0.9  # input signal height
noise = 1e-3  # std of white noise
x = rect(time, t0, t1, height, noise=noise)  # input signal

y, Uy = IIRuncFilter(x, noise, b, a, Uab)  # apply IIR formula (GUM)
yMC, UyMC = MC.SMC(x, noise, b, a, Uab,
                   runs=10000)  # apply sequential Monte Carlo method (GUM S1)

plt.figure(1)
plt.cla()
plt.plot(time * 1e3, x, label="input signal")
plt.plot(time * 1e3, y, label="output signal")
plt.xlabel('time / ms', fontsize=22)
plt.ylabel('signal amplitude / au', fontsize=22)
plt.tick_params(which="both", labelsize=16)

plt.figure(2)
plt.cla()
plt.plot(time * 1e3, Uy, label='IIR formula')
plt.plot(time * 1e3, UyMC, label='Monte Carlo')
Ejemplo n.º 4
0
def test_IIRuncFilter_raises_warning_for_kind_not_diag_with_scalar_covariance(
    sigma_noise, iir_filter, input_signal
):
    input_signal["Ux"] = sigma_noise
    with pytest.warns(UserWarning):
        IIRuncFilter(**input_signal, **iir_filter, kind="corr")
Ejemplo n.º 5
0
def run_IIRuncFilter_all_at_once(iir_filter, input_signal):
    y, Uy, _ = IIRuncFilter(**input_signal, **iir_filter, kind="diag")
    return y, Uy
Ejemplo n.º 6
0
FC = fcut + (2*np.random.rand(runs)-1)*0.2e3
AB = np.zeros((runs,len(b)+len(a)-1))

for k in range(runs):
	bb,aa = dsp.butter(L,2*FC[k]/Fs,btype='lowpass')
	AB[k,:] = np.hstack((aa[1:],bb))

Uab = make_semiposdef(np.cov(AB,rowvar=0))

time = np.arange(0,499*Ts,Ts)
t0 = 100*Ts; t1 = 300*Ts
height = 0.9
noise = 1e-3
x = rect(time,t0,t1,height,noise=noise)

y,Uy = IIRuncFilter(x, noise, b, a, Uab)
yMC,UyMC = MC.SMC(x,noise,b,a,Uab,runs=10000)

plt.figure(1);plt.cla()
plt.plot(time*1e3, x, label="input signal")
plt.plot(time*1e3, y, label="output signal")
plt.xlabel('time / ms',fontsize=22)
plt.ylabel('signal amplitude / au',fontsize=22)
plt.tick_params(which="both",labelsize=16)

plt.figure(2);plt.cla()
plt.plot(time*1e3, Uy, label='IIR formula')
plt.plot(time*1e3, UyMC, label='Monte Carlo')
# plt.title('uncertainty of filter output')
plt.legend()
plt.xlabel('time / ms',fontsize=22)