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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use std::sync::Arc;

use rustfft::num_traits::Zero;
use rustfft::num_complex::Complex;
use rustfft::{FFT, Length};

use common;
use ::{DCT4, DST4, TransformType4};

/// DCT Type 4 and DST Type 4 implementation that converts the problem into a FFT of the same size. 
///
/// This algorithm can only be used if the problem size is odd.
///
/// ~~~
/// // Computes a DCT Type 4 and DST Type 4 of size 1233
/// use rustdct::{DCT4, DST4};
/// use rustdct::algorithm::Type4ConvertToFFTOdd;
/// use rustdct::rustfft::FFTplanner;
/// 
/// let len = 1233;
/// let mut planner = FFTplanner::new(false);
/// let fft = planner.plan_fft(len);
/// let dct = Type4ConvertToFFTOdd::new(fft);
///
/// let mut dct4_input:  Vec<f32> = vec![0f32; len];
/// let mut dct4_output: Vec<f32> = vec![0f32; len];
/// dct.process_dct4(&mut dct4_input, &mut dct4_output);
/// 
/// let mut dst4_input:  Vec<f32> = vec![0f32; len];
/// let mut dst4_output: Vec<f32> = vec![0f32; len];
/// dct.process_dst4(&mut dst4_input, &mut dst4_output);
/// ~~~
pub struct Type4ConvertToFFTOdd<T> {
    fft: Arc<FFT<T>>,
}

impl<T: common::DCTnum> Type4ConvertToFFTOdd<T> {
    /// Creates a new DCT4 context that will process signals of length `inner_fft.len()`. `inner_fft.len()` must be odd.
    pub fn new(inner_fft: Arc<FFT<T>>) -> Self {
        assert!(
            !inner_fft.is_inverse(),
            "Type4ConvertToFFTOdd requires a forward FFT, but an inverse FFT was provided");

        let len = inner_fft.len();

        assert!(len % 2 == 1, "Type4ConvertToFFTOdd size must be odd. Got {}", len);

        Type4ConvertToFFTOdd {
            fft: inner_fft,
        }
    }
}

impl<T: common::DCTnum> DCT4<T> for Type4ConvertToFFTOdd<T> {
    fn process_dct4(&self, input: &mut [T], output: &mut [T]) {
        common::verify_length(input, output, self.len());

        let len = self.len();
        let half_len = len / 2;
        let quarter_len = len / 4;

        let mut buffer = vec![Complex::zero(); len * 2];
        let (mut fft_input, mut fft_output) = buffer.split_at_mut(len);

        //start by reordering the input into the FFT input
        let mut input_index = half_len;
        let mut fft_index = 0;
        while input_index < len {
            fft_input[fft_index] = Complex{ re: input[input_index], im: T::zero() };

            input_index += 4;
            fft_index += 1;
        }

        //subtract len to simulate modular arithmetic
        input_index = input_index - len;
        while input_index < len {
            fft_input[fft_index] = Complex{ re: -input[len - input_index - 1], im: T::zero() };

            input_index += 4;
            fft_index += 1;
        }

        input_index = input_index - len;
        while input_index < len {
            fft_input[fft_index] = Complex{ re: -input[input_index], im: T::zero() };

            input_index += 4;
            fft_index += 1;
        }

        input_index = input_index - len;
        while input_index < len {
            fft_input[fft_index] = Complex{ re: input[len - input_index - 1], im: T::zero() };

            input_index += 4;
            fft_index += 1;
        }

        input_index = input_index - len;
        while fft_index < len {
            fft_input[fft_index] = Complex{ re: input[input_index], im: T::zero() };

            input_index += 4;
            fft_index += 1;
        }

        // run the fft
        self.fft.process(&mut fft_input, &mut fft_output);

        let result_scale = T::SQRT_2() * T::half();
        let second_half_sign = if len % 4 == 1 { T::one() } else { -T::one() };

        //post-process the results 4 at a time
        let mut output_sign = T::one();
        for i in 0..quarter_len {
            let fft_result = fft_output[4 * i + 1] * (output_sign * result_scale);
            let next_result = fft_output[4 * i + 3] * (output_sign * result_scale);

            output[i * 2] =           fft_result.re + fft_result.im;
            output[i * 2 + 1] =      -next_result.re + next_result.im;

            output[len - i * 2 - 2] = (next_result.re + next_result.im) * second_half_sign;
            output[len - i * 2 - 1] = (fft_result.re - fft_result.im) * second_half_sign;

            output_sign = output_sign.neg();
        }

        //we either have 1 or 3 elements left over that we couldn't get in the above loop, handle them here
        if len % 4 == 1 {
            output[half_len] = fft_output[0].re * output_sign * result_scale;
        }
        else {
            let fft_result = fft_output[len - 2] * (output_sign * result_scale);

            output[half_len - 1] =  fft_result.re + fft_result.im;
            output[half_len + 1] = -fft_result.re + fft_result.im;
            output[half_len] =     -fft_output[0].re * output_sign * result_scale;
        }
        
    }
}
impl<T: common::DCTnum> DST4<T> for Type4ConvertToFFTOdd<T> {
    fn process_dst4(&self, input: &mut [T], output: &mut [T]) {
        common::verify_length(input, output, self.len());

        self.process_dct4(input, output);

        let len = self.len();
        let half_len = len / 2;
        let quarter_len = len / 4;

        let mut buffer = vec![Complex::zero(); len * 2];
        let (mut fft_input, mut fft_output) = buffer.split_at_mut(len);

        //start by reordering the input into the FFT input
        let mut input_index = half_len;
        let mut fft_index = 0;
        while input_index < len {
            fft_input[fft_index] = Complex{ re: input[len - input_index - 1], im: T::zero() };

            input_index += 4;
            fft_index += 1;
        }

        //subtract len to simulate modular arithmetic
        input_index = input_index - len;
        while input_index < len {
            fft_input[fft_index] = Complex{ re: -input[input_index], im: T::zero() };

            input_index += 4;
            fft_index += 1;
        }

        input_index = input_index - len;
        while input_index < len {
            fft_input[fft_index] = Complex{ re: -input[len - input_index - 1], im: T::zero() };

            input_index += 4;
            fft_index += 1;
        }

        input_index = input_index - len;
        while input_index < len {
            fft_input[fft_index] = Complex{ re: input[input_index], im: T::zero() };

            input_index += 4;
            fft_index += 1;
        }

        input_index = input_index - len;
        while fft_index < len {
            fft_input[fft_index] = Complex{ re: input[len - input_index - 1], im: T::zero() };

            input_index += 4;
            fft_index += 1;
        }

        // run the fft
        self.fft.process(&mut fft_input, &mut fft_output);

        let result_scale = T::SQRT_2() * T::half();
        let second_half_sign = if len % 4 == 1 { T::one() } else { -T::one() };

        //post-process the results 4 at a time
        let mut output_sign = T::one();
        for i in 0..quarter_len {
            let fft_result = fft_output[4 * i + 1] * (output_sign * result_scale);
            let next_result = fft_output[4 * i + 3] * (output_sign * result_scale);

            output[i * 2] =           fft_result.re + fft_result.im;
            output[i * 2 + 1] =       next_result.re - next_result.im;

            output[len - i * 2 - 2] = -(next_result.re + next_result.im) * second_half_sign;
            output[len - i * 2 - 1] = (fft_result.re - fft_result.im) * second_half_sign;

            output_sign = output_sign.neg();
        }

        //we either have 1 or 3 elements left over that we couldn't get in the above loop, handle them here
        if len % 4 == 1 {
            output[half_len] = fft_output[0].re * output_sign * result_scale;
        }
        else {
            let fft_result = fft_output[len - 2] * (output_sign * result_scale);

            output[half_len - 1] =  fft_result.re + fft_result.im;
            output[half_len + 1] = -fft_result.re + fft_result.im;
            output[half_len] =      fft_output[0].re * output_sign * result_scale;
        }
        
    }
}
impl<T: common::DCTnum> TransformType4<T> for Type4ConvertToFFTOdd<T>{}
impl<T> Length for Type4ConvertToFFTOdd<T> {
    fn len(&self) -> usize {
        self.fft.len()
    }
}


#[cfg(test)]
mod test {
    use super::*;
    use algorithm::Type4Naive;

    use test_utils::{compare_float_vectors, random_signal};
    use rustfft::FFTplanner;

    /// Verify that our fast implementation of the DCT4 gives the same output as the slow version, for many different inputs
    #[test]
    fn test_dct4_via_fft_odd() {
        for n in 0..50 {
            let size = 2 * n + 1;
            println!("{}", size);

            let mut expected_input = random_signal(size);
            let mut actual_input = expected_input.clone();

            println!("input: {:?}", actual_input);

            let mut expected_output = vec![0f32; size];
            let mut actual_output = vec![0f32; size];

            let mut naive_dct = Type4Naive::new(size);
            naive_dct.process_dct4(&mut expected_input, &mut expected_output);

            let mut fft_planner = FFTplanner::new(false);
            let mut dct = Type4ConvertToFFTOdd::new(fft_planner.plan_fft(size));
            dct.process_dct4(&mut actual_input, &mut actual_output);

            println!("expected: {:?}", expected_output);
            println!("actual: {:?}", actual_output);

            assert!(
                compare_float_vectors(&actual_output, &expected_output),
                "len = {}",
                size
            );
        }
    }

    /// Verify that our fast implementation of the DST4 gives the same output as the slow version, for many different inputs
    #[test]
    fn test_dst4_via_fft_odd() {
        for n in 0..50 {
            let size = 2 * n + 1;
            println!("{}", size);

            let mut expected_input = random_signal(size);
            let mut actual_input = expected_input.clone();

            println!("input: {:?}", actual_input);

            let mut expected_output = vec![0f32; size];
            let mut actual_output = vec![0f32; size];

            let mut naive_dct = Type4Naive::new(size);
            naive_dct.process_dst4(&mut expected_input, &mut expected_output);

            let mut fft_planner = FFTplanner::new(false);
            let mut dct = Type4ConvertToFFTOdd::new(fft_planner.plan_fft(size));
            dct.process_dst4(&mut actual_input, &mut actual_output);

            println!("expected: {:?}", expected_output);
            println!("actual: {:?}", actual_output);

            assert!(
                compare_float_vectors(&actual_output, &expected_output),
                "len = {}",
                size
            );
        }
    }
}