Skip to content

Commit 5b7241c

Browse files
committed
FEAT/TEST: imported all functions from singal.h
1 parent e35f3c4 commit 5b7241c

File tree

4 files changed

+305
-10
lines changed

4 files changed

+305
-10
lines changed

arrayfire/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
# http://arrayfire.com/licenses/BSD-3-Clause
88
########################################################
99

10-
from .library import *
11-
from .data import *
12-
from .util import *
13-
from .algorithm import *
14-
from .device import *
15-
from .blas import *
16-
from .arith import *
10+
from .library import *
11+
from .data import *
12+
from .util import *
13+
from .algorithm import *
14+
from .device import *
15+
from .blas import *
16+
from .arith import *
1717
from .statistics import *
18-
from .lapack import *
18+
from .lapack import *
19+
from .signal import *

arrayfire/signal.py

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#######################################################
2+
# Copyright (c) 2014, ArrayFire
3+
# All rights reserved.
4+
#
5+
# This file is distributed under 3-clause BSD license.
6+
# The complete license agreement can be obtained at:
7+
# http://arrayfire.com/licenses/BSD-3-Clause
8+
########################################################
9+
10+
from .library import *
11+
from .array import *
12+
13+
def approx1(signal, pos0, method=AF_INTERP_LINEAR, off_grid=0.0):
14+
output = array()
15+
safe_call(clib.af_approx1(pointer(output.arr), signal.arr, pos0.arr, method, c_double(off_grid)))
16+
return output
17+
18+
def approx2(signal, pos0, pos1, method=AF_INTERP_LINEAR, off_grid=0.0):
19+
output = array()
20+
safe_call(clib.af_approx2(pointer(output.arr), signal.arr, \
21+
pos0.arr, pos1.arr, method, c_double(off_grid)))
22+
return output
23+
24+
def fft(signal, dim0 = None , scale = None):
25+
26+
if dim0 is None:
27+
dim0 = 0
28+
29+
if scale is None:
30+
scale = 1.0
31+
32+
output = array()
33+
safe_call(clib.af_fft(pointer(output.arr), signal.arr, c_double(scale), c_longlong(dim0)))
34+
return output
35+
36+
def fft2(signal, dim0 = None, dim1 = None , scale = None):
37+
38+
if dim0 is None:
39+
dim0 = 0
40+
41+
if dim1 is None:
42+
dim1 = 0
43+
44+
if scale is None:
45+
scale = 1.0
46+
47+
output = array()
48+
safe_call(clib.af_fft2(pointer(output.arr), signal.arr, c_double(scale),\
49+
c_longlong(dim0), c_longlong(dim1)))
50+
return output
51+
52+
def fft3(signal, dim0 = None, dim1 = None , dim2 = None, scale = None):
53+
54+
if dim0 is None:
55+
dim0 = 0
56+
57+
if dim1 is None:
58+
dim1 = 0
59+
60+
if dim2 is None:
61+
dim2 = 0
62+
63+
if scale is None:
64+
scale = 1.0
65+
66+
output = array()
67+
safe_call(clib.af_fft3(pointer(output.arr), signal.arr, c_double(scale),\
68+
c_longlong(dim0), c_longlong(dim1), c_longlong(dim2)))
69+
return output
70+
71+
def ifft(signal, dim0 = None , scale = None):
72+
73+
if dim0 is None:
74+
dim0 = signal.dims()[0]
75+
76+
if scale is None:
77+
scale = 1.0/float(dim0)
78+
79+
output = array()
80+
safe_call(clib.af_ifft(pointer(output.arr), signal.arr, c_double(scale), c_longlong(dim0)))
81+
return output
82+
83+
def ifft2(signal, dim0 = None, dim1 = None , scale = None):
84+
85+
dims = signal.dims()
86+
87+
if (len(dims) < 2):
88+
return ifft(signal)
89+
90+
if dim0 is None:
91+
dim0 = dims[0]
92+
93+
if dim1 is None:
94+
dim1 = dims[1]
95+
96+
if scale is None:
97+
scale = 1.0/float(dim0 * dim1)
98+
99+
output = array()
100+
safe_call(clib.af_ifft2(pointer(output.arr), signal.arr, c_double(scale),\
101+
c_longlong(dim0), c_longlong(dim1)))
102+
return output
103+
104+
def ifft3(signal, dim0 = None, dim1 = None , dim2 = None, scale = None):
105+
106+
dims = signal.dims()
107+
108+
if (len(dims) < 3):
109+
return ifft2(signal)
110+
111+
if dim0 is None:
112+
dim0 = dims[0]
113+
114+
if dim1 is None:
115+
dim1 = dims[1]
116+
117+
if dim2 is None:
118+
dim2 = dims[2]
119+
120+
if scale is None:
121+
scale = 1.0 / float(dim0 * dim1 * dim2)
122+
123+
output = array()
124+
safe_call(clib.af_ifft3(pointer(output.arr), signal.arr, c_double(scale),\
125+
c_longlong(dim0), c_longlong(dim1), c_longlong(dim2)))
126+
return output
127+
128+
def dft(signal, scale = None, odims=(None, None, None, None)):
129+
130+
odims4 = dim4_tuple(odims, default=None)
131+
132+
dims = signal.dims()
133+
ndims = len(dims)
134+
135+
if (ndims == 1):
136+
return fft(signal, scale, dims[0])
137+
elif (ndims == 2):
138+
return fft2(signal, scale, dims[0], dims[1])
139+
else:
140+
return fft3(signal, scale, dims[0], dims[1], dims[2])
141+
142+
def idft(signal, scale = None, odims=(None, None, None, None)):
143+
144+
odims4 = dim4_tuple(odims, default=None)
145+
146+
dims = signal.dims()
147+
ndims = len(dims)
148+
149+
if (ndims == 1):
150+
return ifft(signal, scale, dims[0])
151+
elif (ndims == 2):
152+
return ifft2(signal, scale, dims[0], dims[1])
153+
else:
154+
return ifft3(signal, scale, dims[0], dims[1], dims[2])
155+
156+
def convolve1(signal, kernel, conv_mode = AF_CONV_DEFAULT, conv_domain = AF_CONV_AUTO):
157+
output = array()
158+
safe_call(clib.af_convolve1(pointer(output.arr), signal.arr, kernel.arr, conv_mode, conv_domain))
159+
return output
160+
161+
def convolve2(signal, kernel, conv_mode = AF_CONV_DEFAULT, conv_domain = AF_CONV_AUTO):
162+
output = array()
163+
safe_call(clib.af_convolve2(pointer(output.arr), signal.arr, kernel.arr, conv_mode, conv_domain))
164+
return output
165+
166+
def convolve3(signal, kernel, conv_mode = AF_CONV_DEFAULT, conv_domain = AF_CONV_AUTO):
167+
output = array()
168+
safe_call(clib.af_convolve3(pointer(output.arr), signal.arr, kernel.arr, conv_mode, conv_domain))
169+
return output
170+
171+
def convolve(signal, kernel, conv_mode = AF_CONV_DEFAULT, conv_domain = AF_CONV_AUTO):
172+
dims = signal.dims()
173+
ndims = len(dims)
174+
175+
if (ndims == 1):
176+
return convolve1(signal, kernel, conv_mode, conv_domain)
177+
elif (ndims == 2):
178+
return convolve2(signal, kernel, conv_mode, conv_domain)
179+
else:
180+
return convolve3(signal, kernel, conv_mode, conv_domain)
181+
182+
def fft_convolve1(signal, kernel, conv_mode = AF_CONV_DEFAULT):
183+
output = array()
184+
safe_call(clib.af_fft_convolve1(pointer(output.arr), signal.arr, kernel.arr, conv_mode))
185+
return output
186+
187+
def fft_convolve2(signal, kernel, conv_mode = AF_CONV_DEFAULT):
188+
output = array()
189+
safe_call(clib.af_fft_convolve2(pointer(output.arr), signal.arr, kernel.arr, conv_mode))
190+
return output
191+
192+
def fft_convolve3(signal, kernel, conv_mode = AF_CONV_DEFAULT):
193+
output = array()
194+
safe_call(clib.af_fft_convolve3(pointer(output.arr), signal.arr, kernel.arr, conv_mode))
195+
return output
196+
197+
def fft_convolve(signal, kernel, conv_mode = AF_CONV_DEFAULT):
198+
dims = signal.dims()
199+
ndims = len(dims)
200+
201+
if (ndims == 1):
202+
return fft_convolve1(signal, kernel, conv_mode)
203+
elif (ndims == 2):
204+
return fft_convolve2(signal, kernel, conv_mode)
205+
else:
206+
return fft_convolve3(signal, kernel, conv_mode)
207+
208+
def fir(B, X):
209+
Y = array()
210+
safe_call(clib.af_fir(pointer(Y.arr), B.arr, X.arr))
211+
return Y
212+
213+
def iir(B, A, X):
214+
Y = array()
215+
safe_call(clib.af_iir(pointer(Y.arr), B.arr, A.arr, X.arr))
216+
return Y

arrayfire/util.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
########################################################
99

1010
from .library import *
11+
import numbers
1112

1213
def dim4(d0=1, d1=1, d2=1, d3=1):
1314
c_dim4 = c_longlong * 4
@@ -18,9 +19,13 @@ def dim4(d0=1, d1=1, d2=1, d3=1):
1819

1920
return out
2021

21-
def dim4_tuple(dims):
22+
def dim4_tuple(dims, default=1):
2223
assert(isinstance(dims, tuple))
23-
out = [1]*4
24+
25+
if (default is not None):
26+
assert(isinstance(default, numbers.Number))
27+
28+
out = [default]*4
2429

2530
for i, dim in enumerate(dims):
2631
out[i] = dim

tests/simple_signal.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/python
2+
#######################################################
3+
# Copyright (c) 2014, ArrayFire
4+
# All rights reserved.
5+
#
6+
# This file is distributed under 3-clause BSD license.
7+
# The complete license agreement can be obtained at:
8+
# http://arrayfire.com/licenses/BSD-3-Clause
9+
########################################################
10+
11+
import arrayfire as af
12+
13+
a = af.randu(10, 1)
14+
pos0 = af.randu(10) * 10
15+
af.print_array(af.approx1(a, pos0))
16+
17+
a = af.randu(3, 3)
18+
pos0 = af.randu(3, 3) * 10
19+
pos1 = af.randu(3, 3) * 10
20+
21+
af.print_array(af.approx2(a, pos0, pos1))
22+
23+
a = af.randu(8, 1)
24+
af.print_array(a)
25+
26+
af.print_array(af.fft(a))
27+
af.print_array(af.dft(a))
28+
af.print_array(af.real(af.ifft(af.fft(a))))
29+
af.print_array(af.real(af.idft(af.dft(a))))
30+
31+
a = af.randu(4, 4)
32+
af.print_array(a)
33+
34+
af.print_array(af.fft2(a))
35+
af.print_array(af.dft(a))
36+
af.print_array(af.real(af.ifft2(af.fft2(a))))
37+
af.print_array(af.real(af.idft(af.dft(a))))
38+
39+
a = af.randu(4, 4, 2)
40+
af.print_array(a)
41+
42+
af.print_array(af.fft3(a))
43+
af.print_array(af.dft(a))
44+
af.print_array(af.real(af.ifft3(af.fft3(a))))
45+
af.print_array(af.real(af.idft(af.dft(a))))
46+
47+
a = af.randu(10, 1)
48+
b = af.randu(3, 1)
49+
af.print_array(af.convolve1(a, b))
50+
af.print_array(af.fft_convolve1(a, b))
51+
af.print_array(af.convolve(a, b))
52+
af.print_array(af.fft_convolve(a, b))
53+
54+
a = af.randu(5, 5)
55+
b = af.randu(3, 3)
56+
af.print_array(af.convolve2(a, b))
57+
af.print_array(af.fft_convolve2(a, b))
58+
af.print_array(af.convolve(a, b))
59+
af.print_array(af.fft_convolve(a, b))
60+
61+
a = af.randu(5, 5, 3)
62+
b = af.randu(3, 3, 2)
63+
af.print_array(af.convolve3(a, b))
64+
af.print_array(af.fft_convolve3(a, b))
65+
af.print_array(af.convolve(a, b))
66+
af.print_array(af.fft_convolve(a, b))
67+
68+
69+
b = af.randu(3, 1)
70+
x = af.randu(10, 1)
71+
a = af.randu(2, 1)
72+
af.print_array(af.fir(b, x))
73+
af.print_array(af.iir(b, a, x))

0 commit comments

Comments
 (0)