{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Numpy FFT, PyFFTW\n\nThis example shows how to use the FFT or IFFT from ``Numpy``.\nIf possible the package PyFFTW is searched. If it is avaible,\nthe fast implementation of FFT and IFFT is used from this package (GPL license!!!).\n\nIn order to have the fast version of the fourier transform\nin ``pyfftw``, align your input data using ``getAlignNdArray``. This is\nan idle operation if PyFFTW is not available. Either ``np.fft.fft2``\nor ``pyfftw.interfaces.numpy_fft.fft2`` are mapped to ``myfft2``, such the\noverall call can be done by using ``myfft2(...)``. The same holds for ``myifft2(...)``.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nmyfft2 = np.fft.fft2 # default: fft2 from numpy\nmyifft2 = np.fft.ifft2 # default: ifft2 from numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define function and overwrite it when ``pyfftw`` is available. \n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def getAlignNdArray(image):\n return np.array(image)\n\n\ntry:\n import pyfftw\n\n myfft2 = pyfftw.interfaces.numpy_fft.fft2 # if PyFFTW: use fft2 from this package\n myifft2 = pyfftw.interfaces.numpy_fft.ifft2\n alignSize = pyfftw.simd_alignment\n\n def getAlignNdArray(image):\n return pyfftw.n_byte_align(np.array(image), alignSize)\n\nexcept ModuleNotFoundError:\n print(\"pyfftw could not be found. Numpy fft is used instead\")\n\n\nimage = np.random.randn(1024, 512)\nI = getAlignNdArray(image)\nI1 = myfft2(I)\nI2 = myifft2(I)\nI2" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }