| 1 |
use crate::common::{compare_float_vectors, random_signal}; |
| 2 |
use rustdct::DctPlanner; |
| 3 |
|
| 4 |
macro_rules! dct_test_with_known_data { |
| 5 |
($reference_fn:ident, $naive_struct:ident, $process_fn: ident, $known_data_fn:ident) => ( |
| 6 |
// Compare our naive struct and our reference_fn implementation against a bunch of known data |
| 7 |
let known_data = $known_data_fn(); |
| 8 |
for entry in known_data { |
| 9 |
let len = entry.input.len(); |
| 10 |
assert_eq!(len, entry.expected_output.len(), "Invalid test data -- input and known output are not the same length"); |
| 11 |
|
| 12 |
let mut naive_buffer = entry.input.clone(); |
| 13 |
|
| 14 |
let naive_dct = $naive_struct::new(len); |
| 15 |
naive_dct.$process_fn(&mut naive_buffer); |
| 16 |
|
| 17 |
let slow_output = $reference_fn(&entry.input); |
| 18 |
|
| 19 |
println!("input: {:?}", entry.input); |
| 20 |
println!("expected output:{:?}", entry.expected_output); |
| 21 |
println!("naive output: {:?}", naive_buffer); |
| 22 |
println!("slow output: {:?}", slow_output); |
| 23 |
|
| 24 |
assert!(compare_float_vectors(&entry.expected_output, &naive_buffer)); |
| 25 |
assert!(compare_float_vectors(&entry.expected_output, &slow_output)); |
| 26 |
} |
| 27 |
) |
| 28 |
} |
| 29 |
|
| 30 |
macro_rules! dct_test_inverse { |
| 31 |
($reference_fn:ident, $inverse_fn:ident, $inverse_scale_fn:ident, $first_size:expr) => ( |
| 32 |
// Test that the slow fn, paired with the correct inverse fn, actually yields the original data |
| 33 |
for len in $first_size..20 { |
| 34 |
let input = random_signal(len); |
| 35 |
let intermediate = $reference_fn(&input); |
| 36 |
let inverse = $inverse_fn(&intermediate); |
| 37 |
|
| 38 |
let inverse_scale = $inverse_scale_fn(len); |
| 39 |
let scaled_inverse: Vec<f64> = inverse.into_iter().map(|entry| entry * inverse_scale).collect(); |
| 40 |
|
| 41 |
println!("input: {:?}", input); |
| 42 |
println!("scaled inverse: {:?}", scaled_inverse); |
| 43 |
|
| 44 |
assert!(compare_float_vectors(&input, &scaled_inverse)); |
| 45 |
} |
| 46 |
) |
| 47 |
} |
| 48 |
|
| 49 |
macro_rules! dct_test_with_planner { |
| 50 |
($reference_fn:ident, $naive_struct:ident, $process_fn: ident, $planner_fn:ident, $first_size:expr) => { |
| 51 |
// Compare our naive struct against the output from the planner |
| 52 |
for len in $first_size..20 { |
| 53 |
let input = random_signal(len); |
| 54 |
|
| 55 |
let mut naive_buffer = input.clone(); |
| 56 |
let mut actual_buffer = input.clone(); |
| 57 |
|
| 58 |
let naive_dct = $naive_struct::new(len); |
| 59 |
|
| 60 |
let mut planner = DctPlanner::new(); |
| 61 |
let actual_dct = planner.$planner_fn(len); |
| 62 |
|
| 63 |
assert_eq!( |
| 64 |
actual_dct.len(), |
| 65 |
len, |
| 66 |
"Planner created a DCT of incorrect length. Expected {}, got {}", |
| 67 |
len, |
| 68 |
actual_dct.len() |
| 69 |
); |
| 70 |
|
| 71 |
let reference_output = $reference_fn(&input); |
| 72 |
naive_dct.$process_fn(&mut naive_buffer); |
| 73 |
actual_dct.$process_fn(&mut actual_buffer); |
| 74 |
|
| 75 |
println!("input: {:?}", input); |
| 76 |
println!("reference output:{:?}", reference_output); |
| 77 |
println!("naive output: {:?}", naive_buffer); |
| 78 |
println!("planned output: {:?}", actual_buffer); |
| 79 |
|
| 80 |
assert!(compare_float_vectors(&reference_output, &naive_buffer)); |
| 81 |
assert!(compare_float_vectors(&reference_output, &actual_buffer)); |
| 82 |
} |
| 83 |
}; |
| 84 |
} |
| 85 |
|
| 86 |
pub mod test_mdct { |
| 87 |
use super::*; |
| 88 |
use rustdct::{ |
| 89 |
mdct::{Mdct, MdctNaive}, |
| 90 |
RequiredScratch, |
| 91 |
}; |
| 92 |
|
| 93 |
pub fn planned_matches_naive<F>(len: usize, window_fn: F) |
| 94 |
where |
| 95 |
F: Fn(usize) -> Vec<f32>, |
| 96 |
{ |
| 97 |
let input = random_signal(len * 2); |
| 98 |
println!("input: {:?}", input); |
| 99 |
|
| 100 |
let (input_a, input_b) = input.split_at(len); |
| 101 |
|
| 102 |
let mut naive_output = vec![0f32; len]; |
| 103 |
let mut actual_output = vec![0f32; len]; |
| 104 |
|
| 105 |
let naive_dct = MdctNaive::new(len, &window_fn); |
| 106 |
|
| 107 |
let mut planner = DctPlanner::new(); |
| 108 |
let actual_dct = planner.plan_mdct(len, window_fn); |
| 109 |
|
| 110 |
assert_eq!( |
| 111 |
actual_dct.len(), |
| 112 |
len, |
| 113 |
"Planner created a DCT of incorrect length" |
| 114 |
); |
| 115 |
|
| 116 |
let mut naive_scratch = vec![0f32; naive_dct.get_scratch_len()]; |
| 117 |
let mut fast_scratch = vec![0f32; actual_dct.get_scratch_len()]; |
| 118 |
|
| 119 |
naive_dct.process_mdct_with_scratch( |
| 120 |
input_a, |
| 121 |
input_b, |
| 122 |
&mut naive_output, |
| 123 |
&mut naive_scratch, |
| 124 |
); |
| 125 |
actual_dct.process_mdct_with_scratch( |
| 126 |
input_a, |
| 127 |
input_b, |
| 128 |
&mut actual_output, |
| 129 |
&mut fast_scratch, |
| 130 |
); |
| 131 |
|
| 132 |
println!("Naive output: {:?}", naive_output); |
| 133 |
println!("Planned output: {:?}", actual_output); |
| 134 |
|
| 135 |
assert!( |
| 136 |
compare_float_vectors(&naive_output, &actual_output), |
| 137 |
"len = {}", |
| 138 |
len |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
pub fn test_tdac<F>(len: usize, scale_factor: f32, window_fn: F) |
| 143 |
where |
| 144 |
F: Fn(usize) -> Vec<f32>, |
| 145 |
{ |
| 146 |
let mut planner = DctPlanner::new(); |
| 147 |
let mdct = planner.plan_mdct(len, &window_fn); |
| 148 |
|
| 149 |
const NUM_SEGMENTS: usize = 5; |
| 150 |
|
| 151 |
let input = random_signal(len * (NUM_SEGMENTS + 1)); |
| 152 |
let mut output = vec![0f32; len * NUM_SEGMENTS]; |
| 153 |
let mut inverse = vec![0f32; len * (NUM_SEGMENTS + 1)]; |
| 154 |
let mut scratch = vec![0f32; mdct.get_scratch_len()]; |
| 155 |
|
| 156 |
for i in 0..NUM_SEGMENTS { |
| 157 |
let input_chunk = &input[len * i..(len * (i + 2))]; |
| 158 |
let output_chunk = &mut output[len * i..(len * (i + 1))]; |
| 159 |
|
| 160 |
let (input_a, input_b) = input_chunk.split_at(len); |
| 161 |
|
| 162 |
mdct.process_mdct_with_scratch(input_a, input_b, output_chunk, &mut scratch); |
| 163 |
} |
| 164 |
for i in 0..NUM_SEGMENTS { |
| 165 |
let input_chunk = &output[len * i..(len * (i + 1))]; |
| 166 |
let output_chunk = &mut inverse[len * i..(len * (i + 2))]; |
| 167 |
|
| 168 |
let (output_a, output_b) = output_chunk.split_at_mut(len); |
| 169 |
|
| 170 |
mdct.process_imdct_with_scratch(input_chunk, output_a, output_b, &mut scratch); |
| 171 |
} |
| 172 |
|
| 173 |
//we have to scale the inverse by 1/len |
| 174 |
for element in inverse.iter_mut() { |
| 175 |
*element = *element * scale_factor; |
| 176 |
} |
| 177 |
|
| 178 |
println!("scale: {:?}", scale_factor); |
| 179 |
println!("input: {:?}", &input[len..input.len() - len]); |
| 180 |
println!("inverse: {:?}", &inverse[len..input.len() - len]); |
| 181 |
|
| 182 |
assert!( |
| 183 |
compare_float_vectors( |
| 184 |
&input[len..input.len() - len], |
| 185 |
&inverse[len..inverse.len() - len], |
| 186 |
), |
| 187 |
"len = {}", |
| 188 |
len |
| 189 |
); |
| 190 |
} |
| 191 |
} |