diff --git a/brro-compressor/src/compressor/constant.rs b/brro-compressor/src/compressor/constant.rs index 5a68f10..1723d22 100644 --- a/brro-compressor/src/compressor/constant.rs +++ b/brro-compressor/src/compressor/constant.rs @@ -16,12 +16,12 @@ pub struct Constant { impl Constant { /// Creates a new instance of the Constant compressor with the size needed to handle the worst case - pub fn new(frame_size: usize) -> Self { + pub fn new(sample_count: usize) -> Self { debug!("Constant compressor"); Constant { id: CONSTANT_COMPRESSOR_ID, constant: 0, - residuals: Vec::with_capacity(frame_size), + residuals: Vec::with_capacity(sample_count), } } diff --git a/brro-compressor/src/compressor/fft.rs b/brro-compressor/src/compressor/fft.rs index 3c517ba..2e50a7a 100644 --- a/brro-compressor/src/compressor/fft.rs +++ b/brro-compressor/src/compressor/fft.rs @@ -92,11 +92,11 @@ pub struct FFT { impl FFT { /// Creates a new instance of the Constant compressor with the size needed to handle the worst case - pub fn new(samples: usize, min: f64, max: f64) -> Self { - debug!("FFT compressor"); + pub fn new(sample_count: usize, min: f64, max: f64) -> Self { + debug!("FFT compressor: min:{} max:{}", min, max); FFT { id: FFT_COMPRESSOR_ID, - frequencies: Vec::with_capacity(samples), + frequencies: Vec::with_capacity(sample_count), /// The maximum numeric value of the points in the frame max_value: FFT::f64_to_f32(max), /// The minimum numeric value of the points in the frame @@ -104,7 +104,6 @@ impl FFT { } } - // TODO: Move this to the optimizer? fn f64_to_f32(x: f64) -> f32 { let y = x as f32; if !(x.is_finite() && y.is_finite()) { @@ -179,46 +178,45 @@ impl FFT { /// NOTE: This does not otimize for smallest possible error, just being smaller than the error. pub fn compress_bounded(&mut self, data: &[f64], max_err: f64) { // Variables - let mut i = 1; - let v = data.len(); - let len = v as f32; - if !v.is_power_of_two() { + let len = data.len(); + let len_f32 = len as f32; + if !len.is_power_of_two() { warn!("Slow FFT, data segment is not a power of 2!"); } // Let's start from the defaults values for frequencies - let max_freq = if 3 >= (v/100) { 3 } else { v/100 }; + let max_freq = if 3 >= (len/100) { 3 } else { len/100 }; // Clean the data let mut buffer = FFT::optimize(data); // Create the FFT planners let mut planner = FftPlanner::new(); - let fft = planner.plan_fft_forward(v); - let ifft = planner.plan_fft_inverse(v); + let fft = planner.plan_fft_forward(len); + let ifft = planner.plan_fft_inverse(len); // FFT calculations fft.process(&mut buffer); self.frequencies = FFT::fft_trim(&mut buffer.clone(), max_freq); // Inverse FFT and error check - let mut idata = self.get_mirrored_freqs(v); + let mut idata = self.get_mirrored_freqs(len); // run the ifft ifft.process(&mut idata); let mut out_data = idata.iter() - .map(|&f| self.round(f.re/len, 1)) + .map(|&f| self.round(f.re/len_f32, 1)) .collect(); - let mut e = calculate_error(&data.to_vec(), &out_data).unwrap(); + let mut e = calculate_error(data, &out_data).unwrap(); // Repeat until the error is good enough - while e > max_err { - println!("Error: {}", e); + for i in 1..data.len() { + if e <= max_err { + break; + } + debug!("Error: {}", e); self.frequencies = FFT::fft_trim(&mut buffer.clone(), max_freq+i); - idata = self.get_mirrored_freqs(v); + idata = self.get_mirrored_freqs(len); ifft.process(&mut idata); out_data = idata.iter() - .map(|&f| self.round(f.re/len, 1)) + .map(|&f| self.round(f.re/len_f32, 1)) .collect(); - e = calculate_error(&data.to_vec(), &out_data).unwrap(); - i += 1; - // We can't have more frequencies - if i > v { break; } + e = calculate_error(data, &out_data).unwrap(); } } @@ -228,6 +226,7 @@ impl FFT { // First thing, always try to get the data len as a power of 2. let v = data.len(); let max_freq = if 3 >= (v/100) { 3 } else { v/100 }; + debug!("Setting max_freq count to: {}", max_freq); if !v.is_power_of_two() { warn!("Slow FFT, data segment is not a power of 2!"); } @@ -357,7 +356,7 @@ mod tests { } #[test] - fn test_to_lossess_data() { + fn test_to_lossless_data() { let vector1 = vec![1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 3.0, 1.0, 1.0, 5.0]; let compressed_data = fft_set(&vector1, 12); let out = fft_to_data(vector1.len(), &compressed_data); diff --git a/brro-compressor/src/compressor/noop.rs b/brro-compressor/src/compressor/noop.rs index f98cb69..e2b8b91 100644 --- a/brro-compressor/src/compressor/noop.rs +++ b/brro-compressor/src/compressor/noop.rs @@ -11,11 +11,11 @@ pub struct Noop { } impl Noop { - pub fn new(frame_size: usize) -> Self { + pub fn new(sample_count: usize) -> Self { debug!("Noop compressor"); Noop { id: NOOP_COMPRESSOR_ID, - data: Vec::with_capacity(frame_size), + data: Vec::with_capacity(sample_count), } } diff --git a/brro-compressor/src/frame/mod.rs b/brro-compressor/src/frame/mod.rs index b8a9664..63f39de 100644 --- a/brro-compressor/src/frame/mod.rs +++ b/brro-compressor/src/frame/mod.rs @@ -9,7 +9,7 @@ pub struct CompressorFrame{ /// The frame size in bytes, frame_size: usize, /// The number of samples in this frame, - samples: usize, + sample_count: usize, /// The compressor used in the current frame compressor: Compressor, /// Output from the compressor @@ -23,7 +23,7 @@ impl CompressorFrame { pub fn new(provided_compressor: Option) -> Self { CompressorFrame { frame_size: 0, - samples: 0, + sample_count: 0, compressor: provided_compressor.unwrap_or_default(), data: Vec::new() } } @@ -31,7 +31,7 @@ impl CompressorFrame { /// Calculates the size of the Frame and "closes it" // TODO this is probably wrong, so we have to use the write stream to dump the bytes writen pub fn close(&mut self) { - let size = size_of_val(&self.samples) + let size = size_of_val(&self.sample_count) + size_of_val(&self.compressor) + size_of_val(&self.data) + size_of_val(&self.frame_size); @@ -42,14 +42,14 @@ impl CompressorFrame { pub fn compress(&mut self, data: &[f64]) { // TODO: Optimize here // self.compressor = optimizer_selection - self.samples = data.len(); + self.sample_count = data.len(); self.data = self.compressor.compress(data); } /// Decompresses a frame and returns the resulting data array pub fn decompress(&self) -> Vec { - debug!("Decompressing Frame. Size: {}, Samples: {}", self.frame_size, self.samples); - self.compressor.decompress(self.samples, &self.data) + debug!("Decompressing Frame. Size: {}, Samples: {}", self.frame_size, self.sample_count); + self.compressor.decompress(self.sample_count, &self.data) } } \ No newline at end of file diff --git a/brro-compressor/src/utils/error.rs b/brro-compressor/src/utils/error.rs index 784b458..744955a 100644 --- a/brro-compressor/src/utils/error.rs +++ b/brro-compressor/src/utils/error.rs @@ -10,7 +10,7 @@ use std::cmp; /// # Returns /// /// The mean squared error, or an error message if the vector lengths are different. -pub fn calculate_error(vec1: &Vec, vec2: &Vec) -> Option { +pub fn calculate_error(vec1: &[f64], vec2: &Vec) -> Option { if vec1.len() != vec2.len() { return None; }