Skip to content

Commit 812cce2

Browse files
Till Rettbergmgeier
authored andcommitted
Change interpolation API, reimplement sinc interpolation, add time signal zeropadding
1 parent 42a58f4 commit 812cce2

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

sfs/td/source.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,31 @@
3030
from .. import util as _util
3131

3232

33-
def point(xs, signal, observation_time, grid, c=None, interpolator_kind='linear'):
33+
def linear_interpolator(x, y):
34+
"""1d linear interpolator with zero-padding.
35+
36+
Parameters
37+
----------
38+
x : (N,) array_like
39+
Sampling points.
40+
y : (N,) array_like
41+
Values at sampling points.
42+
43+
Returns
44+
-------
45+
function
46+
Piecewise linear interpolant.
47+
48+
"""
49+
x = _util.asarray_1d(x)
50+
y = _util.asarray_1d(y)
51+
x = _np.concatenate([_np.array([min(x)-1]), x, _np.array([max(x)+1])])
52+
y = _np.concatenate([_np.array([0]), y, _np.array([0])])
53+
return _interp1d(x, y, bounds_error=False, fill_value=0)
54+
55+
56+
def point(xs, signal, observation_time, grid, c=None,
57+
interpolator=linear_interpolator):
3458
r"""Source model for a point source: 3D Green's function.
3559
3660
Calculates the scalar sound pressure field for a given point in
@@ -50,6 +74,9 @@ def point(xs, signal, observation_time, grid, c=None, interpolator_kind='linear'
5074
See `sfs.util.xyz_grid()`.
5175
c : float, optional
5276
Speed of sound.
77+
interpolator : function, optional
78+
A function which constructs and returns a 1d interpolator.
79+
see: linear_interpolator, sinc_interpolator
5380
5481
Returns
5582
-------
@@ -86,46 +113,19 @@ def point(xs, signal, observation_time, grid, c=None, interpolator_kind='linear'
86113
weights = 1 / (4 * _np.pi * r)
87114
delays = r / c
88115
base_time = observation_time - signal_offset
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)
116+
p = interpolator(_np.arange(len(data)), data)((base_time - delays) * samplerate)
97117
# weights can be +-infinity
98118
with _np.errstate(invalid='ignore'):
99119
return weights * p
100120

101121

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))
122+
def sinc_interpolator(x, y):
123+
x = _util.asarray_1d(x)
124+
y = _util.asarray_1d(y)
127125

128-
return y
126+
def f(xnew):
127+
return sum([y[i] * _np.sinc(xnew - x[i]) for i in range(len(x))])
128+
return f
129129

130130

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

0 commit comments

Comments
 (0)