本文整理汇总了Python中pyfftw.FFTW类的典型用法代码示例。如果您正苦于以下问题:Python FFTW类的具体用法?Python FFTW怎么用?Python FFTW使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FFTW类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_test_arrays
def create_test_arrays(self, input_shape, output_shape, axes=None):
a = self.input_dtype(numpy.random.randn(*input_shape)
+1j*numpy.random.randn(*input_shape))
b = self.output_dtype(numpy.random.randn(*output_shape))
# We fill a by doing the forward FFT from b.
# This means that the relevant bits that should be purely
# real will be (for example the zero freq component).
# This is easier than writing a complicate system to work it out.
try:
if axes == None:
fft = FFTW(b,a,direction='FFTW_FORWARD')
else:
fft = FFTW(b,a,direction='FFTW_FORWARD', axes=axes)
b[:] = self.output_dtype(numpy.random.randn(*output_shape))
fft.execute()
scaling = numpy.prod(numpy.array(a.shape))
a = self.input_dtype(a/scaling)
except ValueError:
# In this case, we assume that it was meant to error,
# so we can return what we want.
pass
b = self.output_dtype(numpy.random.randn(*output_shape))
return a, b
示例2: test_default_args
def test_default_args(self):
in_shape = self.input_shapes['2d']
out_shape = self.output_shapes['2d']
a, b = self.create_test_arrays(in_shape, out_shape)
fft = FFTW(a,b)
fft.execute()
ref_b = self.reference_fftn(a, axes=(-1,))
self.assertTrue(numpy.allclose(b, ref_b, rtol=1e-2, atol=1e-3))
示例3: setUp
def setUp(self):
self.input_array = empty_aligned((256, 512), dtype='complex128', n=16)
self.output_array = empty_aligned((256, 512), dtype='complex128', n=16)
self.fft = FFTW(self.input_array, self.output_array)
self.input_array[:] = (numpy.random.randn(*self.input_array.shape)
+ 1j*numpy.random.randn(*self.input_array.shape))
示例4: setUp
def setUp(self):
self.input_array = n_byte_align_empty((256, 512), 16,
dtype='complex128')
self.output_array = n_byte_align_empty((256, 512), 16,
dtype='complex128')
self.fft = FFTW(self.input_array, self.output_array)
self.output_array[:] = (numpy.random.randn(*self.output_array.shape)
+ 1j*numpy.random.randn(*self.output_array.shape))
示例5: run_validate_fft
def run_validate_fft(self, a, b, axes, fft=None, ifft=None,
force_unaligned_data=False, create_array_copies=True,
threads=1, flags=('FFTW_ESTIMATE',)):
''' *** EVERYTHING IS FLIPPED AROUND BECAUSE WE ARE
VALIDATING AN INVERSE FFT ***
Run a validation of the FFTW routines for the passed pair
of arrays, a and b, and the axes argument.
a and b are assumed to be the same shape (but not necessarily
the same layout in memory).
fft and ifft, if passed, should be instantiated FFTW objects.
If force_unaligned_data is True, the flag FFTW_UNALIGNED
will be passed to the fftw routines.
'''
if create_array_copies:
# Don't corrupt the original mutable arrays
a = a.copy()
b = b.copy()
a_orig = a.copy()
flags = list(flags)
if force_unaligned_data:
flags.append('FFTW_UNALIGNED')
if ifft == None:
ifft = FFTW(a, b, axes=axes, direction='FFTW_BACKWARD',
flags=flags, threads=threads)
else:
ifft.update_arrays(a,b)
if fft == None:
fft = FFTW(b, a, axes=axes, direction='FFTW_FORWARD',
flags=flags, threads=threads)
else:
fft.update_arrays(b,a)
a[:] = a_orig
# Test the inverse FFT by comparing it to the result from numpy.fft
ifft.execute()
a[:] = a_orig
ref_b = self.reference_fftn(a, axes=axes)
# The scaling is the product of the lengths of the fft along
# the axes along which the fft is taken.
scaling = numpy.prod(numpy.array(b.shape)[list(axes)])
self.assertEqual(ifft.N, scaling)
self.assertEqual(fft.N, scaling)
# This is actually quite a poor relative error, but it still
# sometimes fails. I assume that numpy.fft has different internals
# to fftw.
self.assertTrue(numpy.allclose(b/scaling, ref_b, rtol=1e-2, atol=1e-3))
# Test the FFT by comparing the result to the starting
# value (which is scaled as per FFTW being unnormalised).
fft.execute()
self.assertTrue(numpy.allclose(a/scaling, a_orig, rtol=1e-2, atol=1e-3))
return fft, ifft
示例6: do_fft
def do_fft(self, inputa):
outputa = np.zeros(self.fft_size, dtype=complex)
fft_plan = FFTW(inputa, outputa, direction='FFTW_FORWARD')
#fft_plan = FFTW.Plan(inputa, outputa, direction='forward', flags=['estimate'])
fft_plan.execute()
return (np.log10(np.abs(outputa)) * 20)[:self.fft_size/2]
示例7: FFTWMiscTest
class FFTWMiscTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(FFTWMiscTest, self).__init__(*args, **kwargs)
# Assume python 3, but keep backwards compatibility
if not hasattr(self, 'assertRaisesRegex'):
self.assertRaisesRegex = self.assertRaisesRegexp
def setUp(self):
self.input_array = n_byte_align_empty((256, 512), 16,
dtype='complex128')
self.output_array = n_byte_align_empty((256, 512), 16,
dtype='complex128')
self.fft = FFTW(self.input_array, self.output_array)
self.output_array[:] = (numpy.random.randn(*self.output_array.shape)
+ 1j*numpy.random.randn(*self.output_array.shape))
def test_aligned_flag(self):
'''Test to see if the aligned flag is correct
'''
fft = FFTW(self.input_array, self.output_array)
self.assertTrue(fft.simd_aligned)
fft = FFTW(self.input_array, self.output_array,
flags=('FFTW_UNALIGNED',))
self.assertFalse(fft.simd_aligned)
def test_flags(self):
'''Test to see if the flags are correct
'''
fft = FFTW(self.input_array, self.output_array)
self.assertEqual(fft.flags, ('FFTW_MEASURE',))
fft = FFTW(self.input_array, self.output_array,
flags=('FFTW_DESTROY_INPUT', 'FFTW_UNALIGNED'))
self.assertEqual(fft.flags, ('FFTW_DESTROY_INPUT', 'FFTW_UNALIGNED'))
# Test an implicit flag
_input_array = n_byte_align_empty(256, 16, dtype='complex64')
_output_array = n_byte_align_empty(256, 16, dtype='complex64')
# These are guaranteed to be misaligned (due to dtype size == 8)
input_array = _input_array[:-1]
output_array = _output_array[:-1]
u_input_array = _input_array[1:]
u_output_array = _output_array[1:]
fft = FFTW(input_array, u_output_array)
self.assertEqual(fft.flags, ('FFTW_MEASURE', 'FFTW_UNALIGNED'))
fft = FFTW(u_input_array, output_array)
self.assertEqual(fft.flags, ('FFTW_MEASURE', 'FFTW_UNALIGNED'))
fft = FFTW(u_input_array, u_output_array)
self.assertEqual(fft.flags, ('FFTW_MEASURE', 'FFTW_UNALIGNED'))
def test_differing_aligned_arrays_update(self):
'''Test to see if the alignment code is working as expected
'''
# Start by creating arrays that are only on various byte
# alignments (4, 16 and 32)
_input_array = n_byte_align_empty(
len(self.input_array.ravel())*2+5,
32, dtype='float32')
_output_array = n_byte_align_empty(
len(self.output_array.ravel())*2+5,
32, dtype='float32')
_input_array[:] = 0
_output_array[:] = 0
input_array_4 = (
numpy.frombuffer(_input_array[1:-4].data, dtype='complex64')
.reshape(self.input_array.shape))
output_array_4 = (
numpy.frombuffer(_output_array[1:-4].data, dtype='complex64')
.reshape(self.output_array.shape))
input_array_16 = (
numpy.frombuffer(_input_array[4:-1].data, dtype='complex64')
.reshape(self.input_array.shape))
output_array_16 = (
numpy.frombuffer(_output_array[4:-1].data, dtype='complex64')
.reshape(self.output_array.shape))
input_array_32 = (
numpy.frombuffer(_input_array[:-5].data, dtype='complex64')
.reshape(self.input_array.shape))
output_array_32 = (
numpy.frombuffer(_output_array[:-5].data, dtype='complex64')
.reshape(self.output_array.shape))
#.........这里部分代码省略.........
示例8: test_differing_aligned_arrays_update
def test_differing_aligned_arrays_update(self):
'''Test to see if the alignment code is working as expected
'''
# Start by creating arrays that are only on various byte
# alignments (4, 16 and 32)
_input_array = n_byte_align_empty(
len(self.input_array.ravel())*2+5,
32, dtype='float32')
_output_array = n_byte_align_empty(
len(self.output_array.ravel())*2+5,
32, dtype='float32')
_input_array[:] = 0
_output_array[:] = 0
input_array_4 = (
numpy.frombuffer(_input_array[1:-4].data, dtype='complex64')
.reshape(self.input_array.shape))
output_array_4 = (
numpy.frombuffer(_output_array[1:-4].data, dtype='complex64')
.reshape(self.output_array.shape))
input_array_16 = (
numpy.frombuffer(_input_array[4:-1].data, dtype='complex64')
.reshape(self.input_array.shape))
output_array_16 = (
numpy.frombuffer(_output_array[4:-1].data, dtype='complex64')
.reshape(self.output_array.shape))
input_array_32 = (
numpy.frombuffer(_input_array[:-5].data, dtype='complex64')
.reshape(self.input_array.shape))
output_array_32 = (
numpy.frombuffer(_output_array[:-5].data, dtype='complex64')
.reshape(self.output_array.shape))
input_arrays = {4: input_array_4,
16: input_array_16,
32: input_array_32}
output_arrays = {4: output_array_4,
16: output_array_16,
32: output_array_32}
alignments = (4, 16, 32)
# Test the arrays are aligned on 4 bytes...
self.assertTrue(is_n_byte_aligned(input_arrays[4], 4))
self.assertTrue(is_n_byte_aligned(output_arrays[4], 4))
# ...and on 16...
self.assertFalse(is_n_byte_aligned(input_arrays[4], 16))
self.assertFalse(is_n_byte_aligned(output_arrays[4], 16))
self.assertTrue(is_n_byte_aligned(input_arrays[16], 16))
self.assertTrue(is_n_byte_aligned(output_arrays[16], 16))
# ...and on 32...
self.assertFalse(is_n_byte_aligned(input_arrays[16], 32))
self.assertFalse(is_n_byte_aligned(output_arrays[16], 32))
self.assertTrue(is_n_byte_aligned(input_arrays[32], 32))
self.assertTrue(is_n_byte_aligned(output_arrays[32], 32))
if len(pyfftw.pyfftw._valid_simd_alignments) > 0:
max_align = pyfftw.pyfftw._valid_simd_alignments[0]
else:
max_align = simd_alignment
for in_align in alignments:
for out_align in alignments:
expected_align = min(in_align, out_align, max_align)
fft = FFTW(input_arrays[in_align], output_arrays[out_align])
self.assertTrue(fft.input_alignment == expected_align)
self.assertTrue(fft.output_alignment == expected_align)
for update_align in alignments:
if update_align < expected_align:
# This should fail (not aligned properly)
self.assertRaisesRegex(ValueError,
'Invalid input alignment',
fft.update_arrays,
input_arrays[update_align],
output_arrays[out_align])
self.assertRaisesRegex(ValueError,
'Invalid output alignment',
fft.update_arrays,
input_arrays[in_align],
output_arrays[update_align])
else:
# This should work (and not segfault!)
fft.update_arrays(input_arrays[update_align],
output_arrays[out_align])
fft.update_arrays(input_arrays[in_align],
output_arrays[update_align])
fft.execute()
示例9: FFTWCallTest
class FFTWCallTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(FFTWCallTest, self).__init__(*args, **kwargs)
if not hasattr(self, "assertRaisesRegex"):
self.assertRaisesRegex = self.assertRaisesRegexp
def setUp(self):
self.input_array = empty_aligned((256, 512), dtype="complex128", n=16)
self.output_array = empty_aligned((256, 512), dtype="complex128", n=16)
self.fft = FFTW(self.input_array, self.output_array)
self.input_array[:] = numpy.random.randn(*self.input_array.shape) + 1j * numpy.random.randn(
*self.input_array.shape
)
def test_call(self):
"""Test a call to an instance of the class.
"""
self.input_array[:] = numpy.random.randn(*self.input_array.shape) + 1j * numpy.random.randn(
*self.input_array.shape
)
output_array = self.fft()
self.assertTrue(numpy.alltrue(output_array == self.output_array))
def test_call_with_positional_input_update(self):
"""Test the class call with a positional input update.
"""
input_array = byte_align(
(numpy.random.randn(*self.input_array.shape) + 1j * numpy.random.randn(*self.input_array.shape)), n=16
)
output_array = self.fft(byte_align(input_array.copy(), n=16)).copy()
self.fft.update_arrays(input_array, self.output_array)
self.fft.execute()
self.assertTrue(numpy.alltrue(output_array == self.output_array))
def test_call_with_keyword_input_update(self):
"""Test the class call with a keyword input update.
"""
input_array = byte_align(
numpy.random.randn(*self.input_array.shape) + 1j * numpy.random.randn(*self.input_array.shape), n=16
)
output_array = self.fft(input_array=byte_align(input_array.copy(), n=16)).copy()
self.fft.update_arrays(input_array, self.output_array)
self.fft.execute()
self.assertTrue(numpy.alltrue(output_array == self.output_array))
def test_call_with_keyword_output_update(self):
"""Test the class call with a keyword output update.
"""
output_array = byte_align(
(numpy.random.randn(*self.output_array.shape) + 1j * numpy.random.randn(*self.output_array.shape)), n=16
)
returned_output_array = self.fft(output_array=byte_align(output_array.copy(), n=16)).copy()
self.fft.update_arrays(self.input_array, output_array)
self.fft.execute()
self.assertTrue(numpy.alltrue(returned_output_array == output_array))
def test_call_with_positional_updates(self):
"""Test the class call with a positional array updates.
"""
input_array = byte_align(
(numpy.random.randn(*self.input_array.shape) + 1j * numpy.random.randn(*self.input_array.shape)), n=16
)
output_array = byte_align(
(numpy.random.randn(*self.output_array.shape) + 1j * numpy.random.randn(*self.output_array.shape)), n=16
)
returned_output_array = self.fft(
byte_align(input_array.copy(), n=16), byte_align(output_array.copy(), n=16)
).copy()
self.fft.update_arrays(input_array, output_array)
self.fft.execute()
self.assertTrue(numpy.alltrue(returned_output_array == output_array))
def test_call_with_keyword_updates(self):
"""Test the class call with a positional output update.
"""
input_array = byte_align(
#.........这里部分代码省略.........