Skip to content

Commit 84b2f2f

Browse files
shyams2pavanky
authored andcommitted
Implemented interp functionality into approx1/2
1 parent 7a680de commit 84b2f2f

File tree

2 files changed

+52
-115
lines changed

2 files changed

+52
-115
lines changed

arrayfire/signal.py

Lines changed: 40 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -13,90 +13,23 @@
1313

1414
from .library import *
1515
from .array import *
16+
from .bcast import broadcast
1617

17-
def approx1(signal, pos0, method=INTERP.LINEAR, off_grid=0.0):
18-
"""
19-
Interpolate along a single dimension.
20-
21-
Parameters
22-
----------
23-
24-
signal: af.Array
25-
A 1 dimensional signal or batch of 1 dimensional signals.
18+
@broadcast
19+
def _scale_pos_axis0(x_curr, x_orig):
20+
x0 = x_orig[0, 0, 0, 0]
21+
dx = x_orig[1, 0, 0, 0] - x0
22+
return((x_curr - x0) / dx)
2623

27-
pos0 : af.Array
28-
Locations of the interpolation points.
24+
@broadcast
25+
def _scale_pos_axis1(y_curr, y_orig):
26+
y0 = y_orig[0, 0, 0, 0]
27+
dy = y_orig[0, 1, 0, 0] - y0
28+
return((y_curr - y0) / dy)
2929

30-
method: optional: af.INTERP. default: af.INTERP.LINEAR.
31-
Interpolation method.
32-
33-
off_grid: optional: scalar. default: 0.0.
34-
The value used for positions outside the range.
35-
36-
Returns
37-
-------
38-
39-
output: af.Array
40-
Values calculated at interpolation points.
41-
42-
Note
43-
-----
44-
45-
The initial measurements are assumed to have taken place at equal steps between [0, N - 1],
46-
where N is the length of the first dimension of `signal`.
47-
48-
49-
"""
50-
output = Array()
51-
safe_call(backend.get().af_approx1(c_pointer(output.arr), signal.arr, pos0.arr,
52-
method.value, c_float_t(off_grid)))
53-
return output
54-
55-
def approx2(signal, pos0, pos1, method=INTERP.LINEAR, off_grid=0.0):
30+
def approx1(x_interpolated, x_input, signal_input, method=INTERP.LINEAR, off_grid=0.0):
5631
"""
57-
Interpolate along a two dimension.
58-
59-
Parameters
60-
----------
61-
62-
signal: af.Array
63-
A 2 dimensional signal or batch of 2 dimensional signals.
64-
65-
pos0 : af.Array
66-
Locations of the interpolation points along the first dimension.
67-
68-
pos1 : af.Array
69-
Locations of the interpolation points along the second dimension.
70-
71-
method: optional: af.INTERP. default: af.INTERP.LINEAR.
72-
Interpolation method.
73-
74-
off_grid: optional: scalar. default: 0.0.
75-
The value used for positions outside the range.
76-
77-
Returns
78-
-------
79-
80-
output: af.Array
81-
Values calculated at interpolation points.
82-
83-
Note
84-
-----
85-
86-
The initial measurements are assumed to have taken place at equal steps between [(0,0) - [M - 1, N - 1]]
87-
where M is the length of the first dimension of `signal`,
88-
and N is the length of the second dimension of `signal`.
89-
90-
91-
"""
92-
output = Array()
93-
safe_call(backend.get().af_approx2(c_pointer(output.arr), signal.arr,
94-
pos0.arr, pos1.arr, method.value, c_float_t(off_grid)))
95-
return output
96-
97-
def interp1d(x_interpolated, x_input, signal_input, method=INTERP.LINEAR, off_grid=0.0):
98-
"""
99-
One-dimensional linear interpolation.Interpolation is performed along axis 0
32+
Interpolate along a single dimension.Interpolation is performed along axis 0
10033
of the input array.
10134
10235
Parameters
@@ -106,35 +39,36 @@ def interp1d(x_interpolated, x_input, signal_input, method=INTERP.LINEAR, off_gr
10639
The x-coordinates of the interpolation points. The interpolation
10740
function is queried at these set of points.
10841
109-
x : af.Array
110-
The x-coordinates of the input data points
42+
x_input : af.Array
43+
The x-coordinates of the input data points
11144
11245
signal_input: af.Array
11346
Input signal array (signal = f(x))
11447
48+
11549
method: optional: af.INTERP. default: af.INTERP.LINEAR.
11650
Interpolation method.
11751
11852
off_grid: optional: scalar. default: 0.0.
119-
The value used for positions outside the range.
53+
The value used for positions outside the range.
12054
12155
Returns
12256
-------
12357
12458
output: af.Array
12559
Values calculated at interpolation points.
12660
"""
127-
dx = sum(x_input[1, 0, 0, 0] - x_input[0, 0, 0, 0])
128-
pos0 = (x_interpolated - sum(x_input[0, 0, 0, 0]))/dx
129-
130-
return approx1(signal_input, pos0, method, off_grid)
131-
61+
output = Array()
62+
pos0 = _scale_pos_axis0(x_interpolated, x_input)
63+
safe_call(backend.get().af_approx1(c_pointer(output.arr), signal_input.arr, pos0.arr,
64+
method.value, c_float_t(off_grid)))
65+
return output
13266

133-
def interp2d(x_interpolated, x_input, y_interpolated, y_input,
134-
signal_input, method=INTERP.LINEAR, off_grid=0.0
135-
):
67+
def approx2(x_interpolated, x_input, y_interpolated, y_input, signal_input,
68+
method=INTERP.LINEAR, off_grid=0.0
69+
):
13670
"""
137-
Two-dimensional linear interpolation.Interpolation is performed along axes 0 and 1
71+
Interpolate along a two dimension.Interpolation is performed along axes 0 and 1
13872
of the input array.
13973
14074
Parameters
@@ -144,17 +78,17 @@ def interp2d(x_interpolated, x_input, y_interpolated, y_input,
14478
The x-coordinates of the interpolation points. The interpolation
14579
function is queried at these set of points.
14680
147-
x : af.Array
148-
The x-coordinates of the input data points. The convention followed is that
149-
the x-coordinates vary along axis 0
81+
x_input : af.Array
82+
The x-coordinates of the input data points. The convention followed is that
83+
the x-coordinates vary along axis 0
15084
15185
y_interpolated : af.Array
15286
The y-coordinates of the interpolation points. The interpolation
15387
function is queried at these set of points.
15488
155-
y : af.Array
156-
The y-coordinates of the input data points. The convention followed is that
157-
the y-coordinates vary along axis 1
89+
y_input : af.Array
90+
The y-coordinates of the input data points. The convention followed is that
91+
the y-coordinates vary along axis 1
15892
15993
signal_input: af.Array
16094
Input signal array (signal = f(x, y))
@@ -163,21 +97,21 @@ def interp2d(x_interpolated, x_input, y_interpolated, y_input,
16397
Interpolation method.
16498
16599
off_grid: optional: scalar. default: 0.0.
166-
The value used for positions outside the range.
100+
The value used for positions outside the range.
167101
168102
Returns
169103
-------
170104
171105
output: af.Array
172106
Values calculated at interpolation points.
173-
"""
174-
dx = sum(x_input[1, 0, 0, 0] - x_input[0, 0, 0, 0])
175-
dy = sum(y_input[0, 1, 0, 0] - y_input[0, 0, 0, 0])
176-
177-
pos0 = (x_interpolated - sum(x_input[0, 0, 0, 0]))/dx
178-
pos1 = (y_interpolated - sum(y_input[0, 0, 0, 0]))/dy
179107
180-
return approx2(signal_input, pos0, pos1, method, off_grid)
108+
"""
109+
output = Array()
110+
pos0 = _scale_pos_axis0(x_interpolated, x_input)
111+
pos1 = _scale_pos_axis1(y_interpolated, y_input)
112+
safe_call(backend.get().af_approx2(c_pointer(output.arr), signal_input.arr,
113+
pos0.arr, pos1.arr, method.value, c_float_t(off_grid)))
114+
return output
181115

182116
def fft(signal, dim0 = None , scale = None):
183117
"""

arrayfire/tests/simple/signal.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ def simple_signal(verbose=False):
1515
display_func = _util.display_func(verbose)
1616
print_func = _util.print_func(verbose)
1717

18-
a = af.randu(10, 1)
19-
pos0 = af.randu(10) * 10
20-
display_func(af.approx1(a, pos0))
21-
22-
a = af.randu(3, 3)
23-
pos0 = af.randu(3, 3) * 10
24-
pos1 = af.randu(3, 3) * 10
25-
26-
display_func(af.approx2(a, pos0, pos1))
18+
signal = af.randu(10)
19+
x_new = af.randu(10)
20+
x_orig = af.randu(10)
21+
display_func(af.approx1(x_new, x_orig, signal))
22+
23+
signal = af.randu(3, 3)
24+
x_new = af.randu(3, 3)
25+
x_orig = af.randu(3, 3)
26+
y_new = af.randu(3, 3)
27+
y_orig = af.randu(3, 3)
28+
29+
display_func(af.approx2(x_new, x_orig, y_new, y_orig, signal))
2730

2831
a = af.randu(8, 1)
2932
display_func(a)

0 commit comments

Comments
 (0)