Skip to content

Commit

Permalink
Fixed mapping for negative values when saving as PNG
Browse files Browse the repository at this point in the history
  • Loading branch information
martinber committed Sep 4, 2018
1 parent f800a52 commit ec6c79d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/dsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub type Signal = Vec<f32>;

/// Get biggest sample in signal.
pub fn get_max(vector: &Signal) -> &f32 {
let mut max: &f32 = &0_f32;
let mut max: &f32 = &vector[0];
for sample in vector.iter() {
if sample > max {
max = sample;
Expand All @@ -16,6 +16,18 @@ pub fn get_max(vector: &Signal) -> &f32 {
max
}

/// Get smallest sample in signal.
pub fn get_min(vector: &Signal) -> &f32 {
let mut min: &f32 = &vector[0];
for sample in vector.iter() {
if sample < min {
min = sample;
}
}

min
}

/// Resample signal to given rate, using the default filter.
///
/// The filter has a transition band equal to the 20% of the spectrum width of
Expand Down
6 changes: 4 additions & 2 deletions src/noaa_apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,12 @@ pub fn decode(input_filename: &str, output_filename: &str) -> err::Result<()>{

let aligned = dsp::resample_to(&aligned, WORK_RATE, FINAL_RATE);
let max = dsp::get_max(&aligned);
let min = dsp::get_min(&aligned);
let range = max - min;

debug!("Mapping samples from 0-{} to 0-255", max);
debug!("Mapping samples from {}-{} to 0-255", min, max);

let aligned: Vec<u8> = aligned.iter().map(|x| (x/max*255.) as u8).collect();
let aligned: Vec<u8> = aligned.iter().map(|x| ((x-min)/range*255.) as u8).collect();

info!("Writing PNG to '{}'", output_filename);

Expand Down

0 comments on commit ec6c79d

Please sign in to comment.