dct#
- scipy.fftpack.dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False)[原始碼]#
返回任意類型序列 x 的離散餘弦轉換 (Discrete Cosine Transform)。
- 參數:
- xarray_like (類陣列)
輸入陣列。
- type{1, 2, 3, 4}, 選項性
DCT 的類型(請參閱「Notes」)。預設類型為 2。
- nint, 選項性
轉換的長度。如果
n < x.shape[axis]
,則 x 會被截斷。如果n > x.shape[axis]
,則會對 x 進行零填充。預設結果為n = x.shape[axis]
。- axisint, 選項性
計算 dct 的軸;預設值為最後一個軸(即
axis=-1
)。- norm{None, ‘ortho’}, 選項性
正規化模式(請參閱「Notes」)。預設值為 None。
- overwrite_xbool, 選項性
如果為 True,則可以破壞 x 的內容;預設值為 False。
- 返回:
- yndarray of real (實數值的 ndarray 陣列)
轉換後的輸入陣列。
另請參閱
idct
反 DCT
Notes
對於單維陣列
x
,dct(x, norm='ortho')
等於 MATLAB 的dct(x)
。理論上,DCT 有 8 種類型,scipy 僅實作了前 4 種類型。「The」DCT 通常指的是 DCT 類型 2,「the」反 DCT 通常指的是 DCT 類型 3。
類型 I
DCT-I 有幾種定義;我們使用以下定義(對於
norm=None
):\[y_k = x_0 + (-1)^k x_{N-1} + 2 \sum_{n=1}^{N-2} x_n \cos\left( \frac{\pi k n}{N-1} \right)\]If
norm='ortho'
,x[0]
andx[N-1]
are multiplied by a scaling factor of \(\sqrt{2}\), andy[k]
is multiplied by a scaling factorf
\[\begin{split}f = \begin{cases} \frac{1}{2}\sqrt{\frac{1}{N-1}} & \text{if }k=0\text{ or }N-1, \\ \frac{1}{2}\sqrt{\frac{2}{N-1}} & \text{otherwise} \end{cases}\end{split}\]Added in version 1.2.0: Orthonormalization in DCT-I.
Note
The DCT-I is only supported for input size > 1.
類型 II
DCT-II 有幾種定義;我們使用以下定義(對於
norm=None
):\[y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi k(2n+1)}{2N} \right)\]If
norm='ortho'
,y[k]
is multiplied by a scaling factorf
\[\begin{split}f = \begin{cases} \sqrt{\frac{1}{4N}} & \text{if }k=0, \\ \sqrt{\frac{1}{2N}} & \text{otherwise} \end{cases}\end{split}\]which makes the corresponding matrix of coefficients orthonormal (
O @ O.T = np.eye(N)
).類型 III
有幾種定義;我們使用以下定義(對於
norm=None
):\[y_k = x_0 + 2 \sum_{n=1}^{N-1} x_n \cos\left(\frac{\pi(2k+1)n}{2N}\right)\]or, for
norm='ortho'
\[y_k = \frac{x_0}{\sqrt{N}} + \sqrt{\frac{2}{N}} \sum_{n=1}^{N-1} x_n \cos\left(\frac{\pi(2k+1)n}{2N}\right)\]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.類型 IV
DCT-IV 有幾種定義;我們使用以下定義(對於
norm=None
):\[y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi(2k+1)(2n+1)}{4N} \right)\]If
norm='ortho'
,y[k]
is multiplied by a scaling factorf
\[f = \frac{1}{\sqrt{2N}}\]Added in version 1.2.0: Support for DCT-IV.
參考文獻
[1]〈一維和二維的快速餘弦轉換〉,作者:J. Makhoul,《IEEE Transactions on acoustics, speech and signal processing》第 28(1) 卷,第 27-34 頁,DOI:10.1109/TASSP.1980.1163351 (1980)。
[2]維基百科,「離散餘弦轉換」,https://en.wikipedia.org/wiki/Discrete_cosine_transform
範例
對於實數、偶對稱的輸入,類型 1 DCT 等效於 FFT(但速度更快)。輸出也是實數且偶對稱。FFT 輸入的一半用於產生 FFT 輸出的一半
>>> from scipy.fftpack import fft, dct >>> import numpy as np >>> fft(np.array([4., 3., 5., 10., 5., 3.])).real array([ 30., -8., 6., -2., 6., -8.]) >>> dct(np.array([4., 3., 5., 10.]), 1) array([ 30., -8., 6., -2.])