summaryrefslogtreecommitdiff
path: root/tests/unit/jtty/test_tbcc.f90
blob: 97fe50d166e0c1ced3cecb0c176d275baf972903 (plain)
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
program test_tbcc

  ! Transition invariants across supported memories and JTTY candidate acceptance.

  use, intrinsic :: iso_fortran_env, only: real32, int32
  use tbcc
  implicit none

  call expect_incoming_tones()
  call expect_empty_and_tied_candidates()
  call expect_encode_decode_roundtrip()
  call expect_reserved_zero_bit_filters_candidates()

  print *, 'test_tbcc: all checks passed'

contains

  subroutine expect_incoming_tones()
    integer, parameter :: memories(5) = [9, 12, 10, 11, 9]
    integer, parameter :: gray_tones(0:3) = [0, 1, 3, 2]
    integer :: n, predecessor, bit, destination, register_value, expected

    do n = 1, size(memories)
       call tbcc_init(memories(n))
       if (size(incoming_tones, 2) /= num_states) error stop 'incorrect transition table size'
       do predecessor = 0, num_states-1
          do bit = 0, 1
             register_value = 2*predecessor + bit
             destination = mod(register_value, num_states)
             expected = gray_tones(2*mod(popcnt(iand(register_value, g0_poly)), 2) + &
                  mod(popcnt(iand(register_value, g1_poly)), 2))
             if (incoming_tones(predecessor/(num_states/2), destination) /= expected) &
                  error stop 'incoming tone differs from encoder polynomial parity'
          end do
       end do
    end do
  end subroutine expect_incoming_tones

  subroutine expect_empty_and_tied_candidates()
    integer :: decoded(PAYLOAD_BITS)
    real(real32) :: energies(0:3, TOTAL_K)
    logical :: success

    call tbcc_init(9)
    energies = 1.0_real32
    call tbcc_wava_fsk_decode(energies, 0, 2, decoded, success)
    if (success .or. any(decoded /= 0)) error stop 'empty candidate list must fail cleanly'

    ! Equal branch metrics select the all-one circular path, which fails CRC.
    call tbcc_wava_fsk_decode(energies, 4, 2, decoded, success)
    if (success .or. any(decoded /= 0)) error stop 'tied candidate CRC rejection changed'
  end subroutine expect_empty_and_tied_candidates

  subroutine expect_encode_decode_roundtrip()
    integer(int32) :: payload(PAYLOAD_BITS), decoded(PAYLOAD_BITS)
    integer(int32) :: tone_symbols(TOTAL_K)
    real(real32)   :: tone_energies(0:3, TOTAL_K)
    logical        :: success
    integer        :: i, t

    call tbcc_init(9)

    do i = 1, PAYLOAD_BITS
       payload(i) = mod(i, 2)   ! deterministic, not all-zero
    end do

    call tbcc_encode(payload, tone_symbols)

    ! Noiseless channel: all of a symbol's energy on the transmitted tone.
    tone_energies = 0.0_real32
    do t = 1, TOTAL_K
       tone_energies(tone_symbols(t), t) = 1.0_real32
    end do

    call tbcc_wava_fsk_decode(tone_energies, 4, 2, decoded, success)
    if (.not. success) then
       print *, 'test_tbcc: noiseless decode failed'
       error stop 1
    end if
    if (any(decoded /= payload)) then
       print *, 'test_tbcc: noiseless decode mismatch'
       error stop 1
    end if
  end subroutine expect_encode_decode_roundtrip

  subroutine expect_reserved_zero_bit_filters_candidates()
    integer(int32) :: payload(PAYLOAD_BITS), decoded(PAYLOAD_BITS)
    integer(int32) :: tone_symbols(TOTAL_K)
    real(real32)   :: tone_energies(0:3, TOTAL_K)
    logical        :: success
    integer        :: i, t, one_bit_pos

    call tbcc_init(9)

    do i = 1, PAYLOAD_BITS
       payload(i) = mod(i, 2)
    end do
    ! A payload-bit position this payload sets to 1, so requiring it to be
    ! 0 must reject this exact (otherwise noiseless-decodable) codeword.
    one_bit_pos = 0
    do i = 1, PAYLOAD_BITS
       if (payload(i) == 1) then
          one_bit_pos = i
          exit
       end if
    end do
    if (one_bit_pos == 0) then
       print *, 'test_tbcc: test payload unexpectedly all-zero'
       error stop 1
    end if

    call tbcc_encode(payload, tone_symbols)
    tone_energies = 0.0_real32
    do t = 1, TOTAL_K
       tone_energies(tone_symbols(t), t) = 1.0_real32
    end do

    ! Control case: no reserved_zero_bit, decode succeeds as above.
    call tbcc_wava_fsk_decode(tone_energies, 4, 2, decoded, success)
    if (.not. success) then
       print *, 'test_tbcc: control decode (no reserved_zero_bit) failed'
       error stop 1
    end if

    ! With the filter on a bit this payload actually sets to 1, the only
    ! codeword consistent with these noiseless tone energies fails the
    ! filter, so decoding must now report failure.
    decoded = huge(0_int32)
    call tbcc_wava_fsk_decode(tone_energies, 4, 2, decoded, success,   &
         reserved_zero_bit=one_bit_pos)
    if (success) then
       print *, 'test_tbcc: reserved_zero_bit did not filter the candidate'
       error stop 1
    end if
    if (any(decoded /= 0_int32)) then
       print *, 'test_tbcc: failed decode did not clear its payload'
       error stop 1
    end if
  end subroutine expect_reserved_zero_bit_filters_candidates

end program test_tbcc