Debian Codegraph @

.cargo/vendor/rustdct/tests/common/reference_impls.rs

raw
1 /// This file contains reference implementations of all DCT and DST transforms.
2 /// The goal of these implementations is not to be fast, but to match the mathematical definitions as closely as possible and to be easy to follow and debug
3 /// The reference for the mathematical definitions was section 9 of "The Discrete W Transforms" by Wang and Hunt, but with the normalization/orthogonalization factors omitted.
4 use std::f64;
5
6 /// Simplified version of DCT1
7 pub fn reference_dct1(input: &[f64]) -> Vec<f64> {
8 let mut result = Vec::new();
9
10 for output_index in 0..input.len() {
11 let mut entry = 0.0;
12 for input_index in 0..input.len() {
13 let multiplier = if input_index == 0 || input_index == input.len() - 1 {
14 0.5
15 } else {
16 1.0
17 };
18 let cos_inner = (output_index as f64) * (input_index as f64) * f64::consts::PI
19 / ((input.len() - 1) as f64);
20 let twiddle = cos_inner.cos();
21 entry += input[input_index] * twiddle * multiplier;
22 }
23 result.push(entry);
24 }
25 result
26 }
27
28 /// Simplified version of DCT2
29 pub fn reference_dct2(input: &[f64]) -> Vec<f64> {
30 let mut result = Vec::new();
31
32 for output_index in 0..input.len() {
33 let mut entry = 0.0;
34 for input_index in 0..input.len() {
35 let cos_inner = (output_index as f64) * (input_index as f64 + 0.5) * f64::consts::PI
36 / (input.len() as f64);
37 let twiddle = cos_inner.cos();
38 entry += input[input_index] * twiddle;
39 }
40 result.push(entry);
41 }
42
43 result
44 }
45
46 /// Simplified version of DCT3
47 pub fn reference_dct3(input: &[f64]) -> Vec<f64> {
48 let mut result = Vec::new();
49
50 for output_index in 0..input.len() {
51 let mut entry = 0.0;
52 for input_index in 0..input.len() {
53 let multiplier = if input_index == 0 { 0.5 } else { 1.0 };
54 let cos_inner = (output_index as f64 + 0.5) * (input_index as f64) * f64::consts::PI
55 / (input.len() as f64);
56 let twiddle = cos_inner.cos();
57 entry += input[input_index] * twiddle * multiplier;
58 }
59 result.push(entry);
60 }
61
62 result
63 }
64
65 /// Simplified version of DCT4
66 pub fn reference_dct4(input: &[f64]) -> Vec<f64> {
67 let mut result = Vec::new();
68
69 for output_index in 0..input.len() {
70 let mut entry = 0.0;
71 for input_index in 0..input.len() {
72 let cos_inner =
73 (output_index as f64 + 0.5) * (input_index as f64 + 0.5) * f64::consts::PI
74 / (input.len() as f64);
75 let twiddle = cos_inner.cos();
76 entry += input[input_index] * twiddle;
77 }
78 result.push(entry);
79 }
80
81 result
82 }
83
84 /// Simplified version of DCT5
85 pub fn reference_dct5(input: &[f64]) -> Vec<f64> {
86 let mut result = Vec::new();
87
88 for output_index in 0..input.len() {
89 let mut entry = 0.0;
90 for input_index in 0..input.len() {
91 let multiplier = if input_index == 0 { 0.5 } else { 1.0 };
92 let cos_inner = (output_index as f64) * (input_index as f64) * f64::consts::PI
93 / (input.len() as f64 - 0.5);
94 let twiddle = cos_inner.cos();
95 entry += input[input_index] * twiddle * multiplier;
96 }
97 result.push(entry);
98 }
99
100 result
101 }
102
103 /// Simplified version of DCT6
104 pub fn reference_dct6(input: &[f64]) -> Vec<f64> {
105 let mut result = Vec::new();
106
107 for output_index in 0..input.len() {
108 let mut entry = 0.0;
109 for input_index in 0..input.len() {
110 let multiplier = if input_index == input.len() - 1 {
111 0.5
112 } else {
113 1.0
114 };
115 let cos_inner = (output_index as f64) * (input_index as f64 + 0.5) * f64::consts::PI
116 / (input.len() as f64 - 0.5);
117 let twiddle = cos_inner.cos();
118 entry += input[input_index] * twiddle * multiplier;
119 }
120 result.push(entry);
121 }
122
123 result
124 }
125
126 /// Simplified version of DCT7
127 pub fn reference_dct7(input: &[f64]) -> Vec<f64> {
128 let mut result = Vec::new();
129
130 for output_index in 0..input.len() {
131 let mut entry = 0.0;
132 for input_index in 0..input.len() {
133 let multiplier = if input_index == 0 { 0.5 } else { 1.0 };
134 let cos_inner = (output_index as f64 + 0.5) * (input_index as f64) * f64::consts::PI
135 / (input.len() as f64 - 0.5);
136 let twiddle = cos_inner.cos();
137 entry += input[input_index] * twiddle * multiplier;
138 }
139 result.push(entry);
140 }
141
142 result
143 }
144
145 /// Simplified version of DCT8
146 pub fn reference_dct8(input: &[f64]) -> Vec<f64> {
147 let mut result = Vec::new();
148
149 for output_index in 0..input.len() {
150 let mut entry = 0.0;
151 for input_index in 0..input.len() {
152 let cos_inner =
153 (output_index as f64 + 0.5) * (input_index as f64 + 0.5) * f64::consts::PI
154 / (input.len() as f64 + 0.5);
155 let twiddle = cos_inner.cos();
156 entry += input[input_index] * twiddle;
157 }
158 result.push(entry);
159 }
160
161 result
162 }
163
164 /// Simplified version of DST1
165 pub fn reference_dst1(input: &[f64]) -> Vec<f64> {
166 let mut result = Vec::new();
167 for output_index in 0..input.len() {
168 let mut entry = 0.0;
169 for input_index in 0..input.len() {
170 let sin_inner =
171 (output_index as f64 + 1.0) * (input_index as f64 + 1.0) * f64::consts::PI
172 / ((input.len() + 1) as f64);
173 let twiddle = sin_inner.sin();
174 entry += input[input_index] * twiddle;
175 }
176 result.push(entry);
177 }
178 result
179 }
180
181 /// Simplified version of DST2
182 pub fn reference_dst2(input: &[f64]) -> Vec<f64> {
183 let mut result = Vec::new();
184 for output_index in 0..input.len() {
185 let mut entry = 0.0;
186 for input_index in 0..input.len() {
187 let sin_inner =
188 (output_index as f64 + 1.0) * (input_index as f64 + 0.5) * f64::consts::PI
189 / (input.len() as f64);
190 let twiddle = sin_inner.sin();
191 entry += input[input_index] * twiddle;
192 }
193 result.push(entry);
194 }
195 result
196 }
197
198 /// Simplified version of DST3
199 pub fn reference_dst3(input: &[f64]) -> Vec<f64> {
200 let mut result = Vec::new();
201 for output_index in 0..input.len() {
202 let mut entry = 0.0;
203 for input_index in 0..input.len() {
204 let multiplier = if input_index == input.len() - 1 {
205 0.5
206 } else {
207 1.0
208 };
209 let sin_inner =
210 (output_index as f64 + 0.5) * (input_index as f64 + 1.0) * f64::consts::PI
211 / (input.len() as f64);
212 let twiddle = sin_inner.sin();
213 entry += input[input_index] * twiddle * multiplier;
214 }
215 result.push(entry);
216 }
217 result
218 }
219
220 /// Simplified version of DST4
221 pub fn reference_dst4(input: &[f64]) -> Vec<f64> {
222 let mut result = Vec::new();
223
224 for output_index in 0..input.len() {
225 let mut entry = 0.0;
226 for input_index in 0..input.len() {
227 let sin_inner =
228 (output_index as f64 + 0.5) * (input_index as f64 + 0.5) * f64::consts::PI
229 / (input.len() as f64);
230 let twiddle = sin_inner.sin();
231 entry += input[input_index] * twiddle;
232 }
233 result.push(entry);
234 }
235
236 result
237 }
238
239 /// Simplified version of DST5
240 pub fn reference_dst5(input: &[f64]) -> Vec<f64> {
241 let mut result = Vec::new();
242 for output_index in 0..input.len() {
243 let mut entry = 0.0;
244 for input_index in 0..input.len() {
245 let sin_inner =
246 (output_index as f64 + 1.0) * (input_index as f64 + 1.0) * f64::consts::PI
247 / ((input.len()) as f64 + 0.5);
248 let twiddle = sin_inner.sin();
249 entry += input[input_index] * twiddle;
250 }
251 result.push(entry);
252 }
253 result
254 }
255
256 /// Simplified version of DST6
257 pub fn reference_dst6(input: &[f64]) -> Vec<f64> {
258 let mut result = Vec::new();
259 for output_index in 0..input.len() {
260 let mut entry = 0.0;
261 for input_index in 0..input.len() {
262 let sin_inner =
263 (output_index as f64 + 1.0) * (input_index as f64 + 0.5) * f64::consts::PI
264 / (input.len() as f64 + 0.5);
265 let twiddle = sin_inner.sin();
266 entry += input[input_index] * twiddle;
267 }
268 result.push(entry);
269 }
270 result
271 }
272
273 /// Simplified version of DST7
274 pub fn reference_dst7(input: &[f64]) -> Vec<f64> {
275 let mut result = Vec::new();
276 for output_index in 0..input.len() {
277 let mut entry = 0.0;
278 for input_index in 0..input.len() {
279 let sin_inner =
280 (output_index as f64 + 0.5) * (input_index as f64 + 1.0) * f64::consts::PI
281 / (input.len() as f64 + 0.5);
282 let twiddle = sin_inner.sin();
283 entry += input[input_index] * twiddle;
284 }
285 result.push(entry);
286 }
287 result
288 }
289
290 /// Simplified version of DST8
291 pub fn reference_dst8(input: &[f64]) -> Vec<f64> {
292 let mut result = Vec::new();
293
294 for output_index in 0..input.len() {
295 let mut entry = 0.0;
296 for input_index in 0..input.len() {
297 let multiplier = if input_index == input.len() - 1 {
298 0.5
299 } else {
300 1.0
301 };
302 let sin_inner =
303 (output_index as f64 + 0.5) * (input_index as f64 + 0.5) * f64::consts::PI
304 / (input.len() as f64 - 0.5);
305 let twiddle = sin_inner.sin();
306 entry += input[input_index] * twiddle * multiplier;
307 }
308 result.push(entry);
309 }
310
311 result
312 }