fn decode_bit(window: &[f32], sample_rate_hz: f32, dft_size: usize) -> Bit {
    const BIT_THRESHOLD: f32 = 0.1f32;
    // Normalize the samples to -1, 1
    let mut normalized_window = Vec::with_capacity(window.len());
    let mut w_max = 0f32;
    for w in window {
        if *w > w_max {
            w_max = *w;
        }
    }
    if w_max < BIT_THRESHOLD {
        return Bit::Indeterminate;
    }
    for i in 0..window.len() {
        normalized_window.push(window[i] / w_max);
    }
    let ft_data = real_dft(&blackman_harris_window(&normalized_window), dft_size);
    let freq_step = sample_rate_hz / (ft_data.len() as f32) / 2f32;
    let freq_range = freq_step;
    let mut peak_freq = 0f32;
    let mut peak_ampl = 0f32;
    for n in 0..ft_data.len() / 2 {
        if n % 2 == 0 {
            let point = (ft_data[n].powi(2) + ft_data[n + 1].powi(2)).sqrt();
            let freq = (n as f32) * freq_step / 2f32;
            if freq > 0f32 && point > peak_ampl {
                peak_freq = freq;
                peak_ampl = point;
            }
        }
    }
    // Decode
    let zero_delta = MAX_FREQ_DELTA * ZERO_FREQ_HZ;
    let one_delta = MAX_FREQ_DELTA * ONE_FREQ_HZ;
    //println!("    {:01.4} @ {:4.1} Hz", peak_ampl, peak_freq);
    if peak_ampl < BIT_THRESHOLD {
        Bit::Indeterminate
    } else if ZERO_FREQ_HZ - zero_delta < peak_freq && peak_freq < ZERO_FREQ_HZ + zero_delta {
        Bit::Zero
    } else if ONE_FREQ_HZ - one_delta < peak_freq && peak_freq < ONE_FREQ_HZ + one_delta {
        Bit::One
    } else {
        Bit::Indeterminate
    }
}
/// Decode an FSK-encoded bit from a SAME message
Decode an FSK-encoded bit from a SAME message
Decode an FSK - encoded bit from a SAME message
fn decode_bit(window: &[f32], sample_rate_hz: f32, dft_size: usize) -> Bit {
    const BIT_THRESHOLD: f32 = 0.1f32;
    // Normalize the samples to -1, 1
    let mut normalized_window = Vec::with_capacity(window.len());
    let mut w_max = 0f32;
    for w in window {
        if *w > w_max {
            w_max = *w;
        }
    }
    if w_max < BIT_THRESHOLD {
        return Bit::Indeterminate;
    }
    for i in 0..window.len() {
        normalized_window.push(window[i] / w_max);
    }
    let ft_data = real_dft(&blackman_harris_window(&normalized_window), dft_size);
    let freq_step = sample_rate_hz / (ft_data.len() as f32) / 2f32;
    let freq_range = freq_step;
    let mut peak_freq = 0f32;
    let mut peak_ampl = 0f32;
    for n in 0..ft_data.len() / 2 {
        if n % 2 == 0 {
            let point = (ft_data[n].powi(2) + ft_data[n + 1].powi(2)).sqrt();
            let freq = (n as f32) * freq_step / 2f32;
            if freq > 0f32 && point > peak_ampl {
                peak_freq = freq;
                peak_ampl = point;
            }
        }
    }
    // Decode
    let zero_delta = MAX_FREQ_DELTA * ZERO_FREQ_HZ;
    let one_delta = MAX_FREQ_DELTA * ONE_FREQ_HZ;
    //println!("    {:01.4} @ {:4.1} Hz", peak_ampl, peak_freq);
    if peak_ampl < BIT_THRESHOLD {
        Bit::Indeterminate
    } else if ZERO_FREQ_HZ - zero_delta < peak_freq && peak_freq < ZERO_FREQ_HZ + zero_delta {
        Bit::Zero
    } else if ONE_FREQ_HZ - one_delta < peak_freq && peak_freq < ONE_FREQ_HZ + one_delta {
        Bit::One
    } else {
        Bit::Indeterminate
    }
}
fn decode_bit ( window : & [ f32 ] , sample_rate_hz : f32 , dft_size : usize ) -> Bit { const BIT_THRESHOLD : f32 = 0.1f32 ; // Normalize the samples to -1, 1 let mut normalized_window = Vec :: with_capacity ( window . len ( ) ) ; let mut w_max = 0f32 ; for w in window { if * w > w_max { w_max = * w ; } } if w_max < BIT_THRESHOLD { return Bit :: Indeterminate ; } for i in 0 .. window . len ( ) { normalized_window . push ( window [ i ] / w_max ) ; } let ft_data = real_dft ( & blackman_harris_window ( & normalized_window ) , dft_size ) ; let freq_step = sample_rate_hz / ( ft_data . len ( ) as f32 ) / 2f32 ; let freq_range = freq_step ; let mut peak_freq = 0f32 ; let mut peak_ampl = 0f32 ; for n in 0 .. ft_data . len ( ) / 2 { if n % 2 == 0 { let point = ( ft_data [ n ] . powi ( 2 ) + ft_data [ n + 1 ] . powi ( 2 ) ) . sqrt ( ) ; let freq = ( n as f32 ) * freq_step / 2f32 ; if freq > 0f32 && point > peak_ampl { peak_freq = freq ; peak_ampl = point ; } } } // Decode let zero_delta = MAX_FREQ_DELTA * ZERO_FREQ_HZ ; let one_delta = MAX_FREQ_DELTA * ONE_FREQ_HZ ; //println!("    {:01.4} @ {:4.1} Hz", peak_ampl, peak_freq); if peak_ampl < BIT_THRESHOLD { Bit :: Indeterminate } else if ZERO_FREQ_HZ - zero_delta < peak_freq && peak_freq < ZERO_FREQ_HZ + zero_delta { Bit :: Zero } else if ONE_FREQ_HZ - one_delta < peak_freq && peak_freq < ONE_FREQ_HZ + one_delta { Bit :: One } else { Bit :: Indeterminate } }
Decode an FSK-encoded bit from a SAME message
Decode an FSK - encoded bit from a SAME message
window
&[f32]
sample_rate_hz
f32
dft_size
usize