Skip to main content

lib_jsl_dsp/discrete/digital_communications/
barker_codes.rs

1//! Barker codes: binary phase codes whose aperiodic autocorrelation sidelobes never exceed 1 in
2//! magnitude. They are used for frame synchronisation and radar pulse compression because the
3//! correlation peak (equal to the code length) stands out from the sidelobes by a factor of
4//! `length`. Barker codes exist only for lengths 2, 3, 4, 5, 7, 11 and 13.
5//!
6//! See the book entry [Pulse compression, Barker codes and pulse tracking](https://jwkunz.github.io/lib_jsl/book/comms/radar.html#barker-codes).
7
8use lib_jsl_core::ErrorsJSL;
9
10/// The lengths for which a Barker code exists.
11pub const BARKER_LENGTHS: [usize; 7] = [2, 3, 4, 5, 7, 11, 13];
12
13/// The Barker code of the given length as `±1` chips.
14pub fn barker_code(length: usize) -> Result<Vec<i8>, ErrorsJSL> {
15    let code: &[i8] = match length {
16        2 => &[1, -1],
17        3 => &[1, 1, -1],
18        4 => &[1, 1, -1, 1],
19        5 => &[1, 1, 1, -1, 1],
20        7 => &[1, 1, 1, -1, -1, 1, -1],
21        11 => &[1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1],
22        13 => &[1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1],
23        _ => return Err(ErrorsJSL::InvalidInputRange("Barker codes exist only for lengths 2, 3, 4, 5, 7, 11 and 13")),
24    };
25    Ok(code.to_vec())
26}
27
28/// The aperiodic autocorrelation of a code at lags `-(n-1)..=n-1` (the peak is at index `n - 1`).
29pub fn barker_autocorrelation(code: &[i8]) -> Vec<i32> {
30    let n = code.len() as isize;
31    (-(n - 1)..n)
32        .map(|lag| {
33            (0..n)
34                .filter(|&i| i + lag >= 0 && i + lag < n)
35                .map(|i| code[i as usize] as i32 * code[(i + lag) as usize] as i32)
36                .sum()
37        })
38        .collect()
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn every_code_has_unit_sidelobes_and_a_peak_equal_to_its_length() {
47        for &n in &BARKER_LENGTHS {
48            let code = barker_code(n).unwrap();
49            assert_eq!(code.len(), n);
50            assert!(code.iter().all(|&c| c == 1 || c == -1));
51            let r = barker_autocorrelation(&code);
52            assert_eq!(r.len(), 2 * n - 1);
53            assert_eq!(r[n - 1], n as i32, "peak of the length-{n} code");
54            for (i, &v) in r.iter().enumerate() {
55                if i != n - 1 {
56                    assert!(v.abs() <= 1, "length {n}, lag index {i}: sidelobe {v}");
57                }
58            }
59        }
60    }
61
62    #[test]
63    fn known_sequences() {
64        assert_eq!(barker_code(13).unwrap(), vec![1, 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1]);
65        assert_eq!(barker_code(7).unwrap(), vec![1, 1, 1, -1, -1, 1, -1]);
66        // The length-13 autocorrelation is 13 at zero lag and alternates 0, 1 away from it.
67        let r = barker_autocorrelation(&barker_code(13).unwrap());
68        assert_eq!(&r[12..16], &[13, 0, 1, 0]);
69    }
70
71    #[test]
72    fn other_lengths_are_rejected() {
73        for n in [0usize, 1, 6, 8, 9, 10, 12, 14, 100] {
74            assert!(barker_code(n).is_err(), "length {n}");
75        }
76    }
77}