diff options
author | Anton Khirnov <anton@khirnov.net> | 2023-02-14 18:48:18 +0100 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2023-02-14 18:48:18 +0100 |
commit | 791856b14e664e9503748cf522af3be567fa9930 (patch) | |
tree | fcdfa5e5a4bddb6fc5104189791b8df9a2e7d7d2 /interp.py | |
parent | 39a9088c686d019fe5fb219eea5a4f698052d556 (diff) |
interp: add irregular 1D interpolation
Diffstat (limited to 'interp.py')
-rw-r--r-- | interp.py | 19 |
1 files changed, 19 insertions, 0 deletions
@@ -21,6 +21,25 @@ def interp1d(src_start, src_step, src_val, dst_coords, stencil): return ret +def interp1d_irregular(src_coord, src_val, dst_coord, stencil): + idx_src = np.empty(dst_coord.shape, dtype = np.int) + for i, dc in enumerate(dst_coord): + idx_src[i] = np.where(src_coord - dc < 0, src_coord, -np.inf).argmax() - stencil / 2 + 1 + + fact = np.zeros((stencil, ) + idx_src.shape, dtype = src_val.dtype) + 1.0 + for i in range(stencil): + for j in range(stencil): + if i == j: + continue + + fact[i] *= (dst_coord - src_coord[idx_src + j]) / (src_coord[idx_src + i] - src_coord[idx_src + j]) + + ret = np.zeros_like(dst_coord, dtype = src_val.dtype) + for i in range(stencil): + ret += fact[i] * src_val[idx_src + i] + + return ret + def interp2d(src_start, src_step, src_val, dst_coords, stencil): ret = np.zeros_like(dst_coords[0], dtype = src_val.dtype) |