|
| 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 |
0 commit comments