-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiDCT.php
160 lines (112 loc) · 3.46 KB
/
iDCT.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
require_once 'Complex.class.php';
require_once 'FFT.class.php';
class iDCT
{
public $ww;
public $inds;
public function __construct($rowsCount)
{
$this->computeIdct($rowsCount);
}
public function computeIdct($rowsCount) {
$this->ww = array();
$this->inds = array();
for ($i = 0; $i < $rowsCount; $i++) {
$baseComplex = new Complex(0, (-1 * pi() / (2 * $rowsCount)) * $i);
$baseComplex = Complex::Cexp($baseComplex);
$baseComplex = Complex::RCmul(2, $baseComplex);
$baseComplex = Complex::RCdiv(sqrt(2 * $rowsCount), $baseComplex);
$this->ww[] = $baseComplex;
}
$this->ww[0] = Complex::RCdiv(sqrt(2), $this->ww[0]);
$tmp = array();
$counter = 1;
for($i=0;$i<$rowsCount;$i+=2) {
$tmp[$i] = $counter;
$counter++;
}
$counter = $rowsCount;
for($i=1;$i<$rowsCount;$i+=2) {
$tmp[$i] = $counter;
$counter--;
}
$mul = array();
$ind = array();
for($i = 0; $i < $rowsCount; $i++) {
$mul[] = $i*$rowsCount;
}
for($i=0;$i<$rowsCount;$i++) {
$row = array();
for($j=0;$j<$rowsCount;$j++) {
$row[] = $tmp[$i]+$mul[$j];
}
$ind[] = $row;
}
$this->inds = $ind;
}
public function idct2d($data) {
return Matrix::transpose(
$this->idct1d(
Matrix::transpose(
$this->idct1d($data)
)
)
);
}
public function idct1d($data) {
$size = count($data);
$fft = new FFT($size);
for($i=0;$i<$size;$i++) {
for($j=0;$j<$size;$j++) {
$data[$i][$j] = Complex::RCmul($data[$i][$j], $this->ww[$i]);
}
}
$result = array();
for($i=0;$i<$size;$i++) {
$result[$i] = self::fft(Matrix::getColumn($data,$i));
}
$result = Matrix::transpose($result);
$result = $this->reoder($result,$this->inds);
for($i=0;$i<$size;$i++) {
for($j=0;$j<$size;$j++) {
$result[$i][$j] = $result[$i][$j]->getReal();
}
}
return $result;
}
public static function fft($data) {
$real = array();
$imag = array();
$size = count($data);
$fft = new FFT($size);
for($i=0;$i<$size;$i++) {
$real[$i] = $data[$i]->getReal();
$imag[$i] = -1*$data[$i]->getImag();
}
$real = $fft->fft($real);
$imag = $fft->fft($imag);
for($i=0;$i<$size;$i++) {
$c = Complex::Cmul($imag[$i], new Complex(0, 1));
$data[$i] = Complex::Cadd($real[$i], $c);
}
return $data;
}
public function reoder($data,$inds) {
$size = count($data);
$newData = Matrix::initializeArray($size,$size,0);
for($i=0;$i<$size;$i++) {
for($j=0;$j<$size;$j++) {
$indice = $inds[$i][$j];
$newData[$i][$j] = $data[$this->determineRow($indice,$size)][$this->determineColumn($indice,$size)];
}
}
return $newData;
}
public function determineColumn($i, $n) {
return ceil($i/$n)-1;
}
public function determineRow($i, $n) {
return ($i-$this->determineColumn($i, $n)*$n)-1;
}
}