This commit is contained in:
2024-07-03 09:12:31 +08:00
commit d761f91771
70 changed files with 17366 additions and 0 deletions

View File

@ -0,0 +1,28 @@
mod smoothmethod;
mod spectraltools;
mod sharpmethod;
#[tauri::command]
pub fn interpolate_spline(x: Vec<f64>, y: Vec<f64>, step: f64) ->Vec<(f64, f64)>{
spectraltools::interpolate_spline(x, y, step).unwrap()
}
#[tauri::command]
pub fn interpolate_spline_at_points(x: Vec<f64>, y: Vec<f64>, x_target: Vec<f64>) -> Vec<f64>{
spectraltools::interpolate_spline_at_points(x, y, x_target).unwrap()
}
#[tauri::command]
pub fn sg_smooth(data: Vec<f64>, window: usize, order: usize) -> Vec<f64> {
smoothmethod::savgol(data, window, order)
}
#[tauri::command]
pub fn Gaussian_filter_high(data: Vec<f64>, sigma: f64) -> Vec<f64> {
sharpmethod::high_pass_gaussian_filter(data, sigma)
}
#[tauri::command]
pub fn find_peek(data: Vec<f64>, minheigh: f64) -> Vec<(u32, f64)> {
spectraltools::find_peek(data, minheigh)
}

View File

@ -0,0 +1,42 @@
extern crate ndarray;
extern crate ndarray_ndimage;
use ndarray::prelude::*;
use ndarray_ndimage::{gaussian_filter, BorderMode};
pub fn high_pass_gaussian_filter(input: Vec<f64>, sigma: f64) -> Vec<f64> {
// 将输入 Vec<f64> 转换为 Array1<f64>
let mut input_array = Array1::from_vec(input);
// for i in 0..input_array.len(){
//
// input_array[i]=input_array[i]*input_array[i]/( 65535f64);
// }
// return input_array.to_vec();
// 高斯低通滤波
let mut low_pass = gaussian_filter(&input_array, sigma, 0, BorderMode::Reflect, 3);
// Modify the result: set values less than zero to zero
println!("{:?}",low_pass);
// 高通滤波:原始信号 - 低通滤波结果
let mut addarry=&input_array - &low_pass;
for i in 0..addarry.len(){
if addarry[i] < 0.0 {
addarry[i] = 0.0;
}
}
let high_pass =&input_array+ &addarry;
// 找到原始数据和锐化后数据的最大值
let max_original = input_array.iter().cloned().fold(f64::MIN, f64::max);
let max_sharpened = high_pass.iter().cloned().fold(f64::MIN, f64::max);
// 计算系数
let coefficient = max_original / max_sharpened;
// 应用系数并将输出 Array1<f64> 转换回 Vec<f64>
high_pass.map(|&x| x * coefficient).to_vec()
}

View File

@ -0,0 +1,23 @@
extern crate savgol_rs;
use savgol_rs::savgol_filter;
pub fn savgol(data: Vec<f64>, window: usize, order: usize) -> Vec<f64> {
let svinput= savgol_rs::SavGolInput{data:&data,window_length:window,poly_order:order,derivative:0};
savgol_filter(&svinput).unwrap()
}
#[test]
fn test_savgol() {
// 示例数据
let data = vec![1.0, 1.9, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0];
let window = 5;
let order = 2;
// 调用 savgol 函数
let smoothed_data = savgol(data.clone(), window, order);
println!("Smoothed data: {:?}", smoothed_data);
}

View 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);
}
}