Improved errors

This commit is contained in:
Sebastian 2023-07-16 20:00:55 +02:00
parent 1aa3d6d6ed
commit 081c5ac2e6
5 changed files with 53 additions and 4 deletions

21
Cargo.lock generated
View File

@ -39,6 +39,7 @@ version = "0.1.0"
dependencies = [
"nalgebra",
"num",
"thiserror",
]
[[package]]
@ -228,6 +229,26 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a35fc5b8971143ca348fa6df4f024d4d55264f3468c71ad1c2f365b0a4d58c42"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "463fe12d7993d3b327787537ce8dd4dfa058de32fc2b195ef3cde03dc4771e8f"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.23",
]
[[package]]
name = "typenum"
version = "1.16.0"

View File

@ -3,7 +3,7 @@ use firls_rs_macros::firls_real;
const COEFFS: [f32; 13] = firls_real!(
13,
8000.0,
[(0.0, 1.0), (1900.0, 1.0), (3000.0, 0.0), (7000.0, 0.0)]
[(0.0, 1.0), (1900.0, 1.0), (3000.0, 0.0), (4000.0, 0.0)]
);
fn main() {

View File

@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
nalgebra = "0.32.2"
num = "0.4.0"
thiserror = "1.0.43"

13
firls-rs/src/error.rs Normal file
View File

@ -0,0 +1,13 @@
use thiserror::Error;
#[derive(Error, Debug)]
pub enum FirLSError {
#[error("Filter length should be odd. Filter length {0} is even.")]
EvenFilterLen(usize),
#[error("Frequency response points should be above 0Hz. {0}Hz is below.")]
ResponseFreqBelowZero(f32),
#[error(
"Frequency response points should be below the nyquist frequency. {0}Hz is above {1}Hz"
)]
ResponseFreqAboveNyquist(f32, f32),
}

View File

@ -1,9 +1,24 @@
use nalgebra::base::{DMatrix, DVector};
use num::complex::Complex;
pub fn firls(lenght: usize, f_samp: f32, points: &Vec<(f32, f32)>) -> Result<Vec<f32>, &str> {
mod error;
use error::FirLSError;
pub fn firls(
lenght: usize,
f_samp: f32,
points: &Vec<(f32, f32)>,
) -> Result<Vec<f32>, error::FirLSError> {
if lenght % 2 != 1 {
return Result::Err("Filter should have an odd length");
return Result::Err(FirLSError::EvenFilterLen(lenght));
}
for (freq, _) in points.iter() {
if *freq > f_samp / 2.0 {
return Result::Err(FirLSError::ResponseFreqAboveNyquist(*freq, f_samp / 2.0));
}
if *freq < 0.0 {
return Result::Err(FirLSError::ResponseFreqBelowZero(*freq));
}
}
let half_lenght = lenght / 2 + 1;
@ -49,7 +64,6 @@ fn generate_frequency_response(
for step in 0..num_points {
let step_freq = step as f32 * step_size;
response[step] = interpolate_between_points(step_freq, &points)
}