Skip to content

Commit 42a58f4

Browse files
sporsmgeier
authored andcommitted
Add sinc interpolation
1 parent ef49d81 commit 42a58f4

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

sfs/td/source.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
2525
"""
2626
import numpy as _np
27+
from scipy.interpolate import interp1d as _interp1d
2728

2829
from .. import default as _default
2930
from .. import util as _util
3031

3132

32-
def point(xs, signal, observation_time, grid, c=None):
33+
def point(xs, signal, observation_time, grid, c=None, interpolator_kind='linear'):
3334
r"""Source model for a point source: 3D Green's function.
3435
3536
Calculates the scalar sound pressure field for a given point in
@@ -75,6 +76,7 @@ def point(xs, signal, observation_time, grid, c=None):
7576
xs = _util.asarray_1d(xs)
7677
data, samplerate, signal_offset = _util.as_delayed_signal(signal)
7778
data = _util.asarray_1d(data)
79+
observation_time = _util.asarray_1d(observation_time)
7880
grid = _util.as_xyz_components(grid)
7981
if c is None:
8082
c = _default.c
@@ -84,12 +86,46 @@ def point(xs, signal, observation_time, grid, c=None):
8486
weights = 1 / (4 * _np.pi * r)
8587
delays = r / c
8688
base_time = observation_time - signal_offset
87-
points_at_time = _np.interp(base_time - delays,
88-
_np.arange(len(data)) / samplerate,
89-
data, left=0, right=0)
89+
if interpolator_kind == 'sinc':
90+
p = _sinc_interp(data, _np.arange(len(data)),
91+
_np.array((base_time - delays) * samplerate))
92+
else:
93+
interpolator = _interp1d(_np.arange(len(data)), data,
94+
kind=interpolator_kind, bounds_error=False,
95+
fill_value=0)
96+
p = interpolator((base_time - delays) * samplerate)
9097
# weights can be +-infinity
9198
with _np.errstate(invalid='ignore'):
92-
return weights * points_at_time
99+
return weights * p
100+
101+
102+
def _sinc_interp(x, s, u):
103+
"""
104+
Ideal sinc interpolation of a signal
105+
adapted from https://gist.github.com/endolith/1297227
106+
107+
Parameters
108+
----------
109+
x : (N,) array_like
110+
Signal to be interpolated.
111+
s : (N,) array_like
112+
Sampling instants of signal.
113+
u : (N,) array_like
114+
Sampling instants after interpolation.
115+
116+
Returns
117+
-------
118+
numpy.ndarray
119+
Interpolated signal
120+
"""
121+
122+
# sampling period
123+
T = s[1] - s[0]
124+
# perform sinc interpolation
125+
sincM = _np.tile(u, (len(s), 1)) - _np.tile(s[:, _np.newaxis], (1, len(u)))
126+
y = _np.dot(x, _np.sinc(sincM/T))
127+
128+
return y
93129

94130

95131
def point_image_sources(x0, signal, observation_time, grid, L, max_order,

0 commit comments

Comments
 (0)