SciPy – 28 – trasformate di Fourier – 2

Continuo da qui, copio qui.

Trasformazioni discrete di Fourier bi e n-dimensionali
The functions `fft2` and `ifft2` provide 2-dimensional FFT, and IFFT, respectively. Similar, `fftn` and `ifftn` provide n-dimensional FFT, and IFFT, respectively.

The example below demonstrates a 2-dimensional IFFT and plots the resulting (2-dimensional) time-domain signals. Codice troppo lungo, lo raccolgo nel file `fft2.py`

``````import numpy as np
from scipy.fftpack import ifftn
import matplotlib.pyplot as plt
import matplotlib.cm as cm

N = 30
f, ((ax1, ax2, ax3), (ax4, ax5, ax6)) = plt.subplots(2, 3, sharex='col', sharey='row')
xf = np.zeros((N,N))
xf[0, 5] = 1
xf[0, N-5] = 1
Z = ifftn(xf)
ax1.imshow(xf, cmap=cm.Reds)
ax4.imshow(np.real(Z), cmap=cm.gray)
xf = np.zeros((N, N))
xf[5, 0] = 1
xf[N-5, 0] = 1
Z = ifftn(xf)
ax2.imshow(xf, cmap=cm.Reds)
ax5.imshow(np.real(Z), cmap=cm.gray)
xf = np.zeros((N, N))
xf[5, 10] = 1
xf[N-5, N-10] = 1
Z = ifftn(xf)
ax3.imshow(xf, cmap=cm.Reds)
ax6.imshow(np.real(Z), cmap=cm.gray)
plt.savefig('sp161.png')``````

Convoluzione FFT

`scipy.fftpack.convolve` performs a convolution of two one-dimensional arrays in frequency domain.

Trasformazione discreta del coseno (DCT)
Scipy provides a DCT with the function `dct` and a corresponding IDCT with the function `idct`. There are 8 types of the DCT; however, only the first 3 types are implemented in `scipy`. “The” DCT generally refers to DCT type 2, and “the” Inverse DCT generally refers to DCT type 3. In addition, the DCT coefficients can be normalized differently (for most types, `scipy` provides `None` and `ortho`). Two parameters of the `dct` / `idct` function calls allow setting the DCT type and coefficient normalization.

For a single dimension array `x`, `dct(x, norm=’ortho’)` is equal to MATLAB `dct(x)`.

DCT tipo I
Scipy uses the following definition of the unnormalized DCT-I (`norm='None'`):

Only `None` is supported as normalization mode for DCT-I. Note also that the DCT-I is only supported for input size `> 1`.

DCT tipo II
Scipy uses the following definition of the unnormalized DCT-II (`norm='None'`):

In case of the normalized DCT (`norm='ortho'`), the DCT coefficients `y[k]` are multiplied by a scaling factor `f`:

In this case, the DCT “base functions”

become orthonormal:

DCT tipo III
Scipy uses the following definition of the unnormalized DCT-III (`norm='None'`):

or, for `norm='ortho'`:

DCT e IDCT
The (unnormalized) DCT-III is the inverse of the (unnormalized) DCT-II, up to a factor 2N. The orthonormalized DCT-III is exactly the inverse of the orthonormalized DCT- II. The function `idct` performs the mappings between the DCT and IDCT types.

The example below shows the relation between DCT and IDCT for different types and normalizations.

Esempio
The DCT exhibits the “energy compaction property”, meaning that for many signals only the first few DCT coefficients have significant magnitude. Zeroing out the other coefficients leads to a small reconstruction error, a fact which is exploited in lossy signal compression (e.g. JPEG compression).

The example below shows a signal `x` and two reconstructions (`x20` and `x15`)from the signal’s DCT coefficients. The signal `x20` is reconstructed from the first 20 DCT coefficients, `x15` is reconstructed from the first 15 DCT coefficients. It can be seen that the relative error of using 20 coefficients is still very small (~0.1%), but provides a five-fold compression rate.

``````import numpy as np
from scipy.fftpack import dct, idct
import matplotlib.pyplot as plt

N = 100
t = np.linspace(0,20,N)
x = np.exp(-t/3)*np.cos(2*t)
y = dct(x, norm='ortho')
window = np.zeros(N)
window[:20] = 1
yr = idct(y*window, norm='ortho')
sum(abs(x-yr)**2) / sum(abs(x)**2)
0.0010901402257
plt.plot(t, x, '-bx')
plt.plot(t, yr, 'ro')
window = np.zeros(N)
window[:15] = 1
yr = idct(y*window, norm='ortho')
sum(abs(x-yr)**2) / sum(abs(x)**2)
0.0718818065008
plt.plot(t, yr, 'g+')
plt.legend(['x', '\$x_{20}\$', '\$x_{15}\$'])
plt.grid()
plt.savefig('sp170.png')``````

Trasformazione discreta del seno (DST)

Scipy provides a DST with the function dst and a corresponding IDST with the function `idst`.

There are theoretically 8 types of the DST for different combinations of even/odd boundary conditions and boundary off sets, only the first 3 types are implemented in scipy.

DST tipo I
DST-I assumes the input is odd around `n=-1` and `n=N`. Scipy uses the following definition of the unnormalized DST-I (`norm='None'`):

Only `None` is supported as normalization mode for DST-I. Note also that the DST-I is only supported for input size > 1. The (unnormalized) DST-I is its own inverse, up to a factor 2(N+1).

DST tipo II
DST-II assumes the input is odd around `n=-1/2` and even around `n=N`. Scipy uses the following definition of the unnormalized DST-II (`norm='None'`):

DST tipo III
DST-III assumes the input is odd around `n=-1` and even around `n=N-1`. Scipy uses the following definition of the unnormalized DST-III (`norm='None'`):

DST e IDST
The example below shows the relation between DST and IDST for different types and normalizations.

Distruzione della cache
To accelerate repeat transforms on arrays of the same shape and dtype, `scipy.fftpack` keeps a cache of the prime factorization of length of the array and pre-computed trigonometric functions. These caches can be destroyed by calling the appropriate function in `scipy.fftpack._fftpack`. dst(type=1) and `idst(type=1)` share a cache (`*dst1_cache`). As do `dst(type=2)`, `dst(type=3)`, `idst(type=3)`, and `idst(type=3)` (`*dst2_cache`).

Annunci