Skip to content

Commit

Permalink
Cleaning up vars, loops, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrolo committed Oct 18, 2023
1 parent ac5c2d8 commit 9bb7fa2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
4 changes: 2 additions & 2 deletions brro-compressor/src/compressor/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
45 changes: 22 additions & 23 deletions brro-compressor/src/compressor/fft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,18 @@ 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
min_value: FFT::f64_to_f32(min),
}
}

// 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()) {
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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!");
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions brro-compressor/src/compressor/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
12 changes: 6 additions & 6 deletions brro-compressor/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,15 +23,15 @@ impl CompressorFrame {
pub fn new(provided_compressor: Option<Compressor>) -> Self {
CompressorFrame {
frame_size: 0,
samples: 0,
sample_count: 0,
compressor: provided_compressor.unwrap_or_default(),
data: Vec::new() }
}

/// 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);
Expand All @@ -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<f64> {
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)
}

}
2 changes: 1 addition & 1 deletion brro-compressor/src/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>, vec2: &Vec<f64>) -> Option<f64> {
pub fn calculate_error(vec1: &[f64], vec2: &Vec<f64>) -> Option<f64> {
if vec1.len() != vec2.len() {
return None;
}
Expand Down

0 comments on commit 9bb7fa2

Please sign in to comment.