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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use std::sync::Arc;
use rustfft::num_complex::Complex;
use rustfft::Length;
use twiddles;
use common;
use ::{DCT4, DST4, TransformType2And3, TransformType4};
pub struct Type4ConvertToType3Even<T> {
inner_dct: Arc<TransformType2And3<T>>,
twiddles: Box<[Complex<T>]>,
}
impl<T: common::DCTnum> Type4ConvertToType3Even<T> {
pub fn new(inner_dct: Arc<TransformType2And3<T>>) -> Self {
let inner_len = inner_dct.len();
let len = inner_len * 2;
let twiddles: Vec<Complex<T>> = (0..inner_len)
.map(|i| twiddles::single_twiddle(2 * i + 1, len * 8).conj())
.collect();
Type4ConvertToType3Even {
inner_dct: inner_dct,
twiddles: twiddles.into_boxed_slice(),
}
}
}
impl<T: common::DCTnum> DCT4<T> for Type4ConvertToType3Even<T> {
fn process_dct4(&self, input: &mut [T], output: &mut [T]) {
common::verify_length(input, output, self.len());
let len = self.len();
let inner_len = len / 2;
let (mut output_left, mut output_right) = output.split_at_mut(inner_len);
output_left[0] = input[0] * T::two();
for k in 1..inner_len {
output_left[k] = input[2 * k - 1] + input[2 * k];
output_right[k - 1] = input[2 * k - 1] - input[2 * k];
}
output_right[inner_len - 1] = input[len - 1] * T::two();
let (mut inner_result_cos, mut inner_result_sin) = input.split_at_mut(inner_len);
self.inner_dct.process_dct3(&mut output_left, &mut inner_result_cos);
self.inner_dct.process_dst3(&mut output_right, &mut inner_result_sin);
for k in 0..inner_len {
let twiddle = self.twiddles[k];
let cos_value = inner_result_cos[k];
let sin_value = inner_result_sin[k];
output_left[k] = cos_value * twiddle.re + sin_value * twiddle.im;
output_right[inner_len - 1 - k] = cos_value * twiddle.im - sin_value * twiddle.re;
}
}
}
impl<T: common::DCTnum> DST4<T> for Type4ConvertToType3Even<T> {
fn process_dst4(&self, input: &mut [T], output: &mut [T]) {
common::verify_length(input, output, self.len());
let len = self.len();
let inner_len = len / 2;
let (mut output_left, mut output_right) = output.split_at_mut(inner_len);
output_right[0] = input[0] * T::two();
for k in 1..inner_len {
output_left[k - 1] = input[2 * k - 1] + input[2 * k];
output_right[k] = input[2 * k] - input[2 * k - 1];
}
output_left[inner_len - 1] = input[len - 1] * T::two();
let (mut inner_result_cos, mut inner_result_sin) = input.split_at_mut(inner_len);
self.inner_dct.process_dst3(&mut output_left, &mut inner_result_cos);
self.inner_dct.process_dct3(&mut output_right, &mut inner_result_sin);
for k in 0..inner_len {
let twiddle = self.twiddles[k];
let cos_value = inner_result_cos[k];
let sin_value = inner_result_sin[k];
output_left[k] = cos_value * twiddle.re + sin_value * twiddle.im;
output_right[inner_len - 1 - k] = sin_value * twiddle.re - cos_value * twiddle.im;
}
}
}
impl<T: common::DCTnum> TransformType4<T> for Type4ConvertToType3Even<T>{}
impl<T> Length for Type4ConvertToType3Even<T> {
fn len(&self) -> usize {
self.twiddles.len() * 2
}
}
#[cfg(test)]
mod test {
use super::*;
use test_utils::{compare_float_vectors, random_signal};
use algorithm::{Type2And3Naive, Type4Naive};
#[test]
fn unittest_dct4_via_type3() {
for inner_size in 1..20 {
let size = inner_size * 2;
let mut expected_input = random_signal(size);
let mut actual_input = expected_input.clone();
let mut expected_output = vec![0f32; size];
let mut actual_output = vec![0f32; size];
let mut naive_dct4 = Type4Naive::new(size);
naive_dct4.process_dct4(&mut expected_input, &mut expected_output);
let inner_dct3 = Arc::new(Type2And3Naive::new(inner_size));
let mut dct = Type4ConvertToType3Even::new(inner_dct3);
dct.process_dct4(&mut actual_input, &mut actual_output);
println!("");
println!("expected: {:?}", expected_output);
println!("actual: {:?}", actual_output);
assert!(
compare_float_vectors(&expected_output, &actual_output),
"len = {}",
size
);
}
}
#[test]
fn unittest_dst4_via_type3() {
for inner_size in 1..20 {
let size = inner_size * 2;
let mut expected_input = random_signal(size);
let mut actual_input = expected_input.clone();
let mut expected_output = vec![0f32; size];
let mut actual_output = vec![0f32; size];
let mut naive_dst4 = Type4Naive::new(size);
naive_dst4.process_dst4(&mut expected_input, &mut expected_output);
let inner_dct3 = Arc::new(Type2And3Naive::new(inner_size));
let mut dct = Type4ConvertToType3Even::new(inner_dct3);
dct.process_dst4(&mut actual_input, &mut actual_output);
println!("");
println!("expected: {:?}", expected_output);
println!("actual: {:?}", actual_output);
assert!(
compare_float_vectors(&expected_output, &actual_output),
"len = {}",
size
);
}
}
}