diff --git a/src/main.rs b/src/main.rs index 4c945e5..6a72552 100644 --- a/src/main.rs +++ b/src/main.rs @@ -779,7 +779,11 @@ const APP: () = { let x0 = f32::from(a as i16); let y0 = c.resources.iir_ch[1].update(&mut c.resources.iir_state[1], x0); - y0 as i16 as u16 ^ 0x8000 + // note(unsafe): The filter limits ensure that the value is in range. + // The truncation introduces 1/2 LSB distortion. + let y0 = unsafe { y0.to_int_unchecked::() }; + // convert to DAC code + y0 as u16 ^ 0x8000 }; c.resources.dac1.send(output).unwrap(); @@ -792,7 +796,11 @@ const APP: () = { let x0 = f32::from(a as i16); let y0 = c.resources.iir_ch[0].update(&mut c.resources.iir_state[0], x0); - y0 as i16 as u16 ^ 0x8000 + // note(unsafe): The filter limits ensure that the value is in range. + // The truncation introduces 1/2 LSB distortion. + let y0 = unsafe { y0.to_int_unchecked::() }; + // convert to DAC code + y0 as u16 ^ 0x8000 }; c.resources.dac0.send(output).unwrap();