pounder_test/dsp/build.rs

47 lines
1.4 KiB
Rust
Raw Normal View History

2020-12-11 00:17:09 +08:00
use std::env;
2020-12-10 07:53:56 +08:00
use std::f64::consts::PI;
2020-12-08 02:22:09 +08:00
use std::fs::File;
use std::io::prelude::*;
2020-12-10 07:53:56 +08:00
use std::path::Path;
2020-12-08 02:22:09 +08:00
fn write_cossin_table() {
const DEPTH: usize = 7;
2020-12-08 02:22:09 +08:00
2020-12-11 00:17:09 +08:00
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("cossin_table.rs");
let mut file = File::create(dest_path).unwrap();
writeln!(file, "pub(crate) const COSSIN_DEPTH: usize = {};", DEPTH)
.unwrap();
write!(
file,
"pub(crate) const COSSIN: [u32; 1 << COSSIN_DEPTH] = ["
)
.unwrap();
2020-12-08 02:22:09 +08:00
// Treat sin and cos as unsigned values since the sign will always be
// positive in the range [0, pi/4).
// No headroom for interpolation rounding error (this is needed for
// DEPTH = 6 for example).
const AMPLITUDE: f64 = u16::MAX as f64;
2020-12-08 02:22:09 +08:00
for i in 0..(1 << DEPTH) {
// use midpoint samples to save one entry in the LUT
let phase = (PI / 4. / (1 << DEPTH) as f64) * (i as f64 + 0.5);
// add one bit accuracy to cos due to 0.5 < cos(z) <= 1 for |z| < pi/4
let cos = ((phase.cos() - 0.5) * 2. * AMPLITUDE).round() as u32 - 1;
let sin = (phase.sin() * AMPLITUDE).round() as u32;
if i % 4 == 0 {
write!(file, "\n ").unwrap();
2020-12-08 02:22:09 +08:00
}
write!(file, " {},", cos + (sin << 16)).unwrap();
2020-12-08 02:22:09 +08:00
}
writeln!(file, "\n];").unwrap();
2020-12-11 00:17:59 +08:00
println!("cargo:rerun-if-changed=build.rs");
}
2020-12-08 02:22:09 +08:00
fn main() {
write_cossin_table();
2020-12-08 02:22:09 +08:00
}