Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/numpy/fft/helper.py
7763 views
"""1Discrete Fourier Transforms - helper.py23"""4from numpy.core import integer, empty, arange, asarray, roll5from numpy.core.overrides import array_function_dispatch, set_module67# Created by Pearu Peterson, September 200289__all__ = ['fftshift', 'ifftshift', 'fftfreq', 'rfftfreq']1011integer_types = (int, integer)121314def _fftshift_dispatcher(x, axes=None):15return (x,)161718@array_function_dispatch(_fftshift_dispatcher, module='numpy.fft')19def fftshift(x, axes=None):20"""21Shift the zero-frequency component to the center of the spectrum.2223This function swaps half-spaces for all axes listed (defaults to all).24Note that ``y[0]`` is the Nyquist component only if ``len(x)`` is even.2526Parameters27----------28x : array_like29Input array.30axes : int or shape tuple, optional31Axes over which to shift. Default is None, which shifts all axes.3233Returns34-------35y : ndarray36The shifted array.3738See Also39--------40ifftshift : The inverse of `fftshift`.4142Examples43--------44>>> freqs = np.fft.fftfreq(10, 0.1)45>>> freqs46array([ 0., 1., 2., ..., -3., -2., -1.])47>>> np.fft.fftshift(freqs)48array([-5., -4., -3., -2., -1., 0., 1., 2., 3., 4.])4950Shift the zero-frequency component only along the second axis:5152>>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3)53>>> freqs54array([[ 0., 1., 2.],55[ 3., 4., -4.],56[-3., -2., -1.]])57>>> np.fft.fftshift(freqs, axes=(1,))58array([[ 2., 0., 1.],59[-4., 3., 4.],60[-1., -3., -2.]])6162"""63x = asarray(x)64if axes is None:65axes = tuple(range(x.ndim))66shift = [dim // 2 for dim in x.shape]67elif isinstance(axes, integer_types):68shift = x.shape[axes] // 269else:70shift = [x.shape[ax] // 2 for ax in axes]7172return roll(x, shift, axes)737475@array_function_dispatch(_fftshift_dispatcher, module='numpy.fft')76def ifftshift(x, axes=None):77"""78The inverse of `fftshift`. Although identical for even-length `x`, the79functions differ by one sample for odd-length `x`.8081Parameters82----------83x : array_like84Input array.85axes : int or shape tuple, optional86Axes over which to calculate. Defaults to None, which shifts all axes.8788Returns89-------90y : ndarray91The shifted array.9293See Also94--------95fftshift : Shift zero-frequency component to the center of the spectrum.9697Examples98--------99>>> freqs = np.fft.fftfreq(9, d=1./9).reshape(3, 3)100>>> freqs101array([[ 0., 1., 2.],102[ 3., 4., -4.],103[-3., -2., -1.]])104>>> np.fft.ifftshift(np.fft.fftshift(freqs))105array([[ 0., 1., 2.],106[ 3., 4., -4.],107[-3., -2., -1.]])108109"""110x = asarray(x)111if axes is None:112axes = tuple(range(x.ndim))113shift = [-(dim // 2) for dim in x.shape]114elif isinstance(axes, integer_types):115shift = -(x.shape[axes] // 2)116else:117shift = [-(x.shape[ax] // 2) for ax in axes]118119return roll(x, shift, axes)120121122@set_module('numpy.fft')123def fftfreq(n, d=1.0):124"""125Return the Discrete Fourier Transform sample frequencies.126127The returned float array `f` contains the frequency bin centers in cycles128per unit of the sample spacing (with zero at the start). For instance, if129the sample spacing is in seconds, then the frequency unit is cycles/second.130131Given a window length `n` and a sample spacing `d`::132133f = [0, 1, ..., n/2-1, -n/2, ..., -1] / (d*n) if n is even134f = [0, 1, ..., (n-1)/2, -(n-1)/2, ..., -1] / (d*n) if n is odd135136Parameters137----------138n : int139Window length.140d : scalar, optional141Sample spacing (inverse of the sampling rate). Defaults to 1.142143Returns144-------145f : ndarray146Array of length `n` containing the sample frequencies.147148Examples149--------150>>> signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5], dtype=float)151>>> fourier = np.fft.fft(signal)152>>> n = signal.size153>>> timestep = 0.1154>>> freq = np.fft.fftfreq(n, d=timestep)155>>> freq156array([ 0. , 1.25, 2.5 , ..., -3.75, -2.5 , -1.25])157158"""159if not isinstance(n, integer_types):160raise ValueError("n should be an integer")161val = 1.0 / (n * d)162results = empty(n, int)163N = (n-1)//2 + 1164p1 = arange(0, N, dtype=int)165results[:N] = p1166p2 = arange(-(n//2), 0, dtype=int)167results[N:] = p2168return results * val169170171@set_module('numpy.fft')172def rfftfreq(n, d=1.0):173"""174Return the Discrete Fourier Transform sample frequencies175(for usage with rfft, irfft).176177The returned float array `f` contains the frequency bin centers in cycles178per unit of the sample spacing (with zero at the start). For instance, if179the sample spacing is in seconds, then the frequency unit is cycles/second.180181Given a window length `n` and a sample spacing `d`::182183f = [0, 1, ..., n/2-1, n/2] / (d*n) if n is even184f = [0, 1, ..., (n-1)/2-1, (n-1)/2] / (d*n) if n is odd185186Unlike `fftfreq` (but like `scipy.fftpack.rfftfreq`)187the Nyquist frequency component is considered to be positive.188189Parameters190----------191n : int192Window length.193d : scalar, optional194Sample spacing (inverse of the sampling rate). Defaults to 1.195196Returns197-------198f : ndarray199Array of length ``n//2 + 1`` containing the sample frequencies.200201Examples202--------203>>> signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5, -3, 4], dtype=float)204>>> fourier = np.fft.rfft(signal)205>>> n = signal.size206>>> sample_rate = 100207>>> freq = np.fft.fftfreq(n, d=1./sample_rate)208>>> freq209array([ 0., 10., 20., ..., -30., -20., -10.])210>>> freq = np.fft.rfftfreq(n, d=1./sample_rate)211>>> freq212array([ 0., 10., 20., 30., 40., 50.])213214"""215if not isinstance(n, integer_types):216raise ValueError("n should be an integer")217val = 1.0/(n*d)218N = n//2 + 1219results = arange(0, N, dtype=int)220return results * val221222223