Started to turn the crate into a library

This commit is contained in:
Sebastian 2023-06-29 17:59:10 +02:00
parent eb6f22be87
commit c42589df3c
2 changed files with 33 additions and 23 deletions

18
examples/filter.rs Normal file
View File

@ -0,0 +1,18 @@
use firls_rs::firls;
fn main() {
let coeffs = firls(
13,
8000.0,
&vec![(0.0, 1.0), (1900.0, 1.0), (3000.0, 0.0), (4000.0, 0.0)],
)
.unwrap();
println!("[");
for coeff in coeffs.iter() {
println!("{},", coeff)
}
println!("]");
println!("Lenght: {}", coeffs.len());
}

View File

@ -2,35 +2,28 @@ use std::println;
use nalgebra::base::{DMatrix, DVector};
fn main() {
let num_coeffs = 6;
pub fn firls(lenght: usize, f_samp: f32, points: &Vec<(f32, f32)>) -> Result<Vec<f32>, &str> {
if lenght % 2 != 1 {
return Result::Err("Filter should have an odd length");
}
let response = generate_frequency_response(
8000.0,
&vec![(0.0, 1.0), (1900.0, 1.0), (3000.0, 0.0), (4000.0, 0.0)],
128,
);
println!("{:?}", response);
let half_lenght = lenght / 2 + 1;
let matrix = generate_design_matrix(num_coeffs, 128);
println!("{} {}", matrix.ncols(), matrix.nrows());
let response = generate_frequency_response(f_samp, points, 2 * lenght);
let matrix = generate_design_matrix(half_lenght, 2 * lenght);
let pseudo_inverse = matrix.pseudo_inverse(0.0).unwrap();
let half_coeffs = pseudo_inverse * response;
println!("{} {}", pseudo_inverse.ncols(), pseudo_inverse.nrows());
println!("{} {}", response.ncols(), response.nrows());
let coeffs = pseudo_inverse * response;
println!("[");
for i in 1..num_coeffs {
println!("{},", coeffs[num_coeffs - i])
let mut coeffs: Vec<f32> = Vec::with_capacity(lenght);
for i in 1..half_lenght {
coeffs.push(half_coeffs[half_lenght - i]);
}
for i in 0..num_coeffs {
println!("{},", coeffs[i])
for i in 0..half_lenght {
coeffs.push(half_coeffs[i]);
}
println!("]");
Result::Ok(coeffs)
}
fn generate_frequency_response(
@ -39,7 +32,6 @@ fn generate_frequency_response(
num_points: usize,
) -> DVector<f32> {
let step_size = f_samp / 2.0 / (num_points - 1) as f32;
println!("Step size is {}Hz", step_size);
let mut response = DVector::<f32>::zeros(num_points);