def __call__(self, F, extrap=True): """Evaluate the integral Parameters ---------- F : float, array_like input function, internally padded symmetrically to length N with power-law extrapolations or zeros extrap : bool or 2-tuple of bools, optional whether to extrapolate F with power laws or to just pad with zeros for the internal paddings. In case of a tuple, the two elements are for the left and right sides of F respectively Returns ------- y : float, array_like log-evenly spaced output argument G : float, array_like output function, with internal paddings discarded """ if len(F) != len(self.x): raise ValueError( "lengths of input function and argument must match") f = self.prefac * self.x**(-self.q) * F # pad with power-law extrapolations or zeros if isinstance(extrap, bool): extrap_l = extrap_r = extrap elif isinstance(extrap, tuple) and len(extrap) == 2 and \ all(isinstance(e, bool) for e in extrap): extrap_l, extrap_r = extrap else: raise TypeError( "extrap must be either a bool or a tuple of two bools") Npad = self.N - len(self.x) if extrap_l: fpad_l = f[0] * (f[1] / f[0])**numpy.arange(-(Npad // 2), 0) else: fpad_l = numpy.zeros(Npad // 2) if extrap_r: fpad_r = f[-1] * (f[-1] / f[-2])**numpy.arange( 1, Npad - Npad // 2 + 1) else: fpad_r = numpy.zeros(Npad - Npad // 2) f = numpy.concatenate((fpad_l, f, fpad_r)) # convolution f = rfft(f) # f(x_n) -> f_m g = f * self._u # f_m -> g_m g = hfft(g, self.N) / self.N # g_m -> g(y_n) # discard paddings g = g[Npad - Npad // 2:self.N - Npad // 2] G = self.postfac * self.y**(-self.q) * g return self.y, G
def ifourier(f_w, dw, n=None, hermitian=True, output_t=False): if hermitian: len_w = len(f_w) # length of the one-sided array len_w2 = np.max([2 * (len_w - 1), n]) # length of the two-sided arrays # if n > len_w2, f_w is zero-padded iFf_t = dw / (2. * np.pi) * fft.fftshift(fft.hfft(f_w, len_w2)) else: len_w2 = np.max([len(f_w), n]) shift = -len(f_w) / 2 iFf_t = dw / (2. * np.pi) * fft.fftshift( fft.fft(f_w, len_w2) * np.exp(-2j * np.pi / len_w2 * shift * np.arange(len_w2))) if output_t: #return (t2w(dt * np.arange(len_t)), Ff_w) return (2. * np.pi * fft.fftshift(fft.fftfreq(len_w2, dw)), iFf_t) else: return iFf_t
def ifourier(f_w, dw, n=None, hermitian=True, output_t = False): if hermitian: len_w = len(f_w) # length of the one-sided array len_w2 = np.max([2 * (len_w - 1), n]) # length of the two-sided arrays # if n > len_w2, f_w is zero-padded iFf_t = dw / (2. * np.pi) * fft.fftshift(fft.hfft(f_w, len_w2)) else: len_w2 = np.max([len(f_w), n]) shift = -len(f_w) / 2 iFf_t = dw / (2. * np.pi) * fft.fftshift( fft.fft(f_w, len_w2) * np.exp(-2j * np.pi / len_w2 * shift * np.arange(len_w2)) ) if output_t: #return (t2w(dt * np.arange(len_t)), Ff_w) return (2. * np.pi * fft.fftshift(fft.fftfreq(len_w2, dw)), iFf_t) else: return iFf_t
def psd(signal, sr=1, N=2048): ''' numpy.fft.fft: When the input a is a time-domain signal and A = fft(a): . np.abs(A) is its amplitude spectrum; . np.abs(A)**2 is its power spectrum; . np.angle(A) is the phase spectrum. ''' f = signal ft = fft.hfft(f, N) ft_shifted = fft.fftshift(ft) aft = np.abs(ft)**2 aft_shifted = (abs(ft_shifted)**2) # freq = np.fft.fftfreq(N, d=sr) freqs = np.concatenate([freq[int(len(freq) / 2):], [0]]) freqs = np.concatenate([freqs, freq[1:int(len(freq) / 2)]]) # return freqs, aft_shifted
def psd(signal, sr=1): ''' numpy.fft.fft: When the input a is a time-domain signal and A = fft(a): . np.abs(A) is its amplitude spectrum; . np.abs(A)**2 is its power spectrum; . np.angle(A) is the phase spectrum. ''' f = signal N = 2048 ft = fft.hfft(f, N) ft_shifted = fft.fftshift(ft) ft_shifted_real = np.real(fft.fftshift(ft)) ft_shifted_imag = np.imag(fft.fftshift(ft)) aft = np.abs(ft)**2 aft_shifted = abs(ft_shifted)**2 # freq = np.fft.fftfreq(N, d=1 / sr) freqs = np.concatenate([freq[int(len(freq) / 2):], [0]]) freqs = np.concatenate([freqs, freq[1:int(len(freq) / 2)]]) # return freqs, ft_shifted #, aft_shifted[int(N/2)+1:]#, [int(N/2)+1:]
def wft(signal, sr=1, N=2048): ''' numpy.fft.fft: When the input a is a time-domain signal and A = fft(a): . np.abs(A) is its amplitude spectrum; . np.abs(A)**2 is its power spectrum; . np.angle(A) is the phase spectrum. ''' f = signal ft = fft.hfft(f, N) ft_shifted = fft.fftshift(ft) aft = np.abs(ft)**2 aft_shifted = (abs(ft_shifted)**2) # freq = np.fft.fftfreq(N, d=sr) freqs = np.concatenate([freq[int(len(freq) / 2):], [0]]) freqs = np.concatenate([freqs, freq[1:int(len(freq) / 2)]]) # #for i in range(len(freq)): # if abs(i-f_final)<=.01: # break #[:i] return freq[1:int(N / 2)], ft[1:int(N / 2)]
def test_hfft_nkwarg(): assert_eq(hfft(darr, 5), npfft.hfft(nparr, 5)) assert_eq(hfft(darr, 13), npfft.hfft(nparr, 13)) assert_eq(hfft(darr2, 5, axis=0), npfft.hfft(nparr, 5, axis=0)) assert_eq(hfft(darr2, 13, axis=0), npfft.hfft(nparr, 13, axis=0)) assert_eq(hfft(darr2, 12, axis=0), npfft.hfft(nparr, 12, axis=0))
def test_hfft(): assert_eq(hfft(darr), npfft.hfft(nparr)) assert_eq(hfft(darr2, axis=0), npfft.hfft(nparr, axis=0))
def phys(q): return hfft(fft(q, axis=-2)) # Nx must be even
def test_hfft_nkwarg(): assert eq(hfft(darr, 5), npfft.hfft(nparr, 5)) assert eq(hfft(darr, 13), npfft.hfft(nparr, 13)) assert eq(hfft(darr2, 5, axis=0), npfft.hfft(nparr, 5, axis=0)) assert eq(hfft(darr2, 13, axis=0), npfft.hfft(nparr, 13, axis=0)) assert eq(hfft(darr2, 12, axis=0), npfft.hfft(nparr, 12, axis=0))
def test_hfft(): assert eq(hfft(darr), npfft.hfft(nparr)) assert eq(hfft(darr2, axis=0), npfft.hfft(nparr, axis=0))
def phys(q): return ft.hfft(ft.fft(q, axis=-2)) # Nx must be even @staticmethod