Skip to content
Snippets Groups Projects
Commit e36fd54a authored by Udo Eisenbarth's avatar Udo Eisenbarth :speech_balloon:
Browse files

implement calculation of bucket ratio

parent c7353d0a
No related branches found
No related tags found
No related merge requests found
...@@ -105,10 +105,20 @@ impl Spectrum { ...@@ -105,10 +105,20 @@ impl Spectrum {
.map(|x| x.0) .map(|x| x.0)
.collect::<Vec<usize>>(); .collect::<Vec<usize>>();
if res.is_empty() { if res.is_empty() {
let mut lower_idx = self.clone().lambdas.into_iter().position(|x| x >= x_lower).unwrap(); let mut lower_idx = self
let upper_idx = self.clone().lambdas.into_iter().position(|x| x >= x_upper).unwrap(); .clone()
if lower_idx>0 && self.lambdas[lower_idx]> x_lower { .lambdas
lower_idx-=1; .into_iter()
.position(|x| x >= x_lower)
.unwrap();
let upper_idx = self
.clone()
.lambdas
.into_iter()
.position(|x| x >= x_upper)
.unwrap();
if lower_idx > 0 && self.lambdas[lower_idx] > x_lower {
lower_idx -= 1;
} }
res = vec![lower_idx, upper_idx]; res = vec![lower_idx, upper_idx];
} else { } else {
...@@ -123,6 +133,8 @@ impl Spectrum { ...@@ -123,6 +133,8 @@ impl Spectrum {
} }
res res
} }
pub fn resample(&mut self, spectrum: &Spectrum) -> Result<()> { pub fn resample(&mut self, spectrum: &Spectrum) -> Result<()> {
let _data = spectrum.data.clone(); let _data = spectrum.data.clone();
let _x = spectrum.lambdas.clone(); let _x = spectrum.lambdas.clone();
...@@ -138,7 +150,6 @@ impl Spectrum { ...@@ -138,7 +150,6 @@ impl Spectrum {
Ok(()) Ok(())
} }
} }
impl Display for Spectrum { impl Display for Spectrum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let fmt_length = Length::format_args(nanometer, Abbreviation); let fmt_length = Length::format_args(nanometer, Abbreviation);
...@@ -155,6 +166,26 @@ impl Display for Spectrum { ...@@ -155,6 +166,26 @@ impl Display for Spectrum {
} }
} }
fn calc_ratio(bucket_left: f64, bucket_right: f64, source_left: f64, source_right: f64) -> f64 {
if bucket_left < source_left && bucket_right > source_left && bucket_right < source_right {
// bucket is left partly outside source
return (bucket_right - source_left) / (source_right - source_left);
}
if bucket_left <= source_left && bucket_right >= source_right {
// bucket contains source
return 1.0;
}
if bucket_left > source_left && bucket_right < source_right {
// bucket is part of source
return (bucket_right - bucket_left) / (source_right - source_left);
}
if bucket_left > source_left && bucket_left < source_right && bucket_right > source_right {
// bucket is right partly outside source
return (source_right - bucket_left) / (source_right - source_left);
}
return 0.0;
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
...@@ -350,22 +381,34 @@ mod test { ...@@ -350,22 +381,34 @@ mod test {
let l = v.into_iter().map(|i| s.lambdas[i]).collect::<Vec<f64>>(); let l = v.into_iter().map(|i| s.lambdas[i]).collect::<Vec<f64>>();
assert_eq!(l, vec![2.0, 3.0]); assert_eq!(l, vec![2.0, 3.0]);
} }
// #[test] #[test]
// fn resample() { fn calc_ratio_test() {
// let mut s1 = Spectrum::new( assert_eq!(calc_ratio(1.0,2.0,3.0,4.0),0.0); // bucket completely outside
// Length::new::<meter>(1.0)..Length::new::<meter>(5.0), assert_eq!(calc_ratio(1.0, 4.0, 2.0, 3.0),1.0); // bucket contains source
// Length::new::<meter>(1.0), assert_eq!(calc_ratio(2.0,3.0,0.0,4.0),0.25); // bucket is part of source
// ) assert_eq!(calc_ratio(0.0,2.0,1.0,3.0),0.5); // bucket is left outside source
// .unwrap(); assert_eq!(calc_ratio(0.0,2.0,1.0,2.0),1.0); // bucket is left outside source (matching)
// s1.set_single_peak(Length::new::<meter>(2.0), 1.0).unwrap(); assert_eq!(calc_ratio(2.0,4.0, 1.0,3.0),0.5); // bucket is right outside source
// let s2 = Spectrum::new( assert_eq!(calc_ratio(1.0,4.0, 1.0,3.0),1.0); // bucket is right outside source (matching)
// Length::new::<meter>(1.0)..Length::new::<meter>(5.0), assert_eq!(calc_ratio(1.0,2.0,1.0,2.0), 1.0); // bucket matches source
// Length::new::<meter>(1.0),
// ) }
// .unwrap(); #[test]
// s1.resample(&s2).unwrap(); fn resample() {
// assert_eq!(s1.data, s2.data); let mut s1 = Spectrum::new(
// } Length::new::<meter>(1.0)..Length::new::<meter>(5.0),
Length::new::<meter>(1.0),
)
.unwrap();
s1.set_single_peak(Length::new::<meter>(2.0), 1.0).unwrap();
let s2 = Spectrum::new(
Length::new::<meter>(1.0)..Length::new::<meter>(5.0),
Length::new::<meter>(1.0),
)
.unwrap();
s1.resample(&s2).unwrap();
assert_eq!(s1.data, s2.data);
}
#[test] #[test]
fn resample_interp() { fn resample_interp() {
let mut s1 = Spectrum::new( let mut s1 = Spectrum::new(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment