Skip to content

Commit 1315d82

Browse files
committed
Adding convolve example
1 parent 764238e commit 1315d82

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

examples/getting_started/convolve.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python
2+
3+
#######################################################
4+
# Copyright (c) 2015, ArrayFire
5+
# All rights reserved.
6+
#
7+
# This file is distributed under 3-clause BSD license.
8+
# The complete license agreement can be obtained at:
9+
# http://arrayfire.com/licenses/BSD-3-Clause
10+
########################################################
11+
12+
import arrayfire as af
13+
import sys
14+
from array import array
15+
16+
def af_assert(left, right, eps=1E-6):
17+
if (af.max(af.abs(left -right)) > eps):
18+
raise ValueError("Arrays not within dictated precision")
19+
return
20+
21+
if __name__ == "__main__":
22+
if (len(sys.argv) > 1):
23+
af.set_device(int(sys.argv[1]))
24+
af.info()
25+
26+
h_dx = array('f', (1.0/12, -8.0/12, 0, 8.0/12, 1.0/12))
27+
h_spread = array('f', (1.0/5, 1.0/5, 1.0/5, 1.0/5, 1.0/5))
28+
29+
img = af.randu(640, 480)
30+
dx = af.Array(h_dx, (5,1))
31+
spread = af.Array(h_spread, (1, 5))
32+
kernel = af.matmul(dx, spread)
33+
34+
full_res = af.convolve2(img, kernel)
35+
sep_res = af.convolve2_separable(dx, spread, img)
36+
37+
af_assert(full_res, sep_res)
38+
39+
print("full 2D convolution time: %.5f ms" %
40+
(1000 * af.timeit(af.convolve2, img, kernel)))
41+
print("separable 2D convolution time: %.5f ms" %
42+
(1000 * af.timeit(af.convolve2_separable, dx, spread, img)))

0 commit comments

Comments
 (0)