有思想的Python中的线程FFT

编程入门 行业动态 更新时间:2024-10-15 02:25:53
本文介绍了有思想的Python中的线程FFT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Numpy/SciPy中的快速傅立叶变换(FFT)没有线程化. Enthought Python随Intel MKL数值库一起提供,该库具有线程化FFT的功能.一个人如何访问这些例程?

Fast Fourier Transforms (FFTs) in Numpy/SciPy are not threaded. Enthought Python is shipped with the Intel MKL numerical library, which is capable of threaded FFTs. How does one get access to these routines?

推荐答案

新版本和改进版本可以处理输入和输出数组中的任意跨度. 默认情况下,此位置现在不就位,并创建一个新的阵列. 它模仿Numpy FFT例程,但具有不同的归一化.

New and improved version which handles arbitrary strides in the input and output arrays. By default this one is now not-in-place and creates a new array. It mimics the Numpy FFT routines except that it has a different normalisation.

''' Wrapper to MKL FFT routines ''' import numpy as _np import ctypes as _ctypes mkl = _ctypes.cdll.LoadLibrary("mk2_rt.dll") _DFTI_COMPLEX = _ctypes.c_int(32) _DFTI_DOUBLE = _ctypes.c_int(36) _DFTI_PLACEMENT = _ctypes.c_int(11) _DFTI_NOT_INPLACE = _ctypes.c_int(44) _DFTI_INPUT_STRIDES = _ctypes.c_int(12) _DFTI_OUTPUT_STRIDES = _ctypes.c_int(13) def fft2(a, out=None): ''' Forward two-dimensional double-precision complex-complex FFT. Uses the Intel MKL libraries distributed with Enthought Python. Normalisation is different from Numpy! By default, allocates new memory like 'a' for output data. Returns the array containing output data. ''' assert a.dtype == _npplex128 assert len(a.shape) == 2 inplace = False if out is a: inplace = True elif out is not None: assert out.dtype == _npplex128 assert a.shape == out.shape assert not _np.may_share_memory(a, out) else: out = _np.empty_like(a) Desc_Handle = _ctypes.c_void_p(0) dims = (_ctypes.c_int*2)(*a.shape) mkl.DftiCreateDescriptor(_ctypes.byref(Desc_Handle), _DFTI_DOUBLE, _DFTI_COMPLEX, _ctypes.c_int(2), dims ) #Set input strides if necessary if not a.flags['C_CONTIGUOUS']: in_strides = (_ctypes.c_int*3)(0, a.strides[0]/16, a.strides[1]/16) mkl.DftiSetValue(Desc_Handle, _DFTI_INPUT_STRIDES, _ctypes.byref(in_strides)) if inplace: #Inplace FFT mkl.DftiCommitDescriptor(Desc_Handle) mkl.DftiComputeForward(Desc_Handle, a.ctypes.data_as(_ctypes.c_void_p) ) else: #Not-inplace FFT mkl.DftiSetValue(Desc_Handle, _DFTI_PLACEMENT, _DFTI_NOT_INPLACE) #Set output strides if necessary if not out.flags['C_CONTIGUOUS']: out_strides = (_ctypes.c_int*3)(0, out.strides[0]/16, out.strides[1]/16) mkl.DftiSetValue(Desc_Handle, _DFTI_OUTPUT_STRIDES, _ctypes.byref(out_strides)) mkl.DftiCommitDescriptor(Desc_Handle) mkl.DftiComputeForward(Desc_Handle, a.ctypes.data_as(_ctypes.c_void_p), out.ctypes.data_as(_ctypes.c_void_p) ) mkl.DftiFreeDescriptor(_ctypes.byref(Desc_Handle)) return out def ifft2(a, out=None): ''' Backward two-dimensional double-precision complex-complex FFT. Uses the Intel MKL libraries distributed with Enthought Python. Normalisation is different from Numpy! By default, allocates new memory like 'a' for output data. Returns the array containing output data. ''' assert a.dtype == _npplex128 assert len(a.shape) == 2 inplace = False if out is a: inplace = True elif out is not None: assert out.dtype == _npplex128 assert a.shape == out.shape assert not _np.may_share_memory(a, out) else: out = _np.empty_like(a) Desc_Handle = _ctypes.c_void_p(0) dims = (_ctypes.c_int*2)(*a.shape) mkl.DftiCreateDescriptor(_ctypes.byref(Desc_Handle), _DFTI_DOUBLE, _DFTI_COMPLEX, _ctypes.c_int(2), dims ) #Set input strides if necessary if not a.flags['C_CONTIGUOUS']: in_strides = (_ctypes.c_int*3)(0, a.strides[0]/16, a.strides[1]/16) mkl.DftiSetValue(Desc_Handle, _DFTI_INPUT_STRIDES, _ctypes.byref(in_strides)) if inplace: #Inplace FFT mkl.DftiCommitDescriptor(Desc_Handle) mkl.DftiComputeBackward(Desc_Handle, a.ctypes.data_as(_ctypes.c_void_p) ) else: #Not-inplace FFT mkl.DftiSetValue(Desc_Handle, _DFTI_PLACEMENT, _DFTI_NOT_INPLACE) #Set output strides if necessary if not out.flags['C_CONTIGUOUS']: out_strides = (_ctypes.c_int*3)(0, out.strides[0]/16, out.strides[1]/16) mkl.DftiSetValue(Desc_Handle, _DFTI_OUTPUT_STRIDES, _ctypes.byref(out_strides)) mkl.DftiCommitDescriptor(Desc_Handle) mkl.DftiComputeBackward(Desc_Handle, a.ctypes.data_as(_ctypes.c_void_p), out.ctypes.data_as(_ctypes.c_void_p) ) mkl.DftiFreeDescriptor(_ctypes.byref(Desc_Handle)) return out

更多推荐

有思想的Python中的线程FFT

本文发布于:2023-11-30 03:46:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648612.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   思想   Python   FFT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!