def A_matrix(): ''' Calculates A matrix whose elements :math:`A_{p i}` are given by :math:`A_{pi} = \\int^1_{-1} L_p(\\xi)L_i(\\xi) \\frac{dx}{d\\xi}` The integrals are computed using the integrate() function. Since elements are taken to be of equal size, :math:`\\frac {dx}{d\\xi}` is same everywhere Returns ------- A_matrix : arrayfire.Array [N_LGL N_LGL 1 1] The value of integral of product of lagrange basis functions obtained by LGL points, using the integrate() function ''' # Coefficients of Lagrange basis polynomials. lagrange_coeffs = params.lagrange_coeffs lagrange_coeffs = af.reorder(lagrange_coeffs, 1, 2, 0) # Coefficients of product of Lagrange basis polynomials. lag_prod_coeffs = af.convolve1(lagrange_coeffs,\ af.reorder(lagrange_coeffs, 0, 2, 1),\ conv_mode=af.CONV_MODE.EXPAND) lag_prod_coeffs = af.reorder(lag_prod_coeffs, 1, 2, 0) lag_prod_coeffs = af.moddims(lag_prod_coeffs, params.N_LGL**2, 2 * params.N_LGL - 1) dx_dxi = params.dx_dxi A_matrix = dx_dxi * af.moddims(lagrange.integrate(lag_prod_coeffs),\ params.N_LGL, params.N_LGL) return A_matrix
def lagrange_polynomial_coeffs(x): ''' This function doesn't use poly1d. It calculates the coefficients of the Lagrange basis polynomials. A function to get the analytical form and the coefficients of Lagrange basis polynomials evaluated using x nodes. It calculates the Lagrange basis polynomials using the formula: .. math:: \\ L_i = \\prod_{m = 0, m \\notin i}^{N - 1}\\frac{(x - x_m)}{(x_i - x_m)} Parameters ---------- x : numpy.array [N_LGL 1 1 1] Contains the :math: `x` nodes using which the lagrange basis functions need to be evaluated. Returns ------- lagrange_basis_coeffs : numpy.ndarray A :math: `N \\times N` matrix containing the coefficients of the Lagrange basis polynomials such that :math:`i^{th}` lagrange polynomial will be the :math:`i^{th}` row of the matrix. ''' X = np.array(x) lagrange_basis_poly = [] lagrange_basis_coeffs = af.np_to_af_array( np.zeros([X.shape[0], X.shape[0]])) for j in np.arange(X.shape[0]): lagrange_basis_k = af.np_to_af_array(np.array([1.])) for m in np.arange(X.shape[0]): if m != j: lagrange_basis_k = af.convolve1(lagrange_basis_k,\ af.np_to_af_array(np.array([1, -X[m]])/ (X[j] - X[m])),\ conv_mode=af.CONV_MODE.EXPAND) lagrange_basis_coeffs[j] = af.transpose(lagrange_basis_k) return lagrange_basis_coeffs
def poly1d_product(poly_a, poly_b): ''' Finds the product of two polynomials using the arrayfire convolve1 function. Parameters ---------- poly_a : af.Array[N degree_a 1 1] :math:`N` polynomials of degree :math:`degree` poly_b : af.Array[N degree_b 1 1] :math:`N` polynomials of degree :math:`degree_b` ''' return af.transpose( af.convolve1(af.transpose(poly_a), af.transpose(poly_b), conv_mode=af.CONV_MODE.EXPAND))
af.display(af.fft2(a)) af.display(af.dft(a)) af.display(af.real(af.ifft2(af.fft2(a)))) af.display(af.real(af.idft(af.dft(a)))) a = af.randu(4, 4, 2) af.display(a) af.display(af.fft3(a)) af.display(af.dft(a)) af.display(af.real(af.ifft3(af.fft3(a)))) af.display(af.real(af.idft(af.dft(a)))) a = af.randu(10, 1) b = af.randu(3, 1) af.display(af.convolve1(a, b)) af.display(af.fft_convolve1(a, b)) af.display(af.convolve(a, b)) af.display(af.fft_convolve(a, b)) a = af.randu(5, 5) b = af.randu(3, 3) af.display(af.convolve2(a, b)) af.display(af.fft_convolve2(a, b)) af.display(af.convolve(a, b)) af.display(af.fft_convolve(a, b)) a = af.randu(5, 5, 3) b = af.randu(3, 3, 2) af.display(af.convolve3(a, b)) af.display(af.fft_convolve3(a, b))
def simple_signal(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(10, 1) pos0 = af.randu(10) * 10 display_func(af.approx1(a, pos0)) a = af.randu(3, 3) pos0 = af.randu(3, 3) * 10 pos1 = af.randu(3, 3) * 10 display_func(af.approx2(a, pos0, pos1)) a = af.randu(8, 1) display_func(a) display_func(af.fft(a)) display_func(af.dft(a)) display_func(af.real(af.ifft(af.fft(a)))) display_func(af.real(af.idft(af.dft(a)))) a = af.randu(4, 4) display_func(a) display_func(af.fft2(a)) display_func(af.dft(a)) display_func(af.real(af.ifft2(af.fft2(a)))) display_func(af.real(af.idft(af.dft(a)))) a = af.randu(4, 4, 2) display_func(a) display_func(af.fft3(a)) display_func(af.dft(a)) display_func(af.real(af.ifft3(af.fft3(a)))) display_func(af.real(af.idft(af.dft(a)))) a = af.randu(10, 1) b = af.randu(3, 1) display_func(af.convolve1(a, b)) display_func(af.fft_convolve1(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) a = af.randu(5, 5) b = af.randu(3, 3) display_func(af.convolve2(a, b)) display_func(af.fft_convolve2(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) a = af.randu(5, 5, 3) b = af.randu(3, 3, 2) display_func(af.convolve3(a, b)) display_func(af.fft_convolve3(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) b = af.randu(3, 1) x = af.randu(10, 1) a = af.randu(2, 1) display_func(af.fir(b, x)) display_func(af.iir(b, a, x))
def simple_signal(verbose=False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) signal = af.randu(10) x_new = af.randu(10) x_orig = af.randu(10) display_func(af.approx1(signal, x_new, xp = x_orig)) signal = af.randu(3, 3) x_new = af.randu(3, 3) x_orig = af.randu(3, 3) y_new = af.randu(3, 3) y_orig = af.randu(3, 3) display_func(af.approx2(signal, x_new, y_new, xp = x_orig, yp = y_orig)) a = af.randu(8, 1) display_func(a) display_func(af.fft(a)) display_func(af.dft(a)) display_func(af.real(af.ifft(af.fft(a)))) display_func(af.real(af.idft(af.dft(a)))) b = af.fft(a) af.ifft_inplace(b) display_func(b) af.fft_inplace(b) display_func(b) b = af.fft_r2c(a) c = af.fft_c2r(b) display_func(b) display_func(c) a = af.randu(4, 4) display_func(a) display_func(af.fft2(a)) display_func(af.dft(a)) display_func(af.real(af.ifft2(af.fft2(a)))) display_func(af.real(af.idft(af.dft(a)))) b = af.fft2(a) af.ifft2_inplace(b) display_func(b) af.fft2_inplace(b) display_func(b) b = af.fft2_r2c(a) c = af.fft2_c2r(b) display_func(b) display_func(c) a = af.randu(4, 4, 2) display_func(a) display_func(af.fft3(a)) display_func(af.dft(a)) display_func(af.real(af.ifft3(af.fft3(a)))) display_func(af.real(af.idft(af.dft(a)))) b = af.fft3(a) af.ifft3_inplace(b) display_func(b) af.fft3_inplace(b) display_func(b) b = af.fft3_r2c(a) c = af.fft3_c2r(b) display_func(b) display_func(c) a = af.randu(10, 1) b = af.randu(3, 1) display_func(af.convolve1(a, b)) display_func(af.fft_convolve1(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) a = af.randu(5, 5) b = af.randu(3, 3) display_func(af.convolve2(a, b)) display_func(af.fft_convolve2(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) a = af.randu(5, 5, 3) b = af.randu(3, 3, 2) display_func(af.convolve3(a, b)) display_func(af.fft_convolve3(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) b = af.randu(3, 1) x = af.randu(10, 1) a = af.randu(2, 1) display_func(af.fir(b, x)) display_func(af.iir(b, a, x)) display_func(af.medfilt1(a)) display_func(af.medfilt2(a)) display_func(af.medfilt(a))
def simple_signal(verbose=False): display_func = _util.display_func(verbose) signal = af.randu(10) x_new = af.randu(10) x_orig = af.randu(10) display_func(af.approx1(signal, x_new, xp=x_orig)) signal = af.randu(3, 3) x_new = af.randu(3, 3) x_orig = af.randu(3, 3) y_new = af.randu(3, 3) y_orig = af.randu(3, 3) display_func(af.approx2(signal, x_new, y_new, xp=x_orig, yp=y_orig)) a = af.randu(8, 1) display_func(a) display_func(af.fft(a)) display_func(af.dft(a)) display_func(af.real(af.ifft(af.fft(a)))) display_func(af.real(af.idft(af.dft(a)))) b = af.fft(a) af.ifft_inplace(b) display_func(b) af.fft_inplace(b) display_func(b) b = af.fft_r2c(a) c = af.fft_c2r(b) display_func(b) display_func(c) a = af.randu(4, 4) display_func(a) display_func(af.fft2(a)) display_func(af.dft(a)) display_func(af.real(af.ifft2(af.fft2(a)))) display_func(af.real(af.idft(af.dft(a)))) b = af.fft2(a) af.ifft2_inplace(b) display_func(b) af.fft2_inplace(b) display_func(b) b = af.fft2_r2c(a) c = af.fft2_c2r(b) display_func(b) display_func(c) a = af.randu(4, 4, 2) display_func(a) display_func(af.fft3(a)) display_func(af.dft(a)) display_func(af.real(af.ifft3(af.fft3(a)))) display_func(af.real(af.idft(af.dft(a)))) b = af.fft3(a) af.ifft3_inplace(b) display_func(b) af.fft3_inplace(b) display_func(b) b = af.fft3_r2c(a) c = af.fft3_c2r(b) display_func(b) display_func(c) a = af.randu(10, 1) b = af.randu(3, 1) display_func(af.convolve1(a, b)) display_func(af.fft_convolve1(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) a = af.randu(5, 5) b = af.randu(3, 3) display_func(af.convolve2(a, b)) display_func(af.fft_convolve2(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) c = af.convolve2NN(a, b) display_func(c) in_dims = c.dims() incoming_grad = af.constant(1, in_dims[0], in_dims[1]) g = af.convolve2GradientNN(incoming_grad, a, b, c) display_func(g) a = af.randu(5, 5, 3) b = af.randu(3, 3, 2) display_func(af.convolve3(a, b)) display_func(af.fft_convolve3(a, b)) display_func(af.convolve(a, b)) display_func(af.fft_convolve(a, b)) b = af.randu(3, 1) x = af.randu(10, 1) a = af.randu(2, 1) display_func(af.fir(b, x)) display_func(af.iir(b, a, x)) display_func(af.medfilt1(a)) display_func(af.medfilt2(a)) display_func(af.medfilt(a))