Chris@87
|
1 """
|
Chris@87
|
2 Discrete Fourier Transform (:mod:`numpy.fft`)
|
Chris@87
|
3 =============================================
|
Chris@87
|
4
|
Chris@87
|
5 .. currentmodule:: numpy.fft
|
Chris@87
|
6
|
Chris@87
|
7 Standard FFTs
|
Chris@87
|
8 -------------
|
Chris@87
|
9
|
Chris@87
|
10 .. autosummary::
|
Chris@87
|
11 :toctree: generated/
|
Chris@87
|
12
|
Chris@87
|
13 fft Discrete Fourier transform.
|
Chris@87
|
14 ifft Inverse discrete Fourier transform.
|
Chris@87
|
15 fft2 Discrete Fourier transform in two dimensions.
|
Chris@87
|
16 ifft2 Inverse discrete Fourier transform in two dimensions.
|
Chris@87
|
17 fftn Discrete Fourier transform in N-dimensions.
|
Chris@87
|
18 ifftn Inverse discrete Fourier transform in N dimensions.
|
Chris@87
|
19
|
Chris@87
|
20 Real FFTs
|
Chris@87
|
21 ---------
|
Chris@87
|
22
|
Chris@87
|
23 .. autosummary::
|
Chris@87
|
24 :toctree: generated/
|
Chris@87
|
25
|
Chris@87
|
26 rfft Real discrete Fourier transform.
|
Chris@87
|
27 irfft Inverse real discrete Fourier transform.
|
Chris@87
|
28 rfft2 Real discrete Fourier transform in two dimensions.
|
Chris@87
|
29 irfft2 Inverse real discrete Fourier transform in two dimensions.
|
Chris@87
|
30 rfftn Real discrete Fourier transform in N dimensions.
|
Chris@87
|
31 irfftn Inverse real discrete Fourier transform in N dimensions.
|
Chris@87
|
32
|
Chris@87
|
33 Hermitian FFTs
|
Chris@87
|
34 --------------
|
Chris@87
|
35
|
Chris@87
|
36 .. autosummary::
|
Chris@87
|
37 :toctree: generated/
|
Chris@87
|
38
|
Chris@87
|
39 hfft Hermitian discrete Fourier transform.
|
Chris@87
|
40 ihfft Inverse Hermitian discrete Fourier transform.
|
Chris@87
|
41
|
Chris@87
|
42 Helper routines
|
Chris@87
|
43 ---------------
|
Chris@87
|
44
|
Chris@87
|
45 .. autosummary::
|
Chris@87
|
46 :toctree: generated/
|
Chris@87
|
47
|
Chris@87
|
48 fftfreq Discrete Fourier Transform sample frequencies.
|
Chris@87
|
49 rfftfreq DFT sample frequencies (for usage with rfft, irfft).
|
Chris@87
|
50 fftshift Shift zero-frequency component to center of spectrum.
|
Chris@87
|
51 ifftshift Inverse of fftshift.
|
Chris@87
|
52
|
Chris@87
|
53
|
Chris@87
|
54 Background information
|
Chris@87
|
55 ----------------------
|
Chris@87
|
56
|
Chris@87
|
57 Fourier analysis is fundamentally a method for expressing a function as a
|
Chris@87
|
58 sum of periodic components, and for recovering the function from those
|
Chris@87
|
59 components. When both the function and its Fourier transform are
|
Chris@87
|
60 replaced with discretized counterparts, it is called the discrete Fourier
|
Chris@87
|
61 transform (DFT). The DFT has become a mainstay of numerical computing in
|
Chris@87
|
62 part because of a very fast algorithm for computing it, called the Fast
|
Chris@87
|
63 Fourier Transform (FFT), which was known to Gauss (1805) and was brought
|
Chris@87
|
64 to light in its current form by Cooley and Tukey [CT]_. Press et al. [NR]_
|
Chris@87
|
65 provide an accessible introduction to Fourier analysis and its
|
Chris@87
|
66 applications.
|
Chris@87
|
67
|
Chris@87
|
68 Because the discrete Fourier transform separates its input into
|
Chris@87
|
69 components that contribute at discrete frequencies, it has a great number
|
Chris@87
|
70 of applications in digital signal processing, e.g., for filtering, and in
|
Chris@87
|
71 this context the discretized input to the transform is customarily
|
Chris@87
|
72 referred to as a *signal*, which exists in the *time domain*. The output
|
Chris@87
|
73 is called a *spectrum* or *transform* and exists in the *frequency
|
Chris@87
|
74 domain*.
|
Chris@87
|
75
|
Chris@87
|
76 Implementation details
|
Chris@87
|
77 ----------------------
|
Chris@87
|
78
|
Chris@87
|
79 There are many ways to define the DFT, varying in the sign of the
|
Chris@87
|
80 exponent, normalization, etc. In this implementation, the DFT is defined
|
Chris@87
|
81 as
|
Chris@87
|
82
|
Chris@87
|
83 .. math::
|
Chris@87
|
84 A_k = \\sum_{m=0}^{n-1} a_m \\exp\\left\\{-2\\pi i{mk \\over n}\\right\\}
|
Chris@87
|
85 \\qquad k = 0,\\ldots,n-1.
|
Chris@87
|
86
|
Chris@87
|
87 The DFT is in general defined for complex inputs and outputs, and a
|
Chris@87
|
88 single-frequency component at linear frequency :math:`f` is
|
Chris@87
|
89 represented by a complex exponential
|
Chris@87
|
90 :math:`a_m = \\exp\\{2\\pi i\\,f m\\Delta t\\}`, where :math:`\\Delta t`
|
Chris@87
|
91 is the sampling interval.
|
Chris@87
|
92
|
Chris@87
|
93 The values in the result follow so-called "standard" order: If ``A =
|
Chris@87
|
94 fft(a, n)``, then ``A[0]`` contains the zero-frequency term (the mean of
|
Chris@87
|
95 the signal), which is always purely real for real inputs. Then ``A[1:n/2]``
|
Chris@87
|
96 contains the positive-frequency terms, and ``A[n/2+1:]`` contains the
|
Chris@87
|
97 negative-frequency terms, in order of decreasingly negative frequency.
|
Chris@87
|
98 For an even number of input points, ``A[n/2]`` represents both positive and
|
Chris@87
|
99 negative Nyquist frequency, and is also purely real for real input. For
|
Chris@87
|
100 an odd number of input points, ``A[(n-1)/2]`` contains the largest positive
|
Chris@87
|
101 frequency, while ``A[(n+1)/2]`` contains the largest negative frequency.
|
Chris@87
|
102 The routine ``np.fft.fftfreq(n)`` returns an array giving the frequencies
|
Chris@87
|
103 of corresponding elements in the output. The routine
|
Chris@87
|
104 ``np.fft.fftshift(A)`` shifts transforms and their frequencies to put the
|
Chris@87
|
105 zero-frequency components in the middle, and ``np.fft.ifftshift(A)`` undoes
|
Chris@87
|
106 that shift.
|
Chris@87
|
107
|
Chris@87
|
108 When the input `a` is a time-domain signal and ``A = fft(a)``, ``np.abs(A)``
|
Chris@87
|
109 is its amplitude spectrum and ``np.abs(A)**2`` is its power spectrum.
|
Chris@87
|
110 The phase spectrum is obtained by ``np.angle(A)``.
|
Chris@87
|
111
|
Chris@87
|
112 The inverse DFT is defined as
|
Chris@87
|
113
|
Chris@87
|
114 .. math::
|
Chris@87
|
115 a_m = \\frac{1}{n}\\sum_{k=0}^{n-1}A_k\\exp\\left\\{2\\pi i{mk\\over n}\\right\\}
|
Chris@87
|
116 \\qquad m = 0,\\ldots,n-1.
|
Chris@87
|
117
|
Chris@87
|
118 It differs from the forward transform by the sign of the exponential
|
Chris@87
|
119 argument and the normalization by :math:`1/n`.
|
Chris@87
|
120
|
Chris@87
|
121 Real and Hermitian transforms
|
Chris@87
|
122 -----------------------------
|
Chris@87
|
123
|
Chris@87
|
124 When the input is purely real, its transform is Hermitian, i.e., the
|
Chris@87
|
125 component at frequency :math:`f_k` is the complex conjugate of the
|
Chris@87
|
126 component at frequency :math:`-f_k`, which means that for real
|
Chris@87
|
127 inputs there is no information in the negative frequency components that
|
Chris@87
|
128 is not already available from the positive frequency components.
|
Chris@87
|
129 The family of `rfft` functions is
|
Chris@87
|
130 designed to operate on real inputs, and exploits this symmetry by
|
Chris@87
|
131 computing only the positive frequency components, up to and including the
|
Chris@87
|
132 Nyquist frequency. Thus, ``n`` input points produce ``n/2+1`` complex
|
Chris@87
|
133 output points. The inverses of this family assumes the same symmetry of
|
Chris@87
|
134 its input, and for an output of ``n`` points uses ``n/2+1`` input points.
|
Chris@87
|
135
|
Chris@87
|
136 Correspondingly, when the spectrum is purely real, the signal is
|
Chris@87
|
137 Hermitian. The `hfft` family of functions exploits this symmetry by
|
Chris@87
|
138 using ``n/2+1`` complex points in the input (time) domain for ``n`` real
|
Chris@87
|
139 points in the frequency domain.
|
Chris@87
|
140
|
Chris@87
|
141 In higher dimensions, FFTs are used, e.g., for image analysis and
|
Chris@87
|
142 filtering. The computational efficiency of the FFT means that it can
|
Chris@87
|
143 also be a faster way to compute large convolutions, using the property
|
Chris@87
|
144 that a convolution in the time domain is equivalent to a point-by-point
|
Chris@87
|
145 multiplication in the frequency domain.
|
Chris@87
|
146
|
Chris@87
|
147 Higher dimensions
|
Chris@87
|
148 -----------------
|
Chris@87
|
149
|
Chris@87
|
150 In two dimensions, the DFT is defined as
|
Chris@87
|
151
|
Chris@87
|
152 .. math::
|
Chris@87
|
153 A_{kl} = \\sum_{m=0}^{M-1} \\sum_{n=0}^{N-1}
|
Chris@87
|
154 a_{mn}\\exp\\left\\{-2\\pi i \\left({mk\\over M}+{nl\\over N}\\right)\\right\\}
|
Chris@87
|
155 \\qquad k = 0, \\ldots, M-1;\\quad l = 0, \\ldots, N-1,
|
Chris@87
|
156
|
Chris@87
|
157 which extends in the obvious way to higher dimensions, and the inverses
|
Chris@87
|
158 in higher dimensions also extend in the same way.
|
Chris@87
|
159
|
Chris@87
|
160 References
|
Chris@87
|
161 ----------
|
Chris@87
|
162
|
Chris@87
|
163 .. [CT] Cooley, James W., and John W. Tukey, 1965, "An algorithm for the
|
Chris@87
|
164 machine calculation of complex Fourier series," *Math. Comput.*
|
Chris@87
|
165 19: 297-301.
|
Chris@87
|
166
|
Chris@87
|
167 .. [NR] Press, W., Teukolsky, S., Vetterline, W.T., and Flannery, B.P.,
|
Chris@87
|
168 2007, *Numerical Recipes: The Art of Scientific Computing*, ch.
|
Chris@87
|
169 12-13. Cambridge Univ. Press, Cambridge, UK.
|
Chris@87
|
170
|
Chris@87
|
171 Examples
|
Chris@87
|
172 --------
|
Chris@87
|
173
|
Chris@87
|
174 For examples, see the various functions.
|
Chris@87
|
175
|
Chris@87
|
176 """
|
Chris@87
|
177 from __future__ import division, absolute_import, print_function
|
Chris@87
|
178
|
Chris@87
|
179 depends = ['core']
|