def _decision_af(signal, symbols, precision=16): """ Make symbol decisions on signal array onto symbols. Arrayfire function. Parameters ---------- signal : array_like input signal array symbols : array_like array of symbols to decide onto precision : int, optional bit precision either 16 for complex128 or 8 for complex 64 Returns ------- out : array_like array of decided symbols """ global NMAX if precision == 16: prec_dtype = np.complex128 elif precision == 8: prec_dtype = np.complex64 else: raise ValueError( "Precision has to be either 16 for double complex or 8 for single complex" ) Nmax = NMAX // len(symbols.flatten()) // 16 L = signal.flatten().shape[0] sig = af.np_to_af_array(signal.flatten().astype(prec_dtype)) sym = af.transpose(af.np_to_af_array(symbols.flatten().astype(prec_dtype))) tmp = af.constant(0, L, dtype=af.Dtype.c64) if L < Nmax: v, idx = af.imin(af.abs(af.broadcast(lambda x, y: x - y, sig, sym)), dim=1) tmp = af.transpose(sym)[idx] else: steps = L // Nmax rem = L % Nmax for i in range(steps): v, idx = af.imin(af.abs( af.broadcast(lambda x, y: x - y, sig[i * Nmax:(i + 1) * Nmax], sym)), dim=1) tmp[i * Nmax:(i + 1) * Nmax] = af.transpose(sym)[idx] v, idx = af.imin(af.abs( af.broadcast(lambda x, y: x - y, sig[steps * Nmax:], sym)), dim=1) tmp[steps * Nmax:] = af.transpose(sym)[idx] return np.array(tmp)
def _bps_idx_af(E, angles, symbols, N): global NMAX prec_dtype = E.dtype Ntestangles = angles.shape[1] Nmax = NMAX // Ntestangles // symbols.shape[0] // 16 L = E.shape[0] EE = E[:, np.newaxis] * np.exp(1.j * angles) syms = af.np_to_af_array(symbols.astype(prec_dtype).reshape(1, 1, -1)) idxnd = np.zeros(L, dtype=np.int32) if L <= Nmax + N: Eaf = af.np_to_af_array(EE.astype(prec_dtype)) tmp = af.min(af.abs(af.broadcast(lambda x, y: x - y, Eaf[0:L, :], syms))**2, dim=2) cs = _movavg_af(tmp, 2 * N, axis=0) val, idx = af.imin(cs, dim=1) idxnd[N:-N] = np.array(idx) else: K = L // Nmax R = L % Nmax if R < N: R = R + Nmax K -= 1 Eaf = af.np_to_af_array(EE[0:Nmax + N].astype(prec_dtype)) tmp = af.min(af.abs(af.broadcast(lambda x, y: x - y, Eaf, syms))**2, dim=2) tt = np.array(tmp) cs = _movavg_af(tmp, 2 * N, axis=0) val, idx = af.imin(cs, dim=1) idxnd[N:Nmax] = np.array(idx) for i in range(1, K): Eaf = af.np_to_af_array(EE[i * Nmax - N:(i + 1) * Nmax + N].astype( np.complex128)) tmp = af.min(af.abs(af.broadcast(lambda x, y: x - y, Eaf, syms))**2, dim=2) cs = _movavg_af(tmp, 2 * N, axis=0) val, idx = af.imin(cs, dim=1) idxnd[i * Nmax:(i + 1) * Nmax] = np.array(idx) Eaf = af.np_to_af_array(EE[K * Nmax - N:K * Nmax + R].astype( np.complex128)) tmp = af.min(af.abs(af.broadcast(lambda x, y: x - y, Eaf, syms))**2, dim=2) cs = _movavg_af(tmp, 2 * N, axis=0) val, idx = af.imin(cs, dim=1) idxnd[K * Nmax:-N] = np.array(idx) return idxnd
def argmin(a: ndarray, axis: tp.Optional[int] = None, out: tp.Optional[ndarray] = None) -> ndarray: if out: raise ValueError('out != None is not supported') val, idx = af.imin(a._af_array, dim=axis) return _wrap_af_array(idx)
def templateMatchingDemo(console): root_path = os.path.dirname(os.path.abspath(__file__)) file_path = root_path if console: file_path += "/../../assets/examples/images/square.png" else: file_path += "/../../assets/examples/images/man.jpg" img_color = af.load_image(file_path, True) # Convert the image from RGB to gray-scale img = af.color_space(img_color, af.CSPACE.GRAY, af.CSPACE.RGB) iDims = img.dims() print("Input image dimensions: ", iDims) # Extract a patch from the input image patch_size = 100 tmp_img = img[100:100 + patch_size, 100:100 + patch_size] result = af.match_template(img, tmp_img) # Default disparity metric is # Sum of Absolute differences (SAD) # Currently supported metrics are # AF_SAD, AF_ZSAD, AF_LSAD, AF_SSD, # AF_ZSSD, AF_LSSD disp_img = img / 255.0 disp_tmp = tmp_img / 255.0 disp_res = normalize(result) minval, minloc = af.imin(disp_res) print("Location(linear index) of minimum disparity value = {}".format( minloc)) if not console: marked_res = af.tile(disp_img, 1, 1, 3) marked_res = draw_rectangle(marked_res, minloc%iDims[0], minloc/iDims[0],\ patch_size, patch_size) print( "Note: Based on the disparity metric option provided to matchTemplate function" ) print( "either minimum or maximum disparity location is the starting corner" ) print( "of our best matching patch to template image in the search image") wnd = af.Window(512, 512, "Template Matching Demo") while not wnd.close(): wnd.set_colormap(af.COLORMAP.DEFAULT) wnd.grid(2, 2) wnd[0, 0].image(disp_img, "Search Image") wnd[0, 1].image(disp_tmp, "Template Patch") wnd[1, 0].image(marked_res, "Best Match") wnd.set_colormap(af.COLORMAP.HEAT) wnd[1, 1].image(disp_res, "Disparity Values") wnd.show()
def argmin(self, axis: tp.Optional[int] = None, out: tp.Optional['ndarray'] = None): """ Return indices of the minimum values along the given axis of a. """ val, idx = af.imin(self._af_array, dim=axis) return _wrap_af_array(idx)
def argmin(self, axis=None): if axis is None: return self.flat.argmin(axis=0) if not isinstance(axis, numbers.Number): raise TypeError('an integer is required for the axis') val, idx = arrayfire.imin(self.d_array, pu.c2f(self.shape, axis)) shape = list(self.shape) shape.pop(axis) if(len(shape)): return ndarray(shape, dtype=pu.typemap(idx.dtype()), af_array=idx) else: return ndarray(shape, dtype=pu.typemap(idx.dtype()), af_array=idx)[()]
def argmin(self, axis=None): if axis is None: return self.flat.argmin(axis=0) if not isinstance(axis, numbers.Number): raise TypeError('an integer is required for the axis') val, idx = arrayfire.imin(self.d_array, pu.c2f(self.shape, axis)) shape = list(self.shape) shape.pop(axis) if (len(shape)): return ndarray(shape, dtype=pu.typemap(idx.dtype()), af_array=idx) else: return ndarray(shape, dtype=pu.typemap(idx.dtype()), af_array=idx)[()]
def initialize_f(r, theta, rdot, thetadot, phidot, params): # The initialization is performed to setup particles # between r = 0.9 and 1.1 at theta = 0 rho = af.select(r < 2, 1 * r**0, 0) rho = af.select(r > 1, rho, 0) rho = af.select(theta == 0, rho, 0) # Getting f at the right shape: f = rdot * rho f[:] = 0 # Changing to velocities_expanded form: f = af.moddims(f, N_p1, N_p2, N_p3, r.shape[2] * r.shape[3]) rdot = af.moddims(rdot, N_p1, N_p2, N_p3) thetadot = af.moddims(thetadot, N_p1, N_p2, N_p3) # Assigning the velocities such that thetadot ∝ 1 / r for i in range(r.shape[2]): for j in range(r.shape[3]): rho_new = rho * 0 rho_new[:, :, i, j] = rho[:, :, i, j] rho_new = af.moddims(rho_new, 1, 1, 1, r.shape[2] * r.shape[3]) # We set rdot = rdot[64] for all particles and # thetadot is inversely proportional to r # At initialization: thetadot1d = af.flat(thetadot[0, :, 0]) ind = af.imin(af.abs(thetadot1d - 1 / af.sum(r[:, :, i, j])))[1] print('rdot =', af.sum(rdot[64, ind, 0])) print('thetadot =', af.sum(thetadot[64, ind, 0])) f[64, ind, 0] += rho_new f = af.moddims(f, N_p1 * N_p2 * N_p3, 1, r.shape[2], r.shape[3]) af.eval(f) return (f)
A[1,:] = B[2,:] af.display(A) print("\n---- Bitwise operations\n") af.display(A & B) af.display(A | B) af.display(A ^ B) print("\n---- Transpose\n") af.display(A) af.display(af.transpose(A)) print("\n---- Flip Vertically / Horizontally\n") af.display(A) af.display(af.flip(A, 0)) af.display(af.flip(A, 1)) print("\n---- Sum, Min, Max along row / columns\n") af.display(A) af.display(af.sum(A, 0)) af.display(af.min(A, 0)) af.display(af.max(A, 0)) af.display(af.sum(A, 1)) af.display(af.min(A, 1)) af.display(af.max(A, 1)) print("\n---- Get minimum with index\n") (min_val, min_idx) = af.imin(A, 0) af.display(min_val) af.display(min_idx)
A[1, :] = B[2, :] af.display(A) print("\n---- Bitwise operations\n") af.display(A & B) af.display(A | B) af.display(A ^ B) print("\n---- Transpose\n") af.display(A) af.display(af.transpose(A)) print("\n---- Flip Vertically / Horizontally\n") af.display(A) af.display(af.flip(A, 0)) af.display(af.flip(A, 1)) print("\n---- Sum, Min, Max along row / columns\n") af.display(A) af.display(af.sum(A, 0)) af.display(af.min(A, 0)) af.display(af.max(A, 0)) af.display(af.sum(A, 1)) af.display(af.min(A, 1)) af.display(af.max(A, 1)) print("\n---- Get minimum with index\n") (min_val, min_idx) = af.imin(A, 0) af.display(min_val) af.display(min_idx)