Python numpy.fft() Examples
The following are 30
code examples of numpy.fft().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: xrft.py From xrft with MIT License | 6 votes |
def _freq(N, delta_x, real, shift): # calculate frequencies from coordinates # coordinates are always loaded eagerly, so we use numpy if real is None: fftfreq = [np.fft.fftfreq]*len(N) else: # Discard negative frequencies from transform along last axis to be # consistent with np.fft.rfftn fftfreq = [np.fft.fftfreq]*(len(N)-1) fftfreq.append(np.fft.rfftfreq) k = [fftfreq(Nx, dx) for (fftfreq, Nx, dx) in zip(fftfreq, N, delta_x)] if shift: k = [np.fft.fftshift(l) for l in k] return k
Example #2
Source File: test_fft.py From afnumpy with BSD 2-Clause "Simplified" License | 6 votes |
def test_ifft(): # Real to complex inverse fft not implemented in arrayfire # b = numpy.random.random((3,3)) # a = afnumpy.array(b) # fassert(afnumpy.fft.ifft(a), numpy.fft.ifft(b)) # b = numpy.random.random((3,2)) # a = afnumpy.array(b) # fassert(afnumpy.fft.ifft(a), numpy.fft.ifft(b)) # b = numpy.random.random((5,3,2)) # a = afnumpy.array(b) # fassert(afnumpy.fft.ifft(a), numpy.fft.ifft(b)) b = numpy.random.random((5,3,2))+numpy.random.random((5,3,2))*1.0j # b = numpy.ones((3,3))+numpy.zeros((3,3))*1.0j a = afnumpy.array(b) fassert(afnumpy.fft.ifft(a), numpy.fft.ifft(b))
Example #3
Source File: test_fft.py From afnumpy with BSD 2-Clause "Simplified" License | 6 votes |
def test_fft(): b = numpy.random.random((3,3)) a = afnumpy.array(b) fassert(afnumpy.fft.fft(a), numpy.fft.fft(b)) b = numpy.random.random((3,2)) a = afnumpy.array(b) fassert(afnumpy.fft.fft(a), numpy.fft.fft(b)) b = numpy.random.random((5,3,2)) a = afnumpy.array(b) fassert(afnumpy.fft.fft(a), numpy.fft.fft(b)) b = numpy.random.random((5,3,2))+numpy.random.random((5,3,2))*1.0j a = afnumpy.array(b) fassert(afnumpy.fft.fft(a), numpy.fft.fft(b))
Example #4
Source File: test_fft.py From afnumpy with BSD 2-Clause "Simplified" License | 6 votes |
def test_fft2(): b = numpy.random.random((3,3)) a = afnumpy.array(b) fassert(afnumpy.fft.fft2(a), numpy.fft.fft2(b)) b = numpy.random.random((3,2)) a = afnumpy.array(b) fassert(afnumpy.fft.fft2(a), numpy.fft.fft2(b)) b = numpy.random.random((5,3,2)) a = afnumpy.array(b) fassert(afnumpy.fft.fft2(a), numpy.fft.fft2(b)) b = numpy.random.random((5,3,2))+numpy.random.random((5,3,2))*1.0j a = afnumpy.array(b) fassert(afnumpy.fft.fft2(a), numpy.fft.fft2(b))
Example #5
Source File: test_fft.py From afnumpy with BSD 2-Clause "Simplified" License | 6 votes |
def test_ifft2(): # Real to complex inverse fft not implemented in arrayfire # b = numpy.random.random((3,3)) # a = afnumpy.array(b) # fassert(afnumpy.fft.ifft2(a), numpy.fft.ifft2(b)) # b = numpy.random.random((3,2)) # a = afnumpy.array(b) # fassert(afnumpy.fft.ifft2(a), numpy.fft.ifft2(b)) # b = numpy.random.random((5,3,2)) # a = afnumpy.array(b) # fassert(afnumpy.fft.ifft2(a), numpy.fft.ifft2(b)) b = numpy.random.random((5,3,2))+numpy.random.random((5,3,2))*1.0j # b = numpy.ones((3,3))+numpy.zeros((3,3))*1.0j a = afnumpy.array(b) fassert(afnumpy.fft.ifft2(a), numpy.fft.ifft2(b))
Example #6
Source File: fourier.py From D-VAE with MIT License | 6 votes |
def perform(self, node, inp, out): frames, n, axis = inp spectrogram, buf = out if self.inverse: fft_fn = numpy.fft.ifft else: fft_fn = numpy.fft.fft fft = fft_fn(frames, int(n), int(axis)) if self.half: M, N = fft.shape if axis == 0: if (M % 2): raise ValueError( 'halfFFT on odd-length vectors is undefined') spectrogram[0] = fft[0:M / 2, :] elif axis == 1: if (N % 2): raise ValueError( 'halfFFT on odd-length vectors is undefined') spectrogram[0] = fft[:, 0:N / 2] else: raise NotImplementedError() else: spectrogram[0] = fft
Example #7
Source File: fourier.py From D-VAE with MIT License | 6 votes |
def make_node(self, frames, n, axis): """ Compute an n-point fft of frames along given axis. """ _frames = tensor.as_tensor(frames, ndim=2) _n = tensor.as_tensor(n, ndim=0) _axis = tensor.as_tensor(axis, ndim=0) if self.half and _frames.type.dtype.startswith('complex'): raise TypeError('Argument to HalfFFT must not be complex', frames) spectrogram = tensor.zmatrix() buf = generic() # The `buf` output is present for future work # when we call FFTW directly and re-use the 'plan' that FFTW creates. # In that case, buf would store a CObject encapsulating the plan. rval = Apply(self, [_frames, _n, _axis], [spectrogram, buf]) return rval
Example #8
Source File: test_basic.py From GraphicDesignPatternByPython with MIT License | 6 votes |
def test_shape_axes_argument(self): small_x = [[1,2,3],[4,5,6],[7,8,9]] large_x1 = array([[1,2,3,0], [4,5,6,0], [7,8,9,0], [0,0,0,0]]) # Disable tests with shape and axes of different lengths # y = fftn(small_x,shape=(4,4),axes=(-1,)) # for i in range(4): # assert_array_almost_equal (y[i],fft(large_x1[i])) # y = fftn(small_x,shape=(4,4),axes=(-2,)) # for i in range(4): # assert_array_almost_equal (y[:,i],fft(large_x1[:,i])) y = fftn(small_x,shape=(4,4),axes=(-2,-1)) assert_array_almost_equal(y,fftn(large_x1)) y = fftn(small_x,shape=(4,4),axes=(-1,-2)) assert_array_almost_equal(y,swapaxes( fftn(swapaxes(large_x1,-1,-2)),-1,-2))
Example #9
Source File: test_basic.py From Computable with MIT License | 6 votes |
def test_size_accuracy(self): # Sanity check for the accuracy for prime and non-prime sized inputs if self.rdt == np.float32: rtol = 1e-5 elif self.rdt == np.float64: rtol = 1e-10 for size in LARGE_COMPOSITE_SIZES + LARGE_PRIME_SIZES: np.random.seed(1234) x = np.random.rand(size).astype(self.rdt) y = ifft(fft(x)) self.assertTrue(np.linalg.norm(x - y) < rtol*np.linalg.norm(x), (size, self.rdt)) y = fft(ifft(x)) self.assertTrue(np.linalg.norm(x - y) < rtol*np.linalg.norm(x), (size, self.rdt)) x = (x + 1j*np.random.rand(size)).astype(self.cdt) y = ifft(fft(x)) self.assertTrue(np.linalg.norm(x - y) < rtol*np.linalg.norm(x), (size, self.rdt)) y = fft(ifft(x)) self.assertTrue(np.linalg.norm(x - y) < rtol*np.linalg.norm(x), (size, self.rdt))
Example #10
Source File: test_basic.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_djbfft(self): from numpy.fft import fft as numpy_fft for i in range(2,14): n = 2**i x = list(range(n)) y2 = numpy_fft(x) y1 = zeros((n,),dtype=double) y1[0] = y2[0].real y1[-1] = y2[n//2].real for k in range(1, n//2): y1[2*k-1] = y2[k].real y1[2*k] = y2[k].imag y = fftpack.drfft(x) assert_array_almost_equal(y,y1)
Example #11
Source File: test_basic.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_random_real(self): for size in [1,51,111,100,200,64,128,256,1024]: x = random([size]).astype(self.rdt) y1 = ifft(fft(x)) y2 = fft(ifft(x)) assert_equal(y1.dtype, self.cdt) assert_equal(y2.dtype, self.cdt) assert_array_almost_equal(y1, x) assert_array_almost_equal(y2, x)
Example #12
Source File: test_basic.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_random_complex(self): for size in [1,51,111,100,200,64,128,256,1024]: x = random([size]).astype(self.cdt) x = random([size]).astype(self.cdt) + 1j*x y1 = ifft(fft(x)) y2 = fft(ifft(x)) assert_equal(y1.dtype, self.cdt) assert_equal(y2.dtype, self.cdt) assert_array_almost_equal(y1, x) assert_array_almost_equal(y2, x)
Example #13
Source File: test_basic.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_djbfft(self): for i in range(2,14): n = 2**i x = list(range(n)) y = fftpack.zfft(x,direction=-1) y2 = numpy.fft.ifft(x) assert_array_almost_equal(y,y2) y = fftpack.zrfft(x,direction=-1) assert_array_almost_equal(y,y2)
Example #14
Source File: test_basic.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_djbfft(self): from numpy.fft import ifft as numpy_ifft for i in range(2,14): n = 2**i x = list(range(n)) x1 = zeros((n,),dtype=cdouble) x1[0] = x[0] for k in range(1, n//2): x1[k] = x[2*k-1]+1j*x[2*k] x1[n-k] = x[2*k-1]-1j*x[2*k] x1[n//2] = x[-1] y1 = numpy_ifft(x1) y = fftpack.drfft(x,direction=-1) assert_array_almost_equal(y,y1)
Example #15
Source File: test_basic.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_shape_axes_argument2(self): # Change shape of the last axis x = numpy.random.random((10, 5, 3, 7)) y = fftn(x, axes=(-1,), shape=(8,)) assert_array_almost_equal(y, fft(x, axis=-1, n=8)) # Change shape of an arbitrary axis which is not the last one x = numpy.random.random((10, 5, 3, 7)) y = fftn(x, axes=(-2,), shape=(8,)) assert_array_almost_equal(y, fft(x, axis=-2, n=8)) # Change shape of axes: cf #244, where shape and axes were mixed up x = numpy.random.random((4,4,2)) y = fftn(x, axes=(-3,-2), shape=(8,8)) assert_array_almost_equal(y, numpy.fft.fftn(x, axes=(-3, -2), s=(8, 8)))
Example #16
Source File: test_basic.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_fft(self): overwritable = (np.complex128, np.complex64) for dtype in self.dtypes: self._check_1d(fft, dtype, (16,), -1, overwritable) self._check_1d(fft, dtype, (16, 2), 0, overwritable) self._check_1d(fft, dtype, (2, 16), 1, overwritable)
Example #17
Source File: test_basic.py From Computable with MIT License | 5 votes |
def test_djbfft(self): for i in range(2,14): n = 2**i x = list(range(n)) y = fftpack.zfft(x,direction=-1) y2 = numpy.fft.ifft(x) assert_array_almost_equal(y,y2) y = fftpack.zrfft(x,direction=-1) assert_array_almost_equal(y,y2)
Example #18
Source File: test_basic.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_real(self): if np.dtype(np.longdouble).itemsize == np.dtype(np.double).itemsize: # longdouble == double; so fft is supported return x = np.random.randn(10).astype(np.longcomplex) for f in [fft, ifft]: try: f(x) raise AssertionError("Type %r not supported but does not fail" % np.longcomplex) except ValueError: pass
Example #19
Source File: test_fft.py From chainer with MIT License | 5 votes |
def forward_expected(self, inputs): rx, ix = inputs expected = getattr(numpy.fft, self.method)(rx + ix * 1j) return ( expected.real.astype(self.dtype), expected.imag.astype(self.dtype) )
Example #20
Source File: test_public_api.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_numpy_fft(): bad_results = check_dir(np.fft) assert bad_results == {}
Example #21
Source File: least_squares_first_peaks_2.py From Automated_Music_Transcription with MIT License | 5 votes |
def STFT(self, x, samplingFreq, framesz, hop): """ Computes STFT for a given sound wave using Hanning window. """ framesamp = int(framesz * samplingFreq) print 'FRAMESAMP: ' + str(framesamp) hopsamp = int(hop * samplingFreq) print 'HOP SAMP: ' + str(hopsamp) # Modification: using Hanning window instead of Hamming - by Pertusa w = signal.hann(framesamp) X = numpy.array([numpy.fft.fft(w * x[i:i + framesamp]) for i in range(0, len(x) - framesamp, hopsamp)]) return X
Example #22
Source File: test_basic.py From Computable with MIT License | 5 votes |
def test_fft(self): overwritable = (np.complex128, np.complex64) for dtype in self.dtypes: self._check_1d(fft, dtype, (16,), -1, overwritable) self._check_1d(fft, dtype, (16, 2), 0, overwritable) self._check_1d(fft, dtype, (2, 16), 1, overwritable)
Example #23
Source File: test_basic.py From Computable with MIT License | 5 votes |
def test_real(self): if np.dtype(np.longdouble).itemsize == np.dtype(np.double).itemsize: # longdouble == double; so fft is supported return x = np.random.randn(10).astype(np.longcomplex) for f in [fft, ifft]: try: f(x) raise AssertionError("Type %r not supported but does not fail" % np.longcomplex) except ValueError: pass
Example #24
Source File: test_basic.py From Computable with MIT License | 5 votes |
def test_complex(self): if np.dtype(np.longcomplex).itemsize == np.dtype(np.complex).itemsize: # longdouble == double; so fft is supported return x = np.random.randn(10).astype(np.longdouble) + \ 1j * np.random.randn(10).astype(np.longdouble) for f in [fft, ifft]: try: f(x) raise AssertionError("Type %r not supported but does not fail" % np.longcomplex) except ValueError: pass
Example #25
Source File: test_basic.py From Computable with MIT License | 5 votes |
def test_shape_axes_argument2(self): # Change shape of the last axis x = numpy.random.random((10, 5, 3, 7)) y = fftn(x, axes=(-1,), shape=(8,)) assert_array_almost_equal(y, fft(x, axis=-1, n=8)) # Change shape of an arbitrary axis which is not the last one x = numpy.random.random((10, 5, 3, 7)) y = fftn(x, axes=(-2,), shape=(8,)) assert_array_almost_equal(y, fft(x, axis=-2, n=8)) # Change shape of axes: cf #244, where shape and axes were mixed up x = numpy.random.random((4,4,2)) y = fftn(x, axes=(-3,-2), shape=(8,8)) assert_array_almost_equal(y, numpy.fft.fftn(x, axes=(-3, -2), s=(8, 8)))
Example #26
Source File: test_basic.py From Computable with MIT License | 5 votes |
def test_regression_244(self): """fft returns wrong result with axes parameter.""" # fftn (and hence fft2) used to break when both axes and shape were # used x = numpy.ones((4,4,2)) y = fft2(x, shape=(8,8), axes=(-3,-2)) y_r = numpy.fft.fftn(x, s=(8, 8), axes=(-3, -2)) assert_array_almost_equal(y, y_r)
Example #27
Source File: test_basic.py From Computable with MIT License | 5 votes |
def test_djbfft(self): from numpy.fft import ifft as numpy_ifft for i in range(2,14): n = 2**i x = list(range(n)) x1 = zeros((n,),dtype=cdouble) x1[0] = x[0] for k in range(1, n//2): x1[k] = x[2*k-1]+1j*x[2*k] x1[n-k] = x[2*k-1]-1j*x[2*k] x1[n//2] = x[-1] y1 = numpy_ifft(x1) y = fftpack.drfft(x,direction=-1) assert_array_almost_equal(y,y1)
Example #28
Source File: test_basic.py From Computable with MIT License | 5 votes |
def test_djbfft(self): from numpy.fft import fft as numpy_fft for i in range(2,14): n = 2**i x = list(range(n)) y2 = numpy_fft(x) y1 = zeros((n,),dtype=double) y1[0] = y2[0].real y1[-1] = y2[n//2].real for k in range(1, n//2): y1[2*k-1] = y2[k].real y1[2*k] = y2[k].imag y = fftpack.drfft(x) assert_array_almost_equal(y,y1)
Example #29
Source File: test_basic.py From Computable with MIT License | 5 votes |
def test_random_real(self): for size in [1,51,111,100,200,64,128,256,1024]: x = random([size]).astype(self.rdt) y1 = ifft(fft(x)) y2 = fft(ifft(x)) self.assertTrue(y1.dtype == self.cdt, "Output dtype is %s, expected %s" % (y1.dtype, self.cdt)) self.assertTrue(y2.dtype == self.cdt, "Output dtype is %s, expected %s" % (y2.dtype, self.cdt)) assert_array_almost_equal(y1, x) assert_array_almost_equal(y2, x)
Example #30
Source File: test_basic.py From Computable with MIT License | 5 votes |
def _test_n_argument_complex(self): x1 = np.array([1,2,3,4+1j], dtype=self.cdt) x2 = np.array([1,2,3,4+1j], dtype=self.cdt) y = fft([x1,x2],n=4) self.assertTrue(y.dtype == self.cdt, "Output dtype is %s, expected %s" % (y.dtype, self.cdt)) assert_equal(y.shape,(2,4)) assert_array_almost_equal(y[0],direct_dft(x1)) assert_array_almost_equal(y[1],direct_dft(x2))