我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用scipy.fftpack.ifftn()。
def _make_noise_input(self, init): """ Creates an initial input (generated) image. """ # specify dimensions and create grid in Fourier domain dims = tuple(self.net.blobs["data"].data.shape[2:]) + \ (self.net.blobs["data"].data.shape[1],) grid = np.mgrid[0:dims[0], 0:dims[1]] # create frequency representation for pink noise Sf = (grid[0] - (dims[0] - 1) / 2.0) ** 2 + \ (grid[1] - (dims[1] - 1) / 2.0) ** 2 Sf[np.where(Sf == 0)] = 1 Sf = np.sqrt(Sf) Sf = np.dstack((Sf ** int(init),) * dims[2]) # apply ifft to create pink noise and normalize ifft_kernel = np.cos(2 * np.pi * np.random.randn(*dims)) + \ 1j * np.sin(2 * np.pi * np.random.randn(*dims)) img_noise = np.abs(ifftn(Sf * ifft_kernel)) img_noise -= img_noise.min() img_noise /= img_noise.max() # preprocess the pink noise image x0 = self.transformer.preprocess("data", img_noise) return x0
def ifftn(B, shape=None): if shape != None: B = _checkffttype(B) B = procrustes(B, target=shape, side='after', padval=0) return _anfft.ifftn(B, measure=True)
def fftconvolve(in1, in2, mode="full", axis=None): """ Convolve two N-dimensional arrays using FFT. See convolve. This is a fix of scipy.signal.fftconvolve, adding an axis argument and importing locally the stuff only needed for this function """ s1 = np.array(in1.shape) s2 = np.array(in2.shape) complex_result = (np.issubdtype(in1.dtype, np.complex) or np.issubdtype(in2.dtype, np.complex)) if axis is None: size = s1 + s2 - 1 fslice = tuple([slice(0, int(sz)) for sz in size]) else: equal_shapes = s1 == s2 # allow equal_shapes[axis] to be False equal_shapes[axis] = True assert equal_shapes.all(), 'Shape mismatch on non-convolving axes' size = s1[axis] + s2[axis] - 1 fslice = [slice(l) for l in s1] fslice[axis] = slice(0, int(size)) fslice = tuple(fslice) # Always use 2**n-sized FFT fsize = 2 ** int(np.ceil(np.log2(size))) if axis is None: IN1 = fftpack.fftn(in1, fsize) IN1 *= fftpack.fftn(in2, fsize) ret = fftpack.ifftn(IN1)[fslice].copy() else: IN1 = fftpack.fft(in1, fsize, axis=axis) IN1 *= fftpack.fft(in2, fsize, axis=axis) ret = fftpack.ifft(IN1, axis=axis)[fslice].copy() del IN1 if not complex_result: ret = ret.real if mode == "full": return ret elif mode == "same": if np.product(s1, axis=0) > np.product(s2, axis=0): osize = s1 else: osize = s2 return signaltools._centered(ret, osize) elif mode == "valid": return signaltools._centered(ret, abs(s2 - s1) + 1)
def wfc_r(self, ispin=1, ikpt=1, iband=1, gvec=None, ngrid=None, norm=False, gamma=False): ''' Obtain the pseudo-wavefunction of the specified KS states in real space by performing FT transform on the reciprocal space planewave coefficients. The 3D FT grid size is determined by ngrid, which defaults to self._ngrid if not given. Gvectors of the KS states is used to put 1D planewave coefficients back to 3D grid. ''' self.checkIndex(ispin, ikpt, iband) if ngrid is None: ngrid = self._ngrid.copy() else: ngrid = np.array(ngrid, dtype=int) assert ngrid.shape == (3,) assert np.alltrue(ngrid >= self._ngrid), \ "Minium FT grid size: (%d, %d, %d)" % \ (self._ngrid[0], self._ngrid[1], self._ngrid[2]) if gvec is None: gvec = self.gvectors(ikpt, gamma) if gamma: phi_k = np.zeros((ngrid[0], ngrid[1], ngrid[2]/2 + 1), dtype=np.complex128) else: phi_k = np.zeros(ngrid, dtype=np.complex128) gvec %= ngrid[np.newaxis,:] phi_k[gvec[:,0], gvec[:,1], gvec[:,2]] = self.readBandCoeff(ispin, ikpt, iband, norm) if gamma: # add some components that are excluded and perform c2r FFT for ii in range(ngrid[0]): for jj in range(ngrid[1]): fx = ii if ii < ngrid[0] / 2 + 1 else ii - ngrid[0] fy = ii if ii < ngrid[1] / 2 + 1 else ii - ngrid[1] if (fy > 0) or (fy == 0 and fx >= 0): continue phi_k[ii,jj,0] = phi_k[-ii,-jj,0].conjugate() phi_k /= np.sqrt(2.) phi_k[0,0,0] *= np.sqrt(2.) return np.fft.irfftn(phi_k, s=ngrid) else: # perform complex2complex FFT return ifftn(phi_k)
def _cwt(X, Ws, mode="same", decim=1, use_fft=True): """Compute cwt with fft based convolutions or temporal convolutions. Return a generator over signals. """ if mode not in ['same', 'valid', 'full']: raise ValueError("`mode` must be 'same', 'valid' or 'full', " "got %s instead." % mode) if mode == 'full' and (not use_fft): # XXX JRK: full wavelet decomposition needs to be implemented raise ValueError('`full` decomposition with convolution is currently' + ' not supported.') decim = _check_decim(decim) X = np.asarray(X) # Precompute wavelets for given frequency range to save time n_signals, n_times = X.shape n_times_out = X[:, decim].shape[1] n_freqs = len(Ws) Ws_max_size = max(W.size for W in Ws) size = n_times + Ws_max_size - 1 # Always use 2**n-sized FFT fsize = 2 ** int(np.ceil(np.log2(size))) # precompute FFTs of Ws if use_fft: fft_Ws = np.empty((n_freqs, fsize), dtype=np.complex128) for i, W in enumerate(Ws): if len(W) > n_times: raise ValueError('Wavelet is too long for such a short signal. ' 'Reduce the number of cycles.') if use_fft: fft_Ws[i] = fftn(W, [fsize]) # Make generator looping across signals tfr = np.zeros((n_freqs, n_times_out), dtype=np.complex128) for x in X: if use_fft: fft_x = fftn(x, [fsize]) # Loop across wavelets for ii, W in enumerate(Ws): if use_fft: ret = ifftn(fft_x * fft_Ws[ii])[:n_times + W.size - 1] else: ret = np.convolve(x, W, mode=mode) # Center and decimate decomposition if mode == "valid": sz = abs(W.size - n_times) + 1 offset = (n_times - sz) / 2 this_slice = slice(offset // decim.step, (offset + sz) // decim.step) if use_fft: ret = _centered(ret, sz) tfr[ii, this_slice] = ret[decim] else: if use_fft: ret = _centered(ret, n_times) tfr[ii, :] = ret[decim] yield tfr