def test_fs_interp2(self): for mod in AVAILABLE_MOD: # parameters of signal T_x = T_y = mod.pi N_FSx = N_FSy = 5 T_cx = T_cy = math.e # And the kernel's FS coefficients. diric_FS = mod.outer( dirichlet_fs(N_FSx, T_x, T_cx, mod=mod), dirichlet_fs(N_FSy, T_y, T_cy, mod=mod) ) # Generate interpolated signal a_x, b_x = T_cx + (T_x / 2) * mod.r_[-1, 1] a_y, b_y = T_cy + (T_y / 2) * mod.r_[-1, 1] M_x = M_y = 6 diric_sig = fs_interpn(diric_FS, T=[T_x, T_y], a=[a_x, a_y], b=[b_x, b_y], M=[M_x, M_y]) # Compare with theoretical result. sample_points_x = a_x + (b_x - a_x) / (M_x - 1) * mod.arange(M_x) sample_points_y = a_y + (b_y - a_y) / (M_y - 1) * mod.arange(M_y) sample_points = [sample_points_x.reshape(M_x, 1), sample_points_y.reshape(1, M_y)] diric_sig_exact = dirichlet_2D( sample_points, T=[T_x, T_y], T_c=[T_cx, T_cy], N_FS=[N_FSx, N_FSy] ) assert mod.allclose(diric_sig, diric_sig_exact) # Try real version diric_sig_real = fs_interpn( diric_FS, T=[T_x, T_y], a=[a_x, a_y], b=[b_x, b_y], M=[M_x, M_y], real_x=True ) assert mod.allclose(diric_sig, diric_sig_real)
def test_ffsn_ref(self): T = [1, 1] T_c = [0, 0] N_FS = [3, 3] N_s = [4, 3] for mod in AVAILABLE_MOD: # Sample the kernel and do the transform. sample_points, _ = ffsn_sample(T=T, N_FS=N_FS, T_c=T_c, N_s=N_s, mod=mod) diric_samples = dirichlet_2D(sample_points=sample_points, T=T, T_c=T_c, N_FS=N_FS) diric_FS = _ffsn(x=diric_samples, T=T, N_FS=N_FS, T_c=T_c) # Compare with theoretical result. diric_FS_exact = mod.outer( dirichlet_fs(N_FS[0], T[0], T_c[0], mod=mod), dirichlet_fs(N_FS[1], T[1], T_c[1], mod=mod), ) assert mod.allclose(diric_FS[:N_FS[0], :N_FS[1]], diric_FS_exact) # Inverse transform. diric_samples_recov = _iffsn(x_FS=diric_FS, T=T, T_c=T_c, N_FS=N_FS) # Compare with original samples. assert mod.allclose(diric_samples, diric_samples_recov)
def test_ffsn_axes(self): T_x = T_y = 1 T_cx = T_cy = 0 N_FSx = N_FSy = 3 N_sx, N_sy = 4, 3 for mod in AVAILABLE_MOD: # Sample the kernel. sample_points, _ = ffsn_sample(T=[T_x, T_y], N_FS=[N_FSx, N_FSy], T_c=[T_cx, T_cy], N_s=[N_sx, N_sy], mod=mod) diric_samples = dirichlet_2D(sample_points=sample_points, T=[T_x, T_y], T_c=[T_cx, T_cy], N_FS=[N_FSx, N_FSy]) # Add new dimension. diric_samples = diric_samples[:, mod.newaxis, :] axes = (0, 2) # Perform transform. diric_FS = ffsn(x=diric_samples, T=[T_x, T_y], T_c=[T_cx, T_cy], N_FS=[N_FSx, N_FSy], axes=axes) # Compare with theoretical result. diric_FS_exact = mod.outer(dirichlet_fs(N_FSx, T_x, T_cx, mod=mod), dirichlet_fs(N_FSy, T_y, T_cy, mod=mod)) assert mod.allclose(diric_FS[:N_FSx, 0, :N_FSy], diric_FS_exact) # Inverse transform. diric_samples_recov = iffsn(x_FS=diric_FS, T=[T_x, T_y], T_c=[T_cx, T_cy], N_FS=[N_FSx, N_FSy], axes=axes) # Compare with original samples. assert mod.allclose(diric_samples, diric_samples_recov)
def test_ffs(self): T, T_c, N_FS = math.pi, math.e, 15 N_samples = 16 # Any >= N_FS will do, but highly-composite best. for mod in AVAILABLE_MOD: # Sample the kernel and do the transform. sample_points, _ = ffs_sample(T, N_FS, T_c, N_samples, mod=mod) diric_samples = dirichlet(sample_points, T, T_c, N_FS) diric_FS = ffs(diric_samples, T, T_c, N_FS) # Compare with theoretical result. assert mod.allclose(diric_FS[:N_FS], dirichlet_fs(N_FS, T, T_c)) # Inverse transform. diric_samples_recov = iffs(diric_FS, T, T_c, N_FS) # Compare with original samples. assert mod.allclose(diric_samples, diric_samples_recov)
def test_fs_interp(self): # parameters of signal T, T_c, N_FS = math.pi, math.e, 15 for mod in AVAILABLE_MOD: # And the kernel's FS coefficients. diric_FS = dirichlet_fs(N_FS, T, T_c, mod=mod) # Generate interpolated signal a, b = T_c + (T / 2) * mod.r_[-1, 1] M = 100 # We want lots of points. diric_sig = fs_interp(diric_FS, T, a, b, M) # Compare with theoretical result. t = a + (b - a) / (M - 1) * mod.arange(M) diric_sig_exact = dirichlet(t, T, T_c, N_FS) assert mod.allclose(diric_sig, diric_sig_exact) # Try real version diric_sig_real = fs_interp(diric_FS, T, a, b, M, real_x=True) assert mod.allclose(diric_sig, diric_sig_real)
def profile_fs_interp(n_trials): print(f"\nCOMPARING FS_INTERP WITH {n_trials} TRIALS") # parameters of signal T, T_c, M = math.pi, math.e, 1000 N_FS_vals = [11, 31, 101, 301, 1001, 3001, 10001, 30001, 100001] # sweep over number of interpolation points a, b = T_c + (T / 2) * np.r_[-1, 1] n_std = 0.5 real_x = {"complex": False, "real": True} proc_time = dict() proc_time_std = dict() for N_FS in N_FS_vals: print("\nNumber of FS coefficients : {}".format(N_FS)) proc_time[N_FS] = dict() proc_time_std[N_FS] = dict() # naive approach, apply synthesis formula diric_FS = dirichlet_fs(N_FS, T, T_c, mod=np) _key = "naive" timings = [] for _ in range(n_trials): start_time = time.time() naive_interp1d(diric_FS, T, a, b, M) timings.append(time.time() - start_time) proc_time[N_FS][_key] = np.mean(timings) proc_time_std[N_FS][_key] = np.std(timings) print("-- {} : {} seconds".format(_key, proc_time[N_FS][_key])) # Loop through modules for mod in AVAILABLE_MOD: backend = get_module_name(mod) print("-- module : {}".format(backend)) # compute FS coefficients diric_FS = dirichlet_fs(N_FS, T, T_c, mod=mod) # Loop through functions for _f in real_x: _key = "{}_{}".format(_f, backend) timings = [] for _ in range(n_trials): start_time = time.time() fs_interp(diric_FS, T, a, b, M, real_x=real_x[_f]) timings.append(time.time() - start_time) proc_time[N_FS][_key] = np.mean(timings) proc_time_std[N_FS][_key] = np.std(timings) print("{} version : {} seconds".format(_f, proc_time[N_FS][_key])) # plot results fig, ax = plt.subplots() util.comparison_plot(proc_time, proc_time_std, n_std, ax) ax.set_title(f"{M} samples, {n_trials} trials") ax.set_xlabel("Number of FS coefficients") fig.tight_layout() fname = pathlib.Path( __file__).resolve().parent / "profile_fs_interp_1D.png" fig.savefig(fname, dpi=300)