Skip to content

Commit 259d3fd

Browse files
committed
Add wsinterp test for coverage
1 parent c101622 commit 259d3fd

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/diffpy/utils/parsers/resample.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ def wsinterp(x, xp, fp, left=None, right=None):
4343
4444
Returns
4545
-------
46+
float:
47+
If input x is a scalar (not an array), return the interpolated value at x.
4648
ndarray:
47-
Returns the interpolated array with dimensions of x.
49+
If input x is an array, return the interpolated array with dimensions of x.
4850
"""
4951
scalar = numpy.isscalar(x)
5052
if scalar:
@@ -58,12 +60,14 @@ def wsinterp(x, xp, fp, left=None, right=None):
5860
# shape = (nx, nxp), m(v) data spans axis 1
5961
m = fp * numpy.sinc(v)
6062
# Sum over m(v) (axis 1)
61-
fp_at_x = numpy.sum(m, axis = 1)
63+
fp_at_x = numpy.sum(m, axis=1)
6264

6365
# Enforce left and right
64-
if left is None: left = fp[0]
66+
if left is None:
67+
left = fp[0]
6568
fp_at_x[x < xp[0]] = left
66-
if right is None: right = fp[-1]
69+
if right is None:
70+
right = fp[-1]
6771
fp_at_x[x > xp[-1]] = right
6872

6973
# Return a float if we got a float
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
import numpy as np
3+
4+
from diffpy.utils.parsers.resample import wsinterp
5+
6+
7+
def test_wsinterp():
8+
import random
9+
# Check known points are unchanged by interpolation
10+
# FIXME: if another SW interp function exists, run comparisons for interpolated points
11+
12+
# Sampling rate
13+
ssr = 44100**-1 # Standard sampling rate for human-hearable frequencies
14+
t = ssr
15+
n = 5 # Do 6 samples
16+
xp = np.array([i*t for i in range(-n, n+1, 1)])
17+
x = np.array([i*t for i in range(-n-1, n+2, 1)])
18+
19+
# Interpolate a few random datasets
20+
trials = 10
21+
for trial in range(trials):
22+
fp = np.array([random.random() * ssr for i in range(-n, n+1, 1)])
23+
fp_at_x = wsinterp(x, xp, fp)
24+
assert np.allclose(fp_at_x[1:-1], fp)
25+
for i in range(len(x)):
26+
assert fp_at_x[i] == pytest.approx(wsinterp(x[i], xp, fp))
27+
28+
29+
if __name__ == '__main__':
30+
test_wsinterp()

0 commit comments

Comments
 (0)