## SciPy – 27 – trasformate di Fourier – 1

Tirana

Continuo da qui, copio qui.

Fourier analysis is a method for expressing a function as a sum of periodic components, and for recovering the signal from those components. When both the function and its Fourier transform are replaced with discretized counterparts, it is called the discrete Fourier transform (DFT). The DFT has become a mainstay of numerical computing in part because of a very fast algorithm for computing it, called the Fast Fourier Transform (FFT), which was known to Gauss (1805) and was brought to light in its current form by Cooley and Tukey. Press et al. provide an accessible introduction to Fourier analysis and its applications.

Note: PyFFTW provides a way to replace a number of functions in `scipy.fftpack` with its own functions, which are usually significantly faster, via `pyfftw.interfaces`. Because PyFFTW relies on the GPL-licensed FFTW it cannot be included in Scipy. Users for whom the speed of FFT routines is critical should consider installing PyFFTW.

Fast Fourier transforms
Trasformata di Fourier unidimensionale discreta
The FFT `y[k]` of length `N` of the length-N sequence `x[n]` is defined as

and the inverse transform is defined as follows

These transforms can be calculated by means of `fft` and `ifft`, respectively as shown in the following example.

From the definition of the FFT it can be seen that

In the example

which corresponds to `y[0]`. For `N` even, the elements `y[1]...y[N/2−1]` contain the positive-frequency terms, and the elements `y[N/2]...y[N−1]` contain the negative-frequency terms, in order of decreasingly negative frequency. For `N` odd, the elements `y[1]...y[(N−1)/2]` contain the positive- frequency terms, and the elements `y[(N+1)/2]...y[N−1]` contain the negative- frequency terms, in order of decreasingly negative frequency.

In case the sequence `x` is real-valued, the values of `y[n]` for positive frequencies is the conjugate of the values `y[n]` for negative frequencies (because the spectrum is symmetric). Typically, only the FFT corresponding to positive frequencies is plotted.

The example plots the FFT of the sum of two sines.

The FFT input signal is inherently truncated. This truncation can be modelled as multiplication of an infinite signal with a rectangular window function. In the spectral domain this multiplication becomes convolution of the signal spectrum with the window function spectrum, being of form `sin(x)/x`. This convolution is the cause of an effect called spectral leakage (see here). Windowing the signal with a dedicated window function helps mitigate spectral leakage. The example below uses a Blackman window from scipy.signal and shows the effect of windowing (the zero component of the FFT has been truncated for illustrative purposes).

uhmmm… un warning e manca la griglia 😯

In case the sequence `x` is complex-valued, the spectrum is no longer symmetric. To simplify working wit the FFT functions, scipy provides the following two helper functions.

The function `fftfreq` returns the FFT sample frequency points.

In a similar spirit, the function `fftshift` allows swapping the lower and upper halves of a vector, so that it becomes suitable for display.

The example below plots the FFT of two complex exponentials; note the asymmetric spectrum.

The function `rfft` calculates the FFT of a real sequence and outputs the FFT coefficients `y[n]` with separate real and imaginary parts. In case of `N` being even: `[y[0],Re(y[1]),Im(y[1]),...,Re(y[N/2])]`; in case `N` being odd `[y[0],Re(y[1]),Im(y[1]),...,Re(y[N/2]),Im(y[N/2])]`.

The corresponding function `irfft` calculates the IFFT of the FFT coefficients with this special ordering.

Annunci