{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import scipy as sp" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x:\n", " [[ 1 0 0 0]\n", " [ 0 -1 0 0]\n", " [ 0 0 3 0]\n", " [ 0 0 0 1]\n", " [ 0 0 2 0]]\n", "y:\n", " [[4 5]\n", " [3 4]]\n", "conv:\n", " [[ 4 5 0 0 0]\n", " [ 3 0 -5 0 0]\n", " [ 0 -3 8 15 0]\n", " [ 0 0 9 16 5]\n", " [ 0 0 8 13 4]\n", " [ 0 0 6 8 0]]\n", "fft for my method:\n", " [[ 4 5 0 0 0]\n", " [ 3 0 -5 0 0]\n", " [ 0 -3 7 15 0]\n", " [ 0 0 9 16 5]\n", " [ 0 0 8 13 4]\n", " [ 0 0 6 8 0]]\n", "fft:\n", " [[ 3 5 0 0 0]\n", " [ 2 0 -4 0 0]\n", " [ 0 -2 7 14 0]\n", " [ 0 0 9 16 5]\n", " [ 0 0 8 13 3]\n", " [ 0 0 6 8 0]]\n", "openpiv:\n", " [[ 4 5 0 0 0]\n", " [ 3 0 -5 0 0]\n", " [ 0 -3 7 15 0]\n", " [ 0 0 9 16 5]\n", " [ 0 0 8 13 4]\n", " [ 0 0 6 8 0]]\n", "circular: \n", " [[ 5 4 0 0]\n", " [ 3 -2 -3 0]\n", " [-2 0 17 6]\n", " [ 0 2 17 11]\n", " [ 0 2 14 8]\n", " [-1 1 10 3]]\n" ] } ], "source": [ "from scipy import signal\n", "\n", "from scipy import linalg\n", "import numpy as np\n", "from openpiv.pyprocess import fft_correlate\n", "\n", "x = [[1 , 0 , 0 , 0] , [0 , -1 , 0 , 0] , [0 , 0 , 3 , 0] , [0 , 0 , 0 , 1],[0,0,2,0]]\n", "x = np.array(x)\n", "y = [[4 , 5] , [3 , 4]]\n", "y = np.array(y)\n", "\n", "# x = np.random.rand(2,3)\n", "# y = np.random.rand(4,6)\n", "\n", "print(\"x:\\n\", x)\n", "print(\"y:\\n\", y)\n", "\n", "print (\"conv:\\n\" , signal.convolve2d(x , y , 'full'))\n", "\n", "s1 = np.array(x.shape)\n", "s2 = np.array(y.shape)\n", "\n", "size = s1 + s2 - 1\n", "\n", "\n", "fsize = 2 ** np.ceil(np.log2(size)).astype(int)\n", "fslice = tuple([slice(0, int(sz)) for sz in size])\n", "\n", "\n", "new_x = np.fft.rfft2(x , fsize)\n", "\n", "\n", "new_y = np.fft.rfft2(y , fsize)\n", "result = np.fft.irfft2(new_x*new_y)[fslice].copy()\n", "\n", "print(\"fft for my method:\\n\" , np.array(result.real, np.int32))\n", "\n", "print(\"fft:\\n\" , np.array(signal.fftconvolve(x ,y,'full'), np.int32 ))\n", "\n", "print(\"openpiv:\\n\", fft_correlate(x,y[::-1,::-1]).astype(np.int32))\n", "\n", "print(\"circular: \\n \" ,np.array(np.fft.irfft2(np.fft.rfft2(x,size)*np.fft.rfft2(y,size))).astype(np.int32))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0.],\n", " [0., 1., 1., 0.],\n", " [0., 1., 1., 0.],\n", " [0., 0., 0., 0.]])" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = np.ones((2,2))\n", "np.pad(x,np.round(np.array(x.shape)/2).astype(np.int))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2., 2.])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.round(np.array(x.shape)/2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "formats": "ipynb,py:percent" }, "kernelspec": { "display_name": "Python [conda env:windef] *", "language": "python", "name": "conda-env-windef-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }