first
This commit is contained in:
140
src-tauri/src/algorithm/spectraltools.rs
Normal file
140
src-tauri/src/algorithm/spectraltools.rs
Normal file
@ -0,0 +1,140 @@
|
||||
extern crate splines;
|
||||
|
||||
use splines::{Spline, Key, Interpolation};
|
||||
use std::error::Error;
|
||||
|
||||
use find_peaks::PeakFinder;
|
||||
|
||||
pub fn interpolate_spline<T: Copy + Into<f64>,>(x_T: Vec<T>, y_T: Vec<T>, step: f64) -> Result<Vec<(f64, f64)>, Box<dyn Error>> {
|
||||
let x: Vec<f64> = x_T.iter().map(|&x| x.into()).collect();
|
||||
let y: Vec<f64> = y_T.iter().map(|&y| y.into()).collect();
|
||||
|
||||
if x.len() != y.len() {
|
||||
return Err("x and y must have the same length".into());
|
||||
}
|
||||
|
||||
// 创建样条曲线
|
||||
let keys: Vec<Key<f64, f64>> = x.iter()
|
||||
.zip(y.iter())
|
||||
.map(|(&x, &y)| Key::new(x, y, Interpolation::Linear))
|
||||
.collect();
|
||||
|
||||
let spline = Spline::from_vec(keys);
|
||||
|
||||
// 计算 x 的最大值和最小值
|
||||
let &start = x.iter().min_by(|a, b| a.partial_cmp(b).unwrap()).unwrap();
|
||||
let &end = x.iter().max_by(|a, b| a.partial_cmp(b).unwrap()).unwrap();
|
||||
|
||||
// 插值到间隔为 step 的点
|
||||
let mut result = Vec::new();
|
||||
let mut t = start;
|
||||
|
||||
while t <= end {
|
||||
if let Some(value) = spline.clamped_sample(t) {
|
||||
result.push((t, value));
|
||||
}
|
||||
t += step;
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub fn interpolate_spline_at_points<T: Copy + Into<f64>>(x_T: Vec<T>, y_T: Vec<T>, x_target: Vec<f64>) -> Result<Vec<(f64)>, Box<dyn Error>> {
|
||||
let x: Vec<f64> = x_T.iter().map(|&x| x.into()).collect();
|
||||
let y: Vec<f64> = y_T.iter().map(|&y| y.into()).collect();
|
||||
|
||||
if x.len() != y.len() {
|
||||
return Err("x and y must have the same length".into());
|
||||
}
|
||||
|
||||
// 创建样条曲线
|
||||
let keys: Vec<Key<f64, f64>> = x.iter()
|
||||
.zip(y.iter())
|
||||
.map(|(&x, &y)| Key::new(x, y, Interpolation::Linear))
|
||||
.collect();
|
||||
|
||||
let spline = Spline::from_vec(keys);
|
||||
|
||||
// 插值到 x_target 指定的点
|
||||
let mut result = Vec::new();
|
||||
|
||||
for &t in x_target.iter() {
|
||||
if let Some(value) = spline.clamped_sample(t) {
|
||||
result.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
pub fn find_peek(data:Vec<f64>,minheigh:f64)->Vec<(u32,f64)>{
|
||||
let mut fp = PeakFinder::new(&data);
|
||||
fp.with_min_prominence(200.);
|
||||
fp.with_min_height(minheigh);
|
||||
let mut retvec=Vec::new();
|
||||
let peaks = fp.find_peaks();
|
||||
for p in peaks {
|
||||
// println!("{} {}", p.middle_position(), p.height.unwrap());
|
||||
retvec.push((p.middle_position().try_into().unwrap(),p.height.unwrap()));
|
||||
|
||||
}
|
||||
retvec
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn testinterpolate_spline() -> Result<(), Box<dyn Error>> {
|
||||
// 示例数据
|
||||
let x = vec![0.0,0.5, 0.569, 1.138, 1.707, 2.276, 2.845];
|
||||
let y = vec![0.0, 0.4,0.5, 1.0, 0.5, 0.0, -0.5];
|
||||
let step = 0.1;
|
||||
|
||||
// 调用插值函数
|
||||
let interpolated_values = interpolate_spline(x, y, step)?;
|
||||
|
||||
// 输出结果
|
||||
for (xi, yi) in interpolated_values {
|
||||
println!("x = {:.3}, y = {:.3}", xi, yi);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
#[test]
|
||||
fn tset_interpolate_spline_at_points() -> Result<(), Box<dyn Error>> {
|
||||
let x = vec![0.0,0.5, 0.569, 1.138, 1.707, 2.276, 2.845];
|
||||
let y = vec![0.0, 0.4,0.5, 1.0, 0.5, 0.0, -0.5];
|
||||
let x_target = vec![0.1, 0.2, 0.3];
|
||||
|
||||
let result = interpolate_spline_at_points(x, y, x_target)?;
|
||||
|
||||
for (yi) in result {
|
||||
println!("y = {:.3}", yi);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
use csv::ReaderBuilder;
|
||||
fn read_csv_to_vec(file_path: &str) -> Result<Vec<f64>, Box<dyn Error>> {
|
||||
let mut rdr = ReaderBuilder::new().from_path(file_path)?;
|
||||
let mut values = Vec::new();
|
||||
|
||||
for result in rdr.records() {
|
||||
let record = result?;
|
||||
if let Some(value) = record.get(1) {
|
||||
values.push(value.parse::<f64>()?);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_find_peek(){
|
||||
let data = read_csv_to_vec("D:\\06Learn\\rust\\tarui\\myfirst_tauri\\src-tauri\\test0_UP.csv").unwrap();
|
||||
let peaks = find_peek(data,10000.0);
|
||||
for p in peaks {
|
||||
println!("{} {}", p.0, p.1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user