Skip to content

Commit e053bb5

Browse files
syurkevi9prady9
authored andcommitted
adds updated approx functions to python wrapper
1 parent a2cd9ce commit e053bb5

File tree

1 file changed

+185
-19
lines changed

1 file changed

+185
-19
lines changed

arrayfire/signal.py

Lines changed: 185 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _scale_pos_axis1(y_curr, y_orig):
2727
dy = y_orig[0, 1, 0, 0] - y0
2828
return((y_curr - y0) / dy)
2929

30-
def approx1(signal, x, method=INTERP.LINEAR, off_grid=0.0, xp = None):
30+
def approx1(signal, x, method=INTERP.LINEAR, off_grid=0.0, xp = None, output = None):
3131
"""
3232
Interpolate along a single dimension.Interpolation is performed along axis 0
3333
of the input array.
@@ -51,6 +51,10 @@ def approx1(signal, x, method=INTERP.LINEAR, off_grid=0.0, xp = None):
5151
xp : af.Array
5252
The x-coordinates of the input data points
5353
54+
output: None or af.Array
55+
Optional preallocated output array. If it is a sub-array of an existing af_array,
56+
only the corresponding portion of the af_array will be overwritten
57+
5458
Returns
5559
-------
5660
@@ -65,20 +69,86 @@ def approx1(signal, x, method=INTERP.LINEAR, off_grid=0.0, xp = None):
6569
where N is the length of the first dimension of `signal`.
6670
"""
6771

68-
output = Array()
72+
if output is None:
73+
output = Array()
74+
75+
if(xp is not None):
76+
pos0 = _scale_pos_axis0(x, xp)
77+
else:
78+
pos0 = x
79+
80+
safe_call(backend.get().af_approx1(c_pointer(output.arr), signal.arr, pos0.arr,
81+
method.value, c_float_t(off_grid)))
6982

70-
if(xp is not None):
71-
pos0 = _scale_pos_axis0(x, xp)
7283
else:
73-
pos0 = x
84+
if(xp is not None):
85+
pos0 = _scale_pos_axis0(x, xp)
86+
else:
87+
pos0 = x
88+
safe_call(backend.get().af_approx1_v2(c_pointer(output.arr), signal.arr, pos0.arr,
89+
method.value, c_float_t(off_grid)))
90+
return output
91+
92+
93+
def approx1_uniform(signal, x, interp_dim, idx_start, idx_step, method=INTERP.LINEAR, off_grid=0.0, output = None):
94+
"""
95+
Interpolation on one dimensional signals along specified dimension.
96+
97+
af_approx1_uniform() accepts the dimension to perform the interpolation along the input.
98+
It also accepts start and step values which define the uniform range of corresponding indices.
99+
100+
Parameters
101+
----------
102+
103+
signal: af.Array
104+
Input signal array (signal = f(x))
105+
106+
x: af.Array
107+
The x-coordinates of the interpolation points. The interpolation
108+
function is queried at these set of points.
109+
110+
interp_dim: scalar
111+
is the dimension to perform interpolation across.
112+
113+
idx_start: scalar
114+
is the first index value along interp_dim.
115+
116+
idx_step: scalar
117+
is the uniform spacing value between subsequent indices along interp_dim.
74118
75-
safe_call(backend.get().af_approx1(c_pointer(output.arr), signal.arr, pos0.arr,
76-
method.value, c_float_t(off_grid)))
119+
method: optional: af.INTERP. default: af.INTERP.LINEAR.
120+
Interpolation method.
121+
122+
off_grid: optional: scalar. default: 0.0.
123+
The value used for positions outside the range.
124+
125+
output: None or af.Array
126+
Optional preallocated output array. If it is a sub-array of an existing af_array,
127+
only the corresponding portion of the af_array will be overwritten
128+
129+
Returns
130+
-------
131+
132+
output: af.Array
133+
Values calculated at interpolation points.
134+
135+
"""
136+
137+
if output is None:
138+
output = Array()
139+
140+
safe_call(backend.get().af_approx1_uniform(c_pointer(output.arr), signal.arr, x.arr,
141+
c_dim_t(interp_dim), c_double_t(idx_start), c_double_t(idx_step),
142+
method.value, c_float_t(off_grid)))
143+
else:
144+
safe_call(backend.get().af_approx1_uniform_v2(c_pointer(output.arr), signal.arr, x.arr,
145+
c_dim_t(interp_dim), c_double_t(idx_start), c_double_t(idx_step),
146+
method.value, c_float_t(off_grid)))
77147
return output
78148

149+
79150
def approx2(signal, x, y,
80-
method=INTERP.LINEAR, off_grid=0.0, xp = None, yp = None
81-
):
151+
method=INTERP.LINEAR, off_grid=0.0, xp = None, yp = None, output = None):
82152
"""
83153
Interpolate along a two dimension.Interpolation is performed along axes 0 and 1
84154
of the input array.
@@ -112,6 +182,10 @@ def approx2(signal, x, y,
112182
The y-coordinates of the input data points. The convention followed is that
113183
the y-coordinates vary along axis 1
114184
185+
output: None or af.Array
186+
Optional preallocated output array. If it is a sub-array of an existing af_array,
187+
only the corresponding portion of the af_array will be overwritten
188+
115189
Returns
116190
-------
117191
@@ -127,22 +201,114 @@ def approx2(signal, x, y,
127201
and N is the length of the second dimension of `signal`.
128202
"""
129203

130-
output = Array()
131-
132-
if(xp is not None):
133-
pos0 = _scale_pos_axis0(x, xp)
204+
if output is None:
205+
output = Array()
206+
207+
if(xp is not None):
208+
pos0 = _scale_pos_axis0(x, xp)
209+
else:
210+
pos0 = x
211+
212+
if(yp is not None):
213+
pos1 = _scale_pos_axis1(y, yp)
214+
else:
215+
pos1 = y
216+
217+
safe_call(backend.get().af_approx2(c_pointer(output.arr), signal.arr,
218+
pos0.arr, pos1.arr, method.value, c_float_t(off_grid)))
134219
else:
135-
pos0 = x
220+
if(xp is not None):
221+
pos0 = _scale_pos_axis0(x, xp)
222+
else:
223+
pos0 = x
224+
225+
if(yp is not None):
226+
pos1 = _scale_pos_axis1(y, yp)
227+
else:
228+
pos1 = y
229+
230+
safe_call(backend.get().af_approx2_v2(c_pointer(output.arr), signal.arr,
231+
pos0.arr, pos1.arr, method.value, c_float_t(off_grid)))
232+
233+
return output
234+
235+
def approx2_uniform(signal, pos0, interp_dim0, idx_start0, idx_step0, pos1, interp_dim1, idx_start1, idx_step1,
236+
method=INTERP.LINEAR, off_grid=0.0, output = None):
237+
"""
238+
Interpolate along two uniformly spaced dimensions of the input array.
239+
af_approx2_uniform() accepts two dimensions to perform the interpolation along the input.
240+
It also accepts start and step values which define the uniform range of corresponding indices.
241+
242+
Parameters
243+
----------
244+
245+
signal: af.Array
246+
Input signal array (signal = f(x, y))
247+
248+
pos0 : af.Array
249+
positions of the interpolation points along interp_dim0.
250+
251+
interp_dim0: scalar
252+
is the first dimension to perform interpolation across.
253+
254+
idx_start0: scalar
255+
is the first index value along interp_dim0.
256+
257+
idx_step0: scalar
258+
is the uniform spacing value between subsequent indices along interp_dim0.
259+
260+
pos1 : af.Array
261+
positions of the interpolation points along interp_dim1.
262+
263+
interp_dim1: scalar
264+
is the second dimension to perform interpolation across.
265+
266+
idx_start1: scalar
267+
is the first index value along interp_dim1.
268+
269+
idx_step1: scalar
270+
is the uniform spacing value between subsequent indices along interp_dim1.
271+
272+
method: optional: af.INTERP. default: af.INTERP.LINEAR.
273+
Interpolation method.
274+
275+
off_grid: optional: scalar. default: 0.0.
276+
The value used for positions outside the range.
277+
278+
output: None or af.Array
279+
Optional preallocated output array. If it is a sub-array of an existing af_array,
280+
only the corresponding portion of the af_array will be overwritten
281+
282+
Returns
283+
-------
284+
285+
output: af.Array
286+
Values calculated at interpolation points.
287+
288+
Note
289+
-----
290+
This holds applicable when x_input/y_input isn't provided:
136291
137-
if(yp is not None):
138-
pos1 = _scale_pos_axis1(y, yp)
292+
The initial measurements are assumed to have taken place at equal steps between [(0,0) - [M - 1, N - 1]]
293+
where M is the length of the first dimension of `signal`,
294+
and N is the length of the second dimension of `signal`.
295+
"""
296+
297+
if output is None:
298+
output = Array()
299+
safe_call(backend.get().af_approx2_uniform(c_pointer(output.arr), signal.arr,
300+
pos0.arr, c_dim_t(interp_dim0), c_double_t(idx_start0), c_double_t(idx_step0),
301+
pos1.arr, c_dim_t(interp_dim1), c_double_t(idx_start1), c_double_t(idx_step1),
302+
method.value, c_float_t(off_grid)))
139303
else:
140-
pos1 = y
304+
safe_call(backend.get().af_approx2_uniform_v2(c_pointer(output.arr), signal.arr,
305+
pos0.arr, c_dim_t(interp_dim0), c_double_t(idx_start0), c_double_t(idx_step0),
306+
pos1.arr, c_dim_t(interp_dim1), c_double_t(idx_start1), c_double_t(idx_step1),
307+
method.value, c_float_t(off_grid)))
141308

142-
safe_call(backend.get().af_approx2(c_pointer(output.arr), signal.arr,
143-
pos0.arr, pos1.arr, method.value, c_float_t(off_grid)))
144309
return output
145310

311+
146312
def fft(signal, dim0 = None , scale = None):
147313
"""
148314
Fast Fourier Transform: 1D

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy