flaw 0.6.1

Embedded signal filtering, no-std and no-alloc compatible.
Documentation
//! Butterworth filters: pre-calculated Second Order Sections coefficients for order 2 lowpass filters.
//! This file is autogenerated.
use crate::sos::SisoSosFilter;
use core::ops::Neg;
use num_traits::{FromPrimitive, MulAdd, Num, ToPrimitive};

pub const NUM_SECTIONS: usize = 1;
const NUM_INTERP_POINTS: usize = 100;
pub const MIN_CUTOFF_RATIO_F32: f64 = 0.005;
pub const MAX_CUTOFF_RATIO_F32: f64 = 0.4;
pub const MIN_CUTOFF_RATIO_F64: f64 = 0.002;
pub const MAX_CUTOFF_RATIO_F64: f64 = 0.4;

// [dimensionless] Log base-10 of cutoff ratios, to improve float precision during interpolation
#[rustfmt::skip]
static LOG10_CUTOFF_RATIOS: [f64; NUM_INTERP_POINTS] = [-4.0, -3.963615555643152, -3.9272311112863036, -3.8908466669294555, -3.8544622225726077, -3.8180777782157596, -3.7816933338589114, -3.745308889502063, -3.708924445145215, -3.672540000788367, -3.636155556431519, -3.599771112074671, -3.5633866677178228, -3.5270022233609746, -3.4906177790041264, -3.454233334647278, -3.41784889029043, -3.3814644459335823, -3.345080001576734, -3.308695557219886, -3.2723111128630378, -3.2359266685061896, -3.199542224149342, -3.1631577797924937, -3.1267733354356455, -3.0903888910787973, -3.054004446721949, -3.017620002365101, -2.981235558008253, -2.9448511136514046, -2.9084666692945564, -2.8720822249377087, -2.8356977805808605, -2.7993133362240123, -2.7629288918671646, -2.7265444475103164, -2.6901600031534683, -2.65377555879662, -2.617391114439772, -2.5810066700829237, -2.5446222257260755, -2.5082377813692274, -2.471853337012379, -2.435468892655531, -2.3990844482986833, -2.362700003941835, -2.326315559584987, -2.289931115228139, -2.253546670871291, -2.217162226514443, -2.1807777821575947, -2.1443933378007465, -2.1080088934438983, -2.07162444908705, -2.035240004730202, -1.9988555603733542, -1.962471116016506, -1.9260866716596579, -1.8897022273028097, -1.8533177829459615, -1.8169333385891133, -1.7805488942322656, -1.7441644498754174, -1.7077800055185692, -1.671395561161721, -1.6350111168048729, -1.5986266724480247, -1.5622422280911765, -1.5258577837343288, -1.4894733393774806, -1.4530888950206324, -1.4167044506637843, -1.380320006306936, -1.343935561950088, -1.3075511175932402, -1.271166673236392, -1.2347822288795438, -1.1983977845226956, -1.1620133401658475, -1.1256288958089993, -1.089244451452151, -1.0528600070953034, -1.0164755627384552, -0.980091118381607, -0.9437066740247588, -0.9073222296679107, -0.8709377853110625, -0.8345533409542147, -0.7981688965973666, -0.7617844522405184, -0.7254000078836702, -0.689015563526822, -0.6526311191699739, -0.6162466748131261, -0.5798622304562779, -0.5434777860994298, -0.5070933417425816, -0.4707088973857334, -0.4343244530288852, -0.3979400086720376];

#[rustfmt::skip]
static SOS_TABLES: [[[f64; NUM_INTERP_POINTS]; 5]; NUM_SECTIONS] = [    [
        [9.865221074441505e-08, 1.1664304655536065e-07, 1.3791433638427876e-07, 1.6306410556372223e-07, 1.927993610357591e-07, 2.2795594080651113e-07, 2.6952197558694694e-07, 3.186656190744321e-07, 3.767678223756709e-07, 4.4546106863414316e-07, 5.266751498720827e-07, 6.226912639461877e-07, 7.362059407159424e-07, 8.704065793549779e-07, 1.0290607006393627e-06, 1.2166213977761385e-06, 1.4383519171853452e-06, 1.7004728287202982e-06, 2.010335867320286e-06, 2.3766292617291145e-06, 2.8096202302680483e-06, 3.3214413416880004e-06, 3.926428637751013e-06, 4.64152082490449e-06, 5.486730502053719e-06, 6.485700343053256e-06, 7.666359446359114e-06, 9.061697758865713e-06, 1.0710679644451416e-05, 1.2659321379389498e-05, 1.4961961708463153e-05, 1.768275969380362e-05, 2.0897460056183274e-05, 2.4695473187605194e-05, 2.9182325167708084e-05, 3.448254263186991e-05, 4.0743048429845115e-05, 4.8137156923927564e-05, 5.686927278128791e-05, 6.718041452819179e-05, 7.93547043044042e-05, 9.372698857485269e-05, 0.00011069178145512704, 0.00013071375326249724, 0.00015434002243073236, 0.00018221454957847912, 0.00021509497888163996, 0.0002538723245747921, 0.0002995939600045394, 0.00035349043377050636, 0.00041700671266115935, 0.0004918385347770434, 0.0005799746487482088, 0.0006837458164041248, 0.0008058815664642638, 0.000949575805221481, 0.001118562515723952, 0.0013172029079299339, 0.0015505855162461016, 0.0018246408743793521, 0.002146272526093782, 0.0025235062486672526, 0.002965659466776126, 0.003483532910244273, 0.0040896266106938296, 0.0047983823303034715, 0.005626454461677071, 0.006593011324030007, 0.007720068604000917, 0.009032856452455126, 0.010560221465253504, 0.012335064475561354, 0.014394814820110157, 0.016781941595826404, 0.01954450252309626, 0.022736731560711033, 0.026419667631207306, 0.03066182906276421, 0.035539942101397666, 0.041139737709781715, 0.04755683965135924, 0.054897779613525065, 0.06328119324078858, 0.0728392762917489, 0.08371961526560666, 0.09608755539817683, 0.11012933622377635, 0.12605631897129982, 0.1441107633746368, 0.16457380381187275, 0.18777655803968626, 0.21411572875722681, 0.2440757165849056, 0.2782603013003039, 0.3174386208740489, 0.36261292524640865, 0.41512016579015204, 0.4767871940216461, 0.5501722129627202, 0.6389455251590224],
        [1.973044214888301e-07, 2.332860931107213e-07, 2.758286727685575e-07, 3.2612821112744446e-07, 3.855987220715182e-07, 4.5591188161302226e-07, 5.390439511738939e-07, 6.373312381488642e-07, 7.535356447513418e-07, 8.909221372682863e-07, 1.0533502997441654e-06, 1.2453825278923755e-06, 1.4724118814318848e-06, 1.7408131587099557e-06, 2.0581214012787254e-06, 2.433242795552277e-06, 2.8767038343706905e-06, 3.4009456574405963e-06, 4.020671734640572e-06, 4.753258523458229e-06, 5.619240460536097e-06, 6.642882683376001e-06, 7.852857275502026e-06, 9.28304164980898e-06, 1.0973461004107438e-05, 1.2971400686106512e-05, 1.5332718892718227e-05, 1.8123395517731426e-05, 2.1421359288902832e-05, 2.5318642758778995e-05, 2.9923923416926306e-05, 3.536551938760724e-05, 4.179492011236655e-05, 4.939094637521039e-05, 5.836465033541617e-05, 6.896508526373982e-05, 8.148609685969023e-05, 9.627431384785513e-05, 0.00011373854556257582, 0.00013436082905638358, 0.0001587094086088084, 0.00018745397714970538, 0.00022138356291025407, 0.0002614275065249945, 0.00030868004486146473, 0.00036442909915695824, 0.0004301899577632799, 0.0005077446491495842, 0.0005991879200090788, 0.0007069808675410127, 0.0008340134253223187, 0.0009836770695540867, 0.0011599492974964177, 0.0013674916328082496, 0.0016117631329285276, 0.001899151610442962, 0.002237125031447904, 0.0026344058158598678, 0.003101171032492203, 0.0036492817487587042, 0.004292545052187564, 0.005047012497334505, 0.005931318933552252, 0.006967065820488546, 0.008179253221387659, 0.009596764660606943, 0.011252908923354142, 0.013186022648060014, 0.015440137208001834, 0.018065712904910252, 0.021120442930507008, 0.024670128951122708, 0.028789629640220315, 0.03356388319165281, 0.03908900504619252, 0.045473463121422066, 0.05283933526241461, 0.06132365812552842, 0.07107988420279533, 0.08227947541956343, 0.09511367930271848, 0.10979555922705013, 0.12656238648157717, 0.1456785525834978, 0.16743923053121332, 0.19217511079635366, 0.2202586724475527, 0.25211263794259964, 0.2882215267492736, 0.3291476076237455, 0.37555311607937253, 0.42823145751445363, 0.4881514331698112, 0.5565206026006078, 0.6348772417480978, 0.7252258504928173, 0.8302403315803041, 0.9535743880432922, 1.1003444259254405, 1.2778910503180447],
        [9.865221074441505e-08, 1.1664304655536065e-07, 1.3791433638427876e-07, 1.6306410556372223e-07, 1.927993610357591e-07, 2.2795594080651113e-07, 2.6952197558694694e-07, 3.186656190744321e-07, 3.767678223756709e-07, 4.4546106863414316e-07, 5.266751498720827e-07, 6.226912639461877e-07, 7.362059407159424e-07, 8.704065793549779e-07, 1.0290607006393627e-06, 1.2166213977761385e-06, 1.4383519171853452e-06, 1.7004728287202982e-06, 2.010335867320286e-06, 2.3766292617291145e-06, 2.8096202302680483e-06, 3.3214413416880004e-06, 3.926428637751013e-06, 4.64152082490449e-06, 5.486730502053719e-06, 6.485700343053256e-06, 7.666359446359114e-06, 9.061697758865713e-06, 1.0710679644451416e-05, 1.2659321379389498e-05, 1.4961961708463153e-05, 1.768275969380362e-05, 2.0897460056183274e-05, 2.4695473187605194e-05, 2.9182325167708084e-05, 3.448254263186991e-05, 4.0743048429845115e-05, 4.8137156923927564e-05, 5.686927278128791e-05, 6.718041452819179e-05, 7.93547043044042e-05, 9.372698857485269e-05, 0.00011069178145512704, 0.00013071375326249724, 0.00015434002243073236, 0.00018221454957847912, 0.00021509497888163996, 0.0002538723245747921, 0.0002995939600045394, 0.00035349043377050636, 0.00041700671266115935, 0.0004918385347770434, 0.0005799746487482088, 0.0006837458164041248, 0.0008058815664642638, 0.000949575805221481, 0.001118562515723952, 0.0013172029079299339, 0.0015505855162461016, 0.0018246408743793521, 0.002146272526093782, 0.0025235062486672526, 0.002965659466776126, 0.003483532910244273, 0.0040896266106938296, 0.0047983823303034715, 0.005626454461677071, 0.006593011324030007, 0.007720068604000917, 0.009032856452455126, 0.010560221465253504, 0.012335064475561354, 0.014394814820110157, 0.016781941595826404, 0.01954450252309626, 0.022736731560711033, 0.026419667631207306, 0.03066182906276421, 0.035539942101397666, 0.041139737709781715, 0.04755683965135924, 0.054897779613525065, 0.06328119324078858, 0.0728392762917489, 0.08371961526560666, 0.09608755539817683, 0.11012933622377635, 0.12605631897129982, 0.1441107633746368, 0.16457380381187275, 0.18777655803968626, 0.21411572875722681, 0.2440757165849056, 0.2782603013003039, 0.3174386208740489, 0.36261292524640865, 0.41512016579015204, 0.4767871940216461, 0.5501722129627202, 0.6389455251590224],
        [-1.9991114234707956, -1.9990337727604992, -1.9989493363310546, -1.998857521194538, -1.9987576825434046, -1.998649119222161, -1.9985310688033433, -1.9984027022332262, -1.9982631180096637, -1.9981113358511933, -1.9979462898129565, -1.9977668208011163, -1.997571668433217, -1.997359462187372, -1.9971287117781622, -1.9968777966917122, -1.996604954806533, -1.996308270020304, -1.9959856587958438, -1.9956348555319212, -1.9952533966563937, -1.9948386033302172, -1.9943875626412075, -1.993897107155912, -1.9933637926865508, -1.9927838741176171, -1.9921532791232768, -1.9914675795921941, -1.9907219605606032, -1.989911186437411, -1.9890295642866116, -1.988070903912348, -1.9870284744703925, -1.9858949573065428, -1.9846623946974187, -1.9833221341421994, -1.9818647678249774, -1.9802800668364902, -1.9785569097110403, -1.9766832047993521, -1.9746458059610283, -1.9724304210212131, -1.970021512395213, -1.967402189242425, -1.9645540904673746, -1.9614572578414695, -1.9580899984750013, -1.9544287358259667, -1.9504478483916414, -1.9461194951922782, -1.941413427125804, -1.9362967832507005, -1.9307338710444661, -1.924685929691318, -1.9181108754797074, -1.9109630284436092, -1.9031928194679588, -1.8947464772056901, -1.885565694330078, -1.8755872728807446, -1.8647427487643327, -1.852957995850883, -1.8401528105724745, -1.8262404784873936, -1.8111273249218913, -1.7947122525362074, -1.7768862694644867, -1.7575320125161402, -1.736523270743556, -1.713724515392271, -1.6889904427301525, -1.662165536326633, -1.633083654784006, -1.6015676493961735, -1.5674290133215236, -1.5304675590966745, -1.4904711140593543, -1.4472152127338067, -1.40046275056052, -1.3499635434670951, -1.2954537114447293, -1.2366547700651915, -1.1732722700070515, -1.1049937689932585, -1.0314858502483877, -0.9523898127800617, -0.8673155459121803, -0.7758329553701911, -0.6774601187218066, -0.5716470963143466, -0.45775398460197514, -0.33502133757547997, -0.2025304576704996, -0.05915023521162438, 0.0965337865296409, 0.26631341054521035, 0.4524802392815798, 0.6579785181254244, 0.8865750515828817, 1.142980502539901],
        [0.9991118180796386, 0.9990342393326854, 0.9989498879884002, 0.9988581734509603, 0.9987584537408487, 0.9986500310459242, 0.9985321468912456, 0.9984039768957025, 0.9982646250809533, 0.9981131176954677, 0.997948396513556, 0.9977693115661721, 0.9975746132569798, 0.9973629438136895, 0.9971328280209648, 0.9968826631773032, 0.9966107082142017, 0.9963150719116189, 0.9959937001393131, 0.9956443620489681, 0.9952646351373148, 0.994851889095584, 0.9944032683557585, 0.9939156732392116, 0.993385739608559, 0.9928098169189893, 0.9921839445610622, 0.9915038263832295, 0.990764803279181, 0.9899618237229286, 0.9890894121334455, 0.9881416349511233, 0.9871120643106173, 0.9859937391992932, 0.9847791239980894, 0.9834600643127269, 0.9820277400186969, 0.9804726154641858, 0.9787843868021654, 0.9769519264574649, 0.9749632247782459, 0.9728053289755125, 0.9704642795210334, 0.967925044255475, 0.9651714505570975, 0.9621861160397834, 0.9589503783905279, 0.9554442251242659, 0.9516462242316596, 0.9475334569273602, 0.9430814539764486, 0.9382641373898086, 0.933053769639459, 0.9274209129569344, 0.9213344017455645, 0.9147613316644951, 0.9076670695308546, 0.9000152888374098, 0.8917680363950624, 0.8828858363782619, 0.8733278388687078, 0.8630520208455521, 0.852015448439579, 0.8401746101283707, 0.8274858313646667, 0.8139057818574212, 0.7993920873111949, 0.7839040578122602, 0.7674035451595597, 0.7498559412020915, 0.7312313285911666, 0.7115057942288784, 0.6906629140644466, 0.6686954157794791, 0.6456070234139086, 0.6214144853395186, 0.5961497845841836, 0.5698625289848636, 0.5426225189661107, 0.514522494306222, 0.48568107005016636, 0.45624588851929165, 0.42639704297020586, 0.3963508741602541, 0.3663643113108144, 0.3367400343727691, 0.30783289080728554, 0.2800582312553904, 0.25390317222035375, 0.22994231156183748, 0.20886021676071997, 0.19148425260438737, 0.1788333240101219, 0.17219144041283974, 0.1732206969665549, 0.1841382904404239, 0.2080004238790285, 0.24917025796116005, 0.31411380026799895, 0.41280159809618855],
    ],
];

pub trait CutoffRatioBounds {
    const MIN_CUTOFF_RATIO: f64;
    const MAX_CUTOFF_RATIO: f64;

    fn is_within_bounds(cutoff_ratio: f64) -> bool {
        cutoff_ratio >= Self::MIN_CUTOFF_RATIO && cutoff_ratio <= Self::MAX_CUTOFF_RATIO
    }
}

impl CutoffRatioBounds for f32 {
    const MIN_CUTOFF_RATIO: f64 = MIN_CUTOFF_RATIO_F32;
    const MAX_CUTOFF_RATIO: f64 = MAX_CUTOFF_RATIO_F32;
}

impl CutoffRatioBounds for f64 {
    const MIN_CUTOFF_RATIO: f64 = MIN_CUTOFF_RATIO_F64;
    const MAX_CUTOFF_RATIO: f64 = MAX_CUTOFF_RATIO_F64;
}

/// Butterworth order 2 lowpass filter, second order sections (SOS) implementation.
/// The filter is created by interpolating the SOS coefficients from stored tables.
///
/// `cutoff_ratio` (dimensionless) is the cutoff frequency divided by the sample frequency.
///
/// Region of validity for `cutoff_ratio` depends on the float type `T`:
/// * f32: 0.0050 to 0.4000
/// * f64: 0.0020 to 0.4000
pub fn butter2<T>(cutoff_ratio: f64) -> Result<SisoSosFilter<NUM_SECTIONS, T>, &'static str>
where
    T: Num
        + Copy
        + MulAdd<Output = T>
        + Neg<Output = T>
        + FromPrimitive
        + ToPrimitive
        + CutoffRatioBounds,
{
    if !T::is_within_bounds(cutoff_ratio) {
        return Err("cutoff_ratio out of bounds for provided float type");
    }
    // Convert SOS tables from static arrays to slices because new_interpolated expects slices.
    let sos_tables = SOS_TABLES
        .each_ref()
        .map(|sec| sec.each_ref().map(|coeffs| &coeffs[..]));
    SisoSosFilter::new_interpolated(cutoff_ratio, &LOG10_CUTOFF_RATIOS, sos_tables)
}

#[cfg(test)]
mod tests {
    use super::super::super::test_helpers::test_filter;
    use super::{
        MAX_CUTOFF_RATIO_F32, MAX_CUTOFF_RATIO_F64, MIN_CUTOFF_RATIO_F32, MIN_CUTOFF_RATIO_F64,
        NUM_SECTIONS, butter2,
    };
    #[test]
    fn test_butter2_f32() {
        test_filter::<NUM_SECTIONS, f32>(MIN_CUTOFF_RATIO_F32, MAX_CUTOFF_RATIO_F32, butter2);
    }

    #[test]
    fn test_butter2_f64() {
        test_filter::<NUM_SECTIONS, f64>(MIN_CUTOFF_RATIO_F64, MAX_CUTOFF_RATIO_F64, butter2);
    }
}