<a href="https://colab.research.google.com/github/msiplab/VieWork/blob/master/vie_sec9_dct.ipynb" target="_parent"></a>

# 線形変換処理


## 配列の準備
数値計算ライブラリ NumPy を利用


```
import numpy as np 

arrayX = np.matrix([[2,6,2,4],[2,2,2,6],[6,6,4,4],[2,4,4,4]])
display(arrayX)
```


    matrix([[2, 6, 2, 4],
            [2, 2, 2, 6],
            [6, 6, 4, 4],
            [2, 4, 4, 4]])


## 離散コサイン変換行列の定義

\begin{array}{l}
y[m]=\sqrt{\frac{2}{M}} k_{m} \sum_{n=0}^{N-1} \cos \left(\frac{(2 n+1) m \pi}{2 M}\right) x[n],\, m=0,1, \cdots, M-1 \\
x[n]=\sqrt{\frac{2}{M}} \sum_{m=0}^{N-1} k_{m} \cos \left(\frac{(2 n+1) m \pi}{2 M}\right) y[m],\, n=0,1, \cdots, M-1
\end{array}
ただし、
\begin{equation}
k_{m}=\left\{\begin{array}{cc}
\frac{1}{\sqrt{2}} & m=0 \\
1 & m=1,2, \cdots, M-1
\end{array}\right.
\end{equation}

### 順変換行列



```
dctSize = 4
arrayDct = np.matrix(np.zeros([dctSize,dctSize]))
for iCol in range(dctSize):
    for iRow in range(dctSize):
        k = 1
        if iRow == 0:
            k = 1/np.sqrt(2)
        arrayDct[iRow,iCol] = np.sqrt(2/dctSize)*k*np.cos((2*iCol+1)*iRow*np.pi/(2*dctSize))
display(arrayDct)

```


    matrix([[ 0.5       ,  0.5       ,  0.5       ,  0.5       ],
            [ 0.65328148,  0.27059805, -0.27059805, -0.65328148],
            [ 0.5       , -0.5       , -0.5       ,  0.5       ],
            [ 0.27059805, -0.65328148,  0.65328148, -0.27059805]])


### 逆変換行列

離散コサイン変換(DCT)は正規直交行列のため、逆変換行列は順変換行列の転置で与えられる。
\begin{equation}
\mathbf{C}^{-1}=\mathbf{C}^T
\end{equation}


```
arrayIdct = arrayDct.T
display(arrayIdct)
```


    matrix([[ 0.5       ,  0.65328148,  0.5       ,  0.27059805],
            [ 0.5       ,  0.27059805, -0.5       , -0.65328148],
            [ 0.5       , -0.27059805, -0.5       ,  0.65328148],
            [ 0.5       , -0.65328148,  0.5       , -0.27059805]])


## 二次元DCT
\begin{equation}
\mathbf{Y}=\mathbf{CXC}^T
\end{equation}



```
arrayY = np.dot(np.dot(arrayDct,arrayX),arrayDct.T)
display(arrayY)
```


    matrix([[ 1.50000000e+01, -1.14805030e+00, -9.99200722e-16,
             -2.77163860e+00],
            [-1.08239220e+00, -5.00000000e-01,  5.41196100e-01,
             -1.79289322e+00],
            [-1.00000000e+00, -3.82683432e-01, -2.00000000e+00,
             -9.23879533e-01],
            [ 2.61312593e+00,  3.20710678e+00, -1.30656296e+00,
             -5.00000000e-01]])


## 二次元逆DCT
\begin{equation}
\mathbf{X}=\mathbf{C}^{-1}\mathbf{YC}^{-T}
\end{equation}


```
arrayR = np.dot(np.dot(arrayIdct,arrayY),arrayIdct.T)
display(arrayR)
```


    matrix([[2., 6., 2., 4.],
            [2., 2., 2., 6.],
            [6., 6., 4., 4.],
            [2., 4., 4., 4.]])


5d128293e0d97a326644d75da699ec5d12478da3
ipynb
Jupyter Notebook
python/vie_sec9_dct.ipynb
shodimaggio/VieWork
d007e91d0df24683356c0304599a2976950890f1
MIT
python/vie_sec9_dct.ipynb
shodimaggio/VieWork
d007e91d0df24683356c0304599a2976950890f1
MIT
python/vie_sec9_dct.ipynb
shodimaggio/VieWork
d007e91d0df24683356c0304599a2976950890f1
MIT
Qwen/Qwen-72B
1. YES
2. YES