(fins: &[f32], resolution: u8)
| 236 | } |
| 237 | |
| 238 | pub fn quantize_to_u8_bits(fins: &[f32], resolution: u8) -> Vec<Vec<u8>> { |
| 239 | let bits_per_value = resolution as usize; |
| 240 | let parts = 2_usize.pow(bits_per_value as u32); |
| 241 | let step = 2.0 / parts as f32; |
| 242 | let u8s_per_value = (fins.len() + 7) / 8; |
| 243 | let mut quantized: Vec<Vec<u8>> = vec![Vec::with_capacity(u8s_per_value); bits_per_value]; |
| 244 | |
| 245 | let mut current_u8s: Vec<u8> = vec![0; bits_per_value]; |
| 246 | let mut bit_index: usize = 0; |
| 247 | |
| 248 | for &f in fins { |
| 249 | let flags = to_float_flag(f, bits_per_value, step); |
| 250 | |
| 251 | for bit_position in 0..bits_per_value { |
| 252 | if flags[bit_position] { |
| 253 | current_u8s[bit_position] |= 1 << bit_index; |
| 254 | } |
| 255 | } |
| 256 | bit_index += 1; |
| 257 | |
| 258 | if bit_index == 8 { |
| 259 | for bit_position in 0..bits_per_value { |
| 260 | quantized[bit_position].push(current_u8s[bit_position]); |
| 261 | current_u8s[bit_position] = 0; |
| 262 | } |
| 263 | bit_index = 0; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Push remaining bits if not a multiple of 8 |
| 268 | if bit_index > 0 { |
| 269 | for bit_position in 0..bits_per_value { |
| 270 | quantized[bit_position].push(current_u8s[bit_position]); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | quantized |
| 275 | } |
| 276 | |
| 277 | #[derive(Debug, Clone)] |
| 278 | pub enum WaCustomError { |
no test coverage detected