sameold 0.6.0

A SAME/EAS digital receiver library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//! Access code correlation and squelch
//!
//! The [`CodeCorrelator`]
//! searches the given input symbols for a known sync
//! pattern. The number of bit errors is output. When
//! the number of bit errors is zero, the system is
//! perfectly synchronized. This struct can be used to
//! detect the start of transmission and also to align
//! the system to byte boundaries.
//!
//! The [`CodeAndPowerSquelch`]
//! extends this by buffering symbols and looking for
//! the sync pattern. When sync is found, samples are
//! emitted. The `CodeAndPowerSquelch` also enforces a
//! minimum signal power.

use std::default::Default;

use arraydeque::ArrayDeque;

#[cfg(not(test))]
use log::debug;

#[cfg(test)]
use std::println as debug;

/// Squelch state
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SquelchState {
    /// No carrier
    ///
    /// The squelch is "closed" and is not emitting data. No data
    /// will be output until sync is acquired
    NoCarrier,

    /// Dropped carrier
    ///
    /// The squelch was previously emitting data, but carrier has
    /// dropped on this update.
    DroppedCarrier,

    /// Reading data
    ///
    /// No state change. The squelch is reading data, but none is
    /// available yet
    Reading,

    /// Data ready
    ///
    /// The squelch is synchronized and an entire byte of
    /// symbol/zero samples is ready. If the boolean payload
    /// is `true`, the squelch has been freshly re-synchronized.
    Ready(bool, SquelchOut),
}

/// Squelch output
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SquelchOut {
    /// Synchronized data samples
    ///
    /// Synchronized samples at *twice* the symbol rate. The array
    /// is aligned to data byte boundaries and contains exactly
    /// one byte of symbols (16 samples).
    ///
    /// ```txt
    /// [0.0, 1.0, 0.0, … … … , 0.0, -1.0]
    ///       |                       |
    ///       |                       + most recent symbol
    ///       |
    ///       + least recent symbol
    /// ```
    ///
    /// The most recent symbols are to the right of the array.
    pub samples: [f32; CodeAndPowerSquelch::OUTPUT_LENGTH],

    /// Lifetime total symbols received
    ///
    /// Counts the lifetime total number of symbols *received*
    /// by the `CodeAndPowerSquelch`. This is *not* the same as
    /// the number of symbols emitted by the squelch. This field
    /// is useful as a measure of time.
    pub symbol_counter: u64,

    /// Symbol power estimate
    ///
    /// A smoothed estimate of the symbol power, in linear power
    /// power (amplitude-squared) units. This value is the power
    /// estimate as of the last symbol in `samples`.
    pub power: f32,
}

/// Access code and power squelch
///
/// This squelch method suppresses all output samples
/// until a synchronization symbol pattern is detected
/// at a specified minimum power level. The detector
/// makes hard binary decisions and requires a match
/// against the sync word to within a specified maximum
/// number of bit errors.
///
/// This block requires that symbols be transmitted
/// least-significant bit (LSb) first.
///
/// When the synchronization is acquired, the input
/// *samples* are output. Samples are output verbatim;
/// the decisions made by the correlator are *not* output.
/// This permits the samples to be equalized or otherwise
/// be further processed prior to decision-making.
///
/// The synchronization may be adjusted after it is
/// acquired. To lock the synchronization, use
/// [`lock(true)`](#method.lock). Locking the
/// synchronization prevents sync-like sequences in the
/// frame data from causing erroneous resync.
///
/// Output samples are always aligned to byte boundaries,
/// which makes it easy to assemble them into bytes.
///
/// This block imposes a delay of 32 *symbols* (64 samples)
/// and will always output 16 samples at a time when output
/// is available. This is, conveniently, one byte's worth
/// of samples.
///
/// This block will drop sync ("close the squelch") if the
/// received power level drops below the low-water mark. You
/// may also drop sync with the [`end()`](#method.end) method.
#[derive(Clone, Debug)]
pub struct CodeAndPowerSquelch {
    // maximum number of bit errors
    max_errors: u32,

    // power required to open the squelch (linear amplitude^2)
    power_open: f32,

    // power required to keep the squelch open (linear amplitude^2)
    power_close: f32,

    // correlator
    correlator: CodeCorrelator,

    // power tracker
    power_track: PowerTracker,

    // sample history
    sample_history: ArrayDeque<f32, 64, arraydeque::Wrapping>,

    // power threshold history
    power_history: ArrayDeque<bool, 32, arraydeque::Wrapping>,

    // lifetime total count of symbols received
    symbol_counter: u64,

    // time since samples were emitted
    sample_clock: Option<u8>,

    // prevent resynchronization
    sync_lock: bool,
}

impl CodeAndPowerSquelch {
    /// Input length, in samples
    pub const INPUT_LENGTH: usize = 2;

    /// Output length, in samples
    pub const OUTPUT_LENGTH: usize = 16;

    /// Create squelch
    ///
    /// The synchronizer will look for the bit pattern given in the
    /// `sync_to` word with no more than `max_errors` errors. When
    /// setting `max_errors`, be mindful of the correlation sidelobes
    /// of your synchronization code. Many synchronization waveforms
    /// have ambiguities at nearby bit shifts, and it is important
    /// not to admit these.
    ///
    /// The `CodeAndPowerSquelch` also tracks the power level of the
    /// demodulated symbols and can enforce a minimum power level.
    /// Power is estimated as the square of the symbol values. (It is
    /// expected that the input to the demodulator is normalized with
    /// an AGC.) The symbol power must reach at least `power_open` to
    /// open the squelch, and the squelch will close if the power falls
    /// below `power_close`. Set these values to zero to disable the
    /// power detector and use the code only. In general, we recommend
    /// `power_open` >= `power_close`.
    ///
    /// The power estimates are smoothed with a single-pole IIR filter
    /// of bandwidth `power_track_bandwidth`, which is expressed as a
    /// fraction of the symbol rate.
    pub fn new(
        sync_to: u32,
        max_errors: u32,
        power_open: f32,
        power_close: f32,
        power_track_bandwidth: f32,
    ) -> Self {
        let mut out = Self {
            max_errors,
            power_open,
            power_close: f32::min(power_close, power_open),
            correlator: CodeCorrelator::new(sync_to),
            power_track: PowerTracker::new(power_track_bandwidth),
            sample_history: ArrayDeque::default(),
            power_history: ArrayDeque::default(),
            symbol_counter: 0,
            sample_clock: None,
            sync_lock: false,
        };
        out.reset();
        out
    }

    /// Process and synchronize input
    ///
    /// The `CodeAndPowerSquelch` expects *TWO* samples per symbol
    /// at the input. The input must already be aligned to the symbol
    /// clock: `input[0]` must be a zero, and `input[1]` must be
    /// a symbol estimate. This is, coincidentally, just how
    /// [`TimingLoop`](super::symsync::TimingLoop) outputs
    /// them.
    ///
    /// If the output is `None`, the sync has not yet been found.
    ///
    /// If the output is `Some`, the sync has been acquired.
    /// Synchronized samples are output in [`SquelchOut`].
    ///
    /// This method may panic if `input` does not contain
    /// exactly two samples (and will panic in debug mode).
    pub fn input(&mut self, input: &[f32]) -> SquelchState {
        // append to history and correlator
        assert_eq!(Self::INPUT_LENGTH, input.len());
        self.sample_history.push_back(input[0]);
        self.sample_history.push_back(input[1]);
        let err = self.correlator.search(input[1]);
        let pwr = self.power_track.track(input[1]);
        self.power_history.push_back(pwr >= self.power_close);
        self.symbol_counter += 1;

        if !self.sample_history.is_full() {
            // wait for buffer to fill
            return SquelchState::NoCarrier;
        }

        let mut adjusted = false;
        if !self.sync_lock && err <= self.max_errors && pwr >= self.power_open {
            // sync is acquired and/or adjusted
            self.sample_clock = match self.sample_clock {
                None => {
                    debug!(
                        "squelch: acquired byte sync: {} errors, power {:.3}",
                        err, pwr
                    );
                    adjusted = true;
                    Some(0)
                }
                Some(0) => {
                    // sync is re-affirmed
                    Some(0)
                }
                Some(n) => {
                    debug!(
                        "squelch: adjust byte sync by +{} symbols with {} errors, power {:.3}",
                        8 - n,
                        err,
                        pwr
                    );
                    adjusted = true;
                    Some(0)
                }
            };
        } else if self.is_sync() && !self.power_history.front().expect("bad power history") {
            // sync is lost
            debug!(
                "squelch: lost sync: symbol power {:.3} below threshold",
                pwr
            );
            self.end();
            return SquelchState::DroppedCarrier;
        }

        // if the output clock is byte-aligned, output the last
        // byte's worth of samples
        match self.sample_clock {
            None => SquelchState::NoCarrier,
            Some(0) => {
                self.sample_clock = Some(1);

                let mut out = SquelchOut::default();
                for (o, h) in out
                    .samples
                    .iter_mut()
                    .zip(self.sample_history.iter().take(Self::OUTPUT_LENGTH))
                {
                    *o = *h;
                }
                out.symbol_counter = self.symbol_counter;
                out.power = pwr;
                SquelchState::Ready(adjusted, out)
            }
            Some(ref mut clk) => {
                *clk = (*clk + 1) % 8;
                SquelchState::Reading
            }
        }
    }

    /// Lock the synchronization
    ///
    /// If `lock` is true, prevent the synchronization from
    /// changing once it is acquired. This setting is cleared
    /// on a call to [`end()`](#method.end).
    pub fn lock(&mut self, lock: bool) {
        self.sync_lock = lock;
    }

    /// Reset to zero initial conditions
    ///
    /// If all you want to do is drop synchronization, you
    /// probably want to use the
    /// [`end()`](#method.end) instead.
    pub fn reset(&mut self) {
        self.end();
        self.correlator.reset();
        self.sample_history.clear();
        self.power_track.reset();
        self.power_history.clear();
        self.symbol_counter = 0;
    }

    /// Drop synchronization, inhibiting further output
    ///
    /// Signals the `CodeAndPowerSquelch` that the frame or
    /// byte synchronization has been lost. The squelch is
    /// immediately closed and the sync is dropped.
    /// [`input()`](#method.input) will produce no further
    /// outputs until synchronization is reacquired.
    pub fn end(&mut self) {
        self.sync_lock = false;
        self.sample_clock = None;
    }

    /// Lifetime count of received symbols
    ///
    /// Reports a total count of all *symbols* received since
    /// the last [`reset()`](#method.reset). This value is in
    /// symbols and not in samples. All received samples are
    /// counted, including those which are suppressed by the
    /// squelch mechanism.
    pub fn symbol_count(&self) -> u64 {
        self.symbol_counter
    }

    /// Power tracker state
    ///
    /// Returns the smoothed *symbol power* estimate as of
    /// the last call to `input()`.
    ///
    /// Since the input signal is typically normalized to
    /// an amplitude of 1.0, these values will *typically*
    /// range from 0.0 (no signal at all) to 1.0 (maximum
    /// symbol power). Values above 1.0 are possible,
    /// however.
    pub fn power(&self) -> f32 {
        self.power_track.power
    }

    /// Is the squelch synchronized?
    ///
    /// If the system is synchronized, synchronized samples
    /// will be emitted.
    pub fn is_sync(&self) -> bool {
        self.sample_clock.is_some()
    }

    /// Is the synchronization locked?
    ///
    /// When unlocked, the `CodeAndPowerSquelch` will resynchronize
    /// itself *any time* it detects the sync sequence. If your
    /// frame data might contain the sync sequence, this could
    /// be a problem. When locked, the `CodeAndPowerSquelch` will
    /// never resynchronize once sync is acquired.
    #[allow(dead_code)]
    pub fn is_locked(&self) -> bool {
        self.sync_lock
    }
}

impl Default for SquelchState {
    fn default() -> SquelchState {
        SquelchState::Reading
    }
}

/// Access code correlator
///
/// This synchronizer searches for a `sync_to` word.
/// The word must be transmitted least significant bit
/// first.
#[derive(Clone, Debug)]
pub struct CodeCorrelator {
    // sync word
    sync_to: u32,

    // current byte created from input
    data: u32,
}

impl CodeCorrelator {
    /// Create synchronizer
    ///
    /// The synchronizer looks for the byte `sync_to` and
    /// will lock on if the number of bit errors is zero.
    pub fn new(sync_to: u32) -> Self {
        Self { sync_to, data: 0 }
    }

    /// Process input symbol
    ///
    /// The correlator makes a hard binary decision on the
    /// given symbol `sym`, searches for the sync sequence,
    /// and returns the number of bit errors.
    pub fn search(&mut self, sym: f32) -> u32 {
        // shift bit onto data from the left
        let bit = (sym >= 0.0f32) as u32;
        self.data = self.data >> 1;
        self.data |= bit << 31;

        num_bit_errors(self.sync_to, self.data)
    }

    /// Reset to zero initial conditions
    ///
    /// The sync is dropped and must be re-acquired before
    /// additional data bytes can be output.
    pub fn reset(&mut self) {
        self.data = 0;
    }
}

// Test for sync
#[inline]
fn num_bit_errors(sync_to: u32, data: u32) -> u32 {
    // any 1 bits are bit errors here
    let err = sync_to ^ data;
    err.count_ones()
}

// Tracks received signal power
//
// The power tracker tracks the power level (in linear
// amplitude^2 units) of the received symbols. The power
// estimate is smoothed by a single-pole IIR filter with
// a specified `bandwidth`.
#[derive(Clone, Debug)]
struct PowerTracker {
    bandwidth: f32,
    power: f32,
}

impl PowerTracker {
    /// New power tracker
    ///
    /// The tracker smooths its estimates with the given
    /// `bandwidth`, which is specified by as a fraction
    /// of the baud rate.
    pub fn new(bandwidth: f32) -> Self {
        Self {
            bandwidth: f32::clamp(bandwidth, 0.0f32, 1.0f32),
            power: 0.0f32,
        }
    }

    /// Reset to zero initial conditions
    pub fn reset(&mut self) {
        self.power = 0.0f32;
    }

    /// Track power
    ///
    /// Accepts a soft symbol estimate in `sym` and calculates
    /// a smoothed power estimate. The power estimate is output
    /// in units of amplitude^2.
    #[inline]
    pub fn track(&mut self, sym: f32) -> f32 {
        let pwr = sym * sym;
        self.power += (pwr - self.power) * self.bandwidth;
        self.power = self.power.max(0.0f32);
        self.power
    }
}

#[cfg(test)]
mod tests {
    use super::super::waveform;
    use super::*;

    use assert_approx_eq::assert_approx_eq;

    #[test]
    fn test_num_bit_errors() {
        let sync_to = waveform::PREAMBLE_SYNC_WORD;
        let one_error = sync_to | 0x40u32;
        let another_error = 0xa9ababab;

        assert_eq!(0, num_bit_errors(sync_to, sync_to));
        assert_eq!(1, num_bit_errors(sync_to, one_error));
        assert_eq!(1, num_bit_errors(sync_to, another_error));
    }

    #[test]
    fn test_codecorr() {
        const BYTES: &[u8] = &[0xAB, 0xAB, 0xAB, 0xAB, 0x21];
        let mut syms = waveform::bytes_to_symbols(BYTES);

        let mut uut = CodeCorrelator::new(waveform::PREAMBLE_SYNC_WORD);

        // correlation reaches minimum error at 32 bits
        let out: Vec<u32> = syms.iter().map(|s| uut.search(*s)).collect();
        for (i, err) in out.iter().enumerate() {
            match i {
                31 => assert_eq!(*err, 0),
                _ => assert!(*err > 0),
            }
        }

        // flip one of the bits. now the minimum error is one.
        syms[19] = -syms[19];

        let out: Vec<u32> = syms.iter().map(|s| uut.search(*s)).collect();
        for (i, err) in out.iter().enumerate() {
            match i {
                31 => assert_eq!(*err, 1),
                _ => assert!(*err >= 1),
            }
        }
    }

    #[test]
    fn test_power_tracker() {
        let mut ptrack = PowerTracker::new(1.0f32);
        ptrack.track(1.0f32);
        ptrack.bandwidth = 0.5f32;
        assert_approx_eq!(0.62500f32, ptrack.track(-0.5f32));

        ptrack.power = 1.0f32;
        for _i in 0..16 {
            ptrack.track(1.0f32);
        }
        assert_approx_eq!(1.0f32, ptrack.power);
        assert_approx_eq!(1.0f32, ptrack.track(1.0f32));
    }

    #[test]
    fn test_simple_sync() {
        const BYTES: &[u8] = &[0xAB, 0xAB, 0xAB, 0xAB, 0x21];
        let insamp = waveform::bytes_to_samples(BYTES, 2);

        let mut squelch = CodeAndPowerSquelch::new(waveform::PREAMBLE_SYNC_WORD, 0, 0.0, 0.0, 0.1);
        assert!(!squelch.is_sync());

        let mut align_idx = 0; // sync sequence begins at 0 samples in insamp
        for (chunk, inp) in insamp.chunks(2).enumerate() {
            match squelch.input(inp) {
                SquelchState::Ready(resync, out) => {
                    assert!(!resync || chunk == 31);
                    if chunk == 31 {
                        assert_eq!(squelch.correlator.data, 0xabababab);
                    }

                    // check alignment
                    assert_eq!(&out.samples, &insamp[align_idx..align_idx + 16]);
                    align_idx += 16;
                    assert_eq!(out.symbol_counter - 1, chunk as u64);
                }
                _ => {}
            }
        }

        assert!(squelch.is_sync());
        assert_eq!(align_idx, 32);
        squelch.end();
        assert!(!squelch.is_sync());
    }

    #[test]
    fn test_sync_with_error() {
        const BYTES: &[u8] = &[0xF0, 0x0B, 0xA9, 0xAB, 0xAB, 0xAB, 0x21];
        let insamp = waveform::bytes_to_samples(BYTES, 2);

        let mut squelch = CodeAndPowerSquelch::new(waveform::PREAMBLE_SYNC_WORD, 1, 0.0, 0.0, 0.1);
        assert!(!squelch.is_sync());

        let mut align_idx = 32; // sync sequence begins at 32 samples in insamp
        for (chunk, inp) in insamp.chunks(2).enumerate() {
            match squelch.input(inp) {
                SquelchState::Ready(resync, out) => {
                    assert!(!resync || 47 == chunk);
                    assert_eq!(&out.samples, &insamp[align_idx..align_idx + 16]);
                    align_idx += 16;
                }
                _ => {}
            }
        }

        assert!(squelch.is_sync());
    }

    // here, we find an erroneous sync early on but then acquire a better sync
    #[test]
    fn test_sync_with_lots_of_errors() {
        const BYTES: &[u8] = &[0xAB, 0x0B, 0xA9, 0xAB, 0xAB, 0xAA, 0x21];
        let insamp = waveform::bytes_to_samples(BYTES, 2);

        let mut found_early = false;
        let mut found_later = false;
        let mut squelch = CodeAndPowerSquelch::new(waveform::PREAMBLE_SYNC_WORD, 3, 0.8, 0.1, 0.1);
        assert!(!squelch.is_sync());

        let mut align_idx = 32; // sync sequence begins at 32 samples in insamp
        for (chunk, inp) in insamp.chunks(2).enumerate() {
            match squelch.input(inp) {
                SquelchState::Ready(_, out) => {
                    if chunk == 47 {
                        assert_eq!(&out.samples, &insamp[align_idx..align_idx + 16]);
                        align_idx += 16;
                        found_later = true;
                    } else {
                        found_early = true;
                    }
                }
                _ => {}
            }
        }

        assert!(squelch.is_sync());
        assert!(found_later);
        assert!(found_early);
    }

    #[test]
    fn test_power_detection() {
        const BYTES: &[u8] = &[0xF0, 0x0B, 0xA9, 0xAB, 0xAB, 0xAB, 0x21];
        let insamp = waveform::bytes_to_samples(BYTES, 2);

        // we open the squelch
        let mut squelch = CodeAndPowerSquelch::new(waveform::PREAMBLE_SYNC_WORD, 1, 0.9, 0.5, 0.1);
        for inp in insamp.chunks(2) {
            let _ = squelch.input(inp);
        }
        assert!(squelch.is_sync());

        // Test that we drop carrier with enough zeros
        let mut found_reading = false;
        let mut found_drop = false;
        let mut found_none = false;
        for (samps, _itr) in std::iter::repeat([0.0f32; 2]).zip(0..40) {
            match squelch.input(&samps) {
                SquelchState::Reading => found_reading = true,
                SquelchState::DroppedCarrier => found_drop = true,
                SquelchState::NoCarrier => found_none = true,
                _ => {}
            }
        }
        assert!(found_reading);
        assert!(found_drop);
        assert!(found_none);
        assert!(!squelch.is_sync());
    }
}