File size: 8,138 Bytes
7885a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
"""
Discrete Fourier Transforms - basic.py
"""
import numpy as np
import functools
from . import pypocketfft as pfft
from .helper import (_asfarray, _init_nd_shape_and_axes, _datacopied,
_fix_shape, _fix_shape_1d, _normalization,
_workers)
def c2c(forward, x, n=None, axis=-1, norm=None, overwrite_x=False,
workers=None, *, plan=None):
""" Return discrete Fourier transform of real or complex sequence. """
if plan is not None:
raise NotImplementedError('Passing a precomputed plan is not yet '
'supported by scipy.fft functions')
tmp = _asfarray(x)
overwrite_x = overwrite_x or _datacopied(tmp, x)
norm = _normalization(norm, forward)
workers = _workers(workers)
if n is not None:
tmp, copied = _fix_shape_1d(tmp, n, axis)
overwrite_x = overwrite_x or copied
elif tmp.shape[axis] < 1:
message = f"invalid number of data points ({tmp.shape[axis]}) specified"
raise ValueError(message)
out = (tmp if overwrite_x and tmp.dtype.kind == 'c' else None)
return pfft.c2c(tmp, (axis,), forward, norm, out, workers)
fft = functools.partial(c2c, True)
fft.__name__ = 'fft'
ifft = functools.partial(c2c, False)
ifft.__name__ = 'ifft'
def r2c(forward, x, n=None, axis=-1, norm=None, overwrite_x=False,
workers=None, *, plan=None):
"""
Discrete Fourier transform of a real sequence.
"""
if plan is not None:
raise NotImplementedError('Passing a precomputed plan is not yet '
'supported by scipy.fft functions')
tmp = _asfarray(x)
norm = _normalization(norm, forward)
workers = _workers(workers)
if not np.isrealobj(tmp):
raise TypeError("x must be a real sequence")
if n is not None:
tmp, _ = _fix_shape_1d(tmp, n, axis)
elif tmp.shape[axis] < 1:
raise ValueError(f"invalid number of data points ({tmp.shape[axis]}) specified")
# Note: overwrite_x is not utilised
return pfft.r2c(tmp, (axis,), forward, norm, None, workers)
rfft = functools.partial(r2c, True)
rfft.__name__ = 'rfft'
ihfft = functools.partial(r2c, False)
ihfft.__name__ = 'ihfft'
def c2r(forward, x, n=None, axis=-1, norm=None, overwrite_x=False,
workers=None, *, plan=None):
"""
Return inverse discrete Fourier transform of real sequence x.
"""
if plan is not None:
raise NotImplementedError('Passing a precomputed plan is not yet '
'supported by scipy.fft functions')
tmp = _asfarray(x)
norm = _normalization(norm, forward)
workers = _workers(workers)
# TODO: Optimize for hermitian and real?
if np.isrealobj(tmp):
tmp = tmp + 0.j
# Last axis utilizes hermitian symmetry
if n is None:
n = (tmp.shape[axis] - 1) * 2
if n < 1:
raise ValueError(f"Invalid number of data points ({n}) specified")
else:
tmp, _ = _fix_shape_1d(tmp, (n//2) + 1, axis)
# Note: overwrite_x is not utilized
return pfft.c2r(tmp, (axis,), n, forward, norm, None, workers)
hfft = functools.partial(c2r, True)
hfft.__name__ = 'hfft'
irfft = functools.partial(c2r, False)
irfft.__name__ = 'irfft'
def hfft2(x, s=None, axes=(-2,-1), norm=None, overwrite_x=False, workers=None,
*, plan=None):
"""
2-D discrete Fourier transform of a Hermitian sequence
"""
if plan is not None:
raise NotImplementedError('Passing a precomputed plan is not yet '
'supported by scipy.fft functions')
return hfftn(x, s, axes, norm, overwrite_x, workers)
def ihfft2(x, s=None, axes=(-2,-1), norm=None, overwrite_x=False, workers=None,
*, plan=None):
"""
2-D discrete inverse Fourier transform of a Hermitian sequence
"""
if plan is not None:
raise NotImplementedError('Passing a precomputed plan is not yet '
'supported by scipy.fft functions')
return ihfftn(x, s, axes, norm, overwrite_x, workers)
def c2cn(forward, x, s=None, axes=None, norm=None, overwrite_x=False,
workers=None, *, plan=None):
"""
Return multidimensional discrete Fourier transform.
"""
if plan is not None:
raise NotImplementedError('Passing a precomputed plan is not yet '
'supported by scipy.fft functions')
tmp = _asfarray(x)
shape, axes = _init_nd_shape_and_axes(tmp, s, axes)
overwrite_x = overwrite_x or _datacopied(tmp, x)
workers = _workers(workers)
if len(axes) == 0:
return x
tmp, copied = _fix_shape(tmp, shape, axes)
overwrite_x = overwrite_x or copied
norm = _normalization(norm, forward)
out = (tmp if overwrite_x and tmp.dtype.kind == 'c' else None)
return pfft.c2c(tmp, axes, forward, norm, out, workers)
fftn = functools.partial(c2cn, True)
fftn.__name__ = 'fftn'
ifftn = functools.partial(c2cn, False)
ifftn.__name__ = 'ifftn'
def r2cn(forward, x, s=None, axes=None, norm=None, overwrite_x=False,
workers=None, *, plan=None):
"""Return multidimensional discrete Fourier transform of real input"""
if plan is not None:
raise NotImplementedError('Passing a precomputed plan is not yet '
'supported by scipy.fft functions')
tmp = _asfarray(x)
if not np.isrealobj(tmp):
raise TypeError("x must be a real sequence")
shape, axes = _init_nd_shape_and_axes(tmp, s, axes)
tmp, _ = _fix_shape(tmp, shape, axes)
norm = _normalization(norm, forward)
workers = _workers(workers)
if len(axes) == 0:
raise ValueError("at least 1 axis must be transformed")
# Note: overwrite_x is not utilized
return pfft.r2c(tmp, axes, forward, norm, None, workers)
rfftn = functools.partial(r2cn, True)
rfftn.__name__ = 'rfftn'
ihfftn = functools.partial(r2cn, False)
ihfftn.__name__ = 'ihfftn'
def c2rn(forward, x, s=None, axes=None, norm=None, overwrite_x=False,
workers=None, *, plan=None):
"""Multidimensional inverse discrete fourier transform with real output"""
if plan is not None:
raise NotImplementedError('Passing a precomputed plan is not yet '
'supported by scipy.fft functions')
tmp = _asfarray(x)
# TODO: Optimize for hermitian and real?
if np.isrealobj(tmp):
tmp = tmp + 0.j
noshape = s is None
shape, axes = _init_nd_shape_and_axes(tmp, s, axes)
if len(axes) == 0:
raise ValueError("at least 1 axis must be transformed")
shape = list(shape)
if noshape:
shape[-1] = (x.shape[axes[-1]] - 1) * 2
norm = _normalization(norm, forward)
workers = _workers(workers)
# Last axis utilizes hermitian symmetry
lastsize = shape[-1]
shape[-1] = (shape[-1] // 2) + 1
tmp, _ = tuple(_fix_shape(tmp, shape, axes))
# Note: overwrite_x is not utilized
return pfft.c2r(tmp, axes, lastsize, forward, norm, None, workers)
hfftn = functools.partial(c2rn, True)
hfftn.__name__ = 'hfftn'
irfftn = functools.partial(c2rn, False)
irfftn.__name__ = 'irfftn'
def r2r_fftpack(forward, x, n=None, axis=-1, norm=None, overwrite_x=False):
"""FFT of a real sequence, returning fftpack half complex format"""
tmp = _asfarray(x)
overwrite_x = overwrite_x or _datacopied(tmp, x)
norm = _normalization(norm, forward)
workers = _workers(None)
if tmp.dtype.kind == 'c':
raise TypeError('x must be a real sequence')
if n is not None:
tmp, copied = _fix_shape_1d(tmp, n, axis)
overwrite_x = overwrite_x or copied
elif tmp.shape[axis] < 1:
raise ValueError(f"invalid number of data points ({tmp.shape[axis]}) specified")
out = (tmp if overwrite_x else None)
return pfft.r2r_fftpack(tmp, (axis,), forward, forward, norm, out, workers)
rfft_fftpack = functools.partial(r2r_fftpack, True)
rfft_fftpack.__name__ = 'rfft_fftpack'
irfft_fftpack = functools.partial(r2r_fftpack, False)
irfft_fftpack.__name__ = 'irfft_fftpack'
|