pub struct FirInstanceF32<'a> {
pub num_taps: u16,
pub coeffs: &'a [f32],
pub state: &'a mut [f32],
}Expand description
Instance structure for the floating-point FIR filter.
Fields§
§num_taps: u16§coeffs: &'a [f32]§state: &'a mut [f32]Implementations§
Source§impl<'a> FirInstanceF32<'a>
impl<'a> FirInstanceF32<'a>
Sourcepub fn init(num_taps: u16, coeffs: &'a [f32], state: &'a mut [f32]) -> Self
pub fn init(num_taps: u16, coeffs: &'a [f32], state: &'a mut [f32]) -> Self
Examples found in repository?
examples/basic_usage.rs (line 28)
5fn main() {
6 println!("=== embedded-dsp Basic Usage Example ===");
7
8 // 1. Vector Operations
9 let a = [1.0f32, 2.0, 3.0, 4.0];
10 let b = [10.0f32, 20.0, 30.0, 40.0];
11 let mut vec_out = [0.0f32; 4];
12 add_f32(&a, &b, &mut vec_out);
13 println!("Vector Add: {:?}", vec_out);
14
15 let dot = dot_prod_f32(&a, &b);
16 println!("Vector Dot Product: {}", dot);
17
18 // 2. Q15 Fixed-Point Saturating Math
19 let q15_a = [q15::from_bits(20000), q15::from_bits(25000)];
20 let q15_b = [q15::from_bits(15000), q15::from_bits(10000)];
21 let mut q15_out = [q15::ZERO; 2];
22 add_q15(&q15_a, &q15_b, &mut q15_out);
23 println!("Q15 Saturating Add (clamped at 32767): {:?}", q15_out);
24
25 // 3. FIR Filtering
26 let coeffs = [0.25f32, 0.5, 0.25]; // 3-tap moving average filter
27 let mut state = [0.0f32; 3 + 4 - 1];
28 let mut fir = FirInstanceF32::init(3, &coeffs, &mut state);
29
30 let input_signal = [1.0f32, 2.0, 3.0, 4.0];
31 let mut filtered_signal = [0.0f32; 4];
32 fir_f32(&mut fir, &input_signal, &mut filtered_signal);
33 println!("FIR Filter Output: {:?}", filtered_signal);
34
35 // 4. PID Motor Controller
36 let mut pid = PidInstanceF32::new(2.0, 0.1, 0.05);
37 let control_output = pid.process(10.0);
38 println!("PID Control Signal: {}", control_output);
39
40 // 5. 64-Point Complex FFT
41 let mut fft_data = [0.0f32; 128]; // 64 complex pairs [re, im, ...]
42 for i in 0..64 {
43 fft_data[2 * i] = (i as f32 * 0.1).sin();
44 }
45 cfft_f32(&mut fft_data, 64, 0, 1);
46 println!("64-Point Complex FFT processed successfully!");
47
48 // 6. 1D Conditional Median Filtering (Impulse / Spike Rejection)
49 let spiky_signal = [1.0f32, 1.1, 1.0, 100.0, 1.2, 1.1, 1.0];
50 let mut clean_signal = [0.0f32; 7];
51 median_filter_1d_f32(&spiky_signal, &mut clean_signal, 3, 5.0);
52 println!("Conditional Median Filter Out: {:?}", clean_signal);
53
54 // 7. Welch's Method Power Spectral Density (PSD)
55 let mut psd_out = [0.0f32; 32];
56 let mut sine_wave = [0.0f32; 128];
57 for (i, val) in sine_wave.iter_mut().enumerate() {
58 *val = (2.0 * core::f32::consts::PI * 100.0 * (i as f32) / 1000.0).sin();
59 }
60 welch_psd_f32(
61 &sine_wave,
62 &mut psd_out,
63 64,
64 32,
65 1000.0,
66 WelchWindow::Hamming,
67 true,
68 );
69 println!("Welch PSD (dB) at bin 6: {:.2} dB", psd_out[6]);
70
71 // 8. 2D Spatial Processing (2D DCT & Sobel Edge Detection)
72 let img_4x4 = [
73 0.0f32, 0.0, 10.0, 10.0, 0.0, 0.0, 10.0, 10.0, 0.0, 0.0, 10.0, 10.0, 0.0, 0.0, 10.0, 10.0,
74 ];
75 let mut edges = [0.0f32; 16];
76 sobel_edge_detection_f32(&img_4x4, &mut edges, 4, 4, 15.0);
77 println!("2D Sobel Edge Output (4x4): {:?}", edges);
78
79 // 9. Weighted Polynomial Least-Squares Sensor Calibration
80 let x_cal = [0.0f32, 1.0, 2.0, 3.0, 4.0];
81 let y_cal = [2.0f32, 5.0, 8.0, 11.0, 14.0]; // y = 2 + 3x
82 let mut cal_coeffs = [0.0f32; 2];
83 polynomial_least_squares_fit(&x_cal, &y_cal, None, 1, &mut cal_coeffs);
84 println!(
85 "Fitted Sensor Calibration: y = {:.2} + {:.2}*x",
86 cal_coeffs[0], cal_coeffs[1]
87 );
88}Auto Trait Implementations§
impl<'a> !UnwindSafe for FirInstanceF32<'a>
impl<'a> Freeze for FirInstanceF32<'a>
impl<'a> RefUnwindSafe for FirInstanceF32<'a>
impl<'a> Send for FirInstanceF32<'a>
impl<'a> Sync for FirInstanceF32<'a>
impl<'a> Unpin for FirInstanceF32<'a>
impl<'a> UnsafeUnpin for FirInstanceF32<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Casts the value.
Source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
Source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
Performs the conversion.
Source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
Source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
Performs the conversion.
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Casts the value.
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> StrictAs for T
impl<T> StrictAs for T
Source§fn strict_as<Dst>(self) -> Dstwhere
T: StrictCast<Dst>,
fn strict_as<Dst>(self) -> Dstwhere
T: StrictCast<Dst>,
Casts the value.
Source§impl<Src, Dst> StrictCastFrom<Src> for Dstwhere
Src: StrictCast<Dst>,
impl<Src, Dst> StrictCastFrom<Src> for Dstwhere
Src: StrictCast<Dst>,
Source§fn strict_cast_from(src: Src) -> Dst
fn strict_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.Source§impl<T> UnwrappedAs for T
impl<T> UnwrappedAs for T
Source§fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
fn unwrapped_as<Dst>(self) -> Dstwhere
T: UnwrappedCast<Dst>,
Casts the value.
Source§impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere
Src: UnwrappedCast<Dst>,
Source§fn unwrapped_cast_from(src: Src) -> Dst
fn unwrapped_cast_from(src: Src) -> Dst
Casts the value.
Source§impl<T> WrappingAs for T
impl<T> WrappingAs for T
Source§fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
fn wrapping_as<Dst>(self) -> Dstwhere
T: WrappingCast<Dst>,
Casts the value.
Source§impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere
Src: WrappingCast<Dst>,
Source§fn wrapping_cast_from(src: Src) -> Dst
fn wrapping_cast_from(src: Src) -> Dst
Casts the value.