|
| 1 | +""" |
| 2 | + encodeNRZI(bits::AbstractVector, transitions::Symbol=:low)::AbstractVector |
| 3 | +
|
| 4 | +Map a bit sequence to Non-Return-to-Zero Inverted (NRZI) encoded bits. |
| 5 | +Expects a vector of bits, e.g. `[0, 1, 1, 0, 0, 1, 0, 1, 0, 1]`. |
| 6 | +
|
| 7 | +# Arguments |
| 8 | +
|
| 9 | +- `bits::AbstractVector`: Vector of bits to encode. |
| 10 | +- `transitions::Symbol`: Symbol indicating the symbol to transition on (`:low`/`:high`). Defaults to `:low`. |
| 11 | +
|
| 12 | +# Returns |
| 13 | +
|
| 14 | +- `encoded_bits::AbstractVector`: Vector of encoded bits. |
| 15 | +
|
| 16 | +# Examples |
| 17 | +
|
| 18 | +```jldoctest |
| 19 | +julia> encoded_bits = encodeNRZI(Int32[0, 1, 1, 0, 0, 1], :low); |
| 20 | +
|
| 21 | +julia> transpose(encoded_bits) |
| 22 | +1×6 transpose(::Vector{Int32}) with eltype Int32: |
| 23 | + 1 1 1 0 1 1 |
| 24 | +``` |
| 25 | +
|
| 26 | +The example below shows how the `transitions` argument affects the encoded bit sequence. |
| 27 | +
|
| 28 | +```jldoctest |
| 29 | +julia> encoded_bits = encodeNRZI(Int32[0, 1, 1, 0, 0, 1], :high); |
| 30 | +
|
| 31 | +julia> transpose(encoded_bits) |
| 32 | +1×6 transpose(::Vector{Int32}) with eltype Int32: |
| 33 | + 0 1 0 0 0 1 |
| 34 | +``` |
| 35 | +""" |
| 36 | +function encodeNRZI(bits::AbstractVector, transitions::Symbol=:low)::AbstractVector |
| 37 | + encoded_bits = similar(bits) |
| 38 | + encodeNRZI!(encoded_bits,bits,transitions) |
| 39 | + return encoded_bits |
| 40 | +end |
| 41 | + |
| 42 | +function encodeNRZI!(encoded_bits::AbstractVector,bits::AbstractVector,transitions::Symbol=:low) |
| 43 | + @assert size(encoded_bits) == size(bits) "With NRZI encoding, input and output should have same size ($(size(encoded_bits)) ≂̸ $(size(bits))" |
| 44 | + last_bit = 0 |
| 45 | + transition_bit = (transitions == :high) ? 1 : 0 |
| 46 | + for n ∈ eachindex(bits) |
| 47 | + if bits[n] == transition_bit |
| 48 | + last_bit = last_bit ⊻ 1 |
| 49 | + end |
| 50 | + encoded_bits[n] = last_bit |
| 51 | + end |
| 52 | + return nothing |
| 53 | +end |
| 54 | + |
| 55 | +""" |
| 56 | + decodeNRZI(bits::AbstractVector, transitions::Symbol=:low)::AbstractVector |
| 57 | +
|
| 58 | +Decode a Non-Return-to-Zero Inverted (NRZI) encoded bit sequence. |
| 59 | +Expects a vector of bits, e.g. `[0, 1, 1, 0, 0, 1, 0, 1, 0, 1]`. |
| 60 | +
|
| 61 | +# Arguments |
| 62 | +
|
| 63 | +- `bits::AbstractVector`: Vector of bits to encode. |
| 64 | +- `transitions::Symbol`: Symbol represented by a transition in the NRZI coded sequence (`:low`/`:high`). Defaults to `:low`. |
| 65 | +
|
| 66 | +# Returns |
| 67 | +
|
| 68 | +- `decoded_bits::AbstractVector`: Vector of decoded bits. The first bit of the output depends on a value of a memory bit in the decoder. |
| 69 | + this value is set to `0`. |
| 70 | +
|
| 71 | +# Examples |
| 72 | +
|
| 73 | +```jldoctest |
| 74 | +julia> decoded_bits = decodeNRZI(Int32[1, 1, 1, 0, 1, 1], :low); |
| 75 | +
|
| 76 | +julia> transpose(decoded_bits) |
| 77 | +1×6 transpose(::Vector{Int32}) with eltype Int32: |
| 78 | + 0 1 1 0 0 1 |
| 79 | +``` |
| 80 | +
|
| 81 | +The example below shows how the `transitions` argument affects the decoded bit sequence. |
| 82 | +
|
| 83 | +```jldoctest |
| 84 | +julia> decoded_bits = decodeNRZI(Int32[0, 1, 0, 0, 0, 1], :high); |
| 85 | +
|
| 86 | +julia> transpose(decoded_bits) |
| 87 | +1×6 transpose(::Vector{Int32}) with eltype Int32: |
| 88 | + 0 1 1 0 0 1 |
| 89 | +``` |
| 90 | +""" |
| 91 | +function decodeNRZI(encoded_bits::AbstractVector, transitions::Symbol=:low)::AbstractVector |
| 92 | + decoded_bits = similar(encoded_bits) |
| 93 | + decodeNRZI!(decoded_bits,encoded_bits,transitions) |
| 94 | + return decoded_bits |
| 95 | +end |
| 96 | + |
| 97 | + |
| 98 | +function decodeNRZI!(decoded_bits::AbstractVector,encoded_bits::AbstractVector, transitions::Symbol=:low) |
| 99 | + @assert size(encoded_bits) == size(decoded_bits) "With NRZI encoding, input and output should have same size ($(size(decoded_bits)) ≂̸ $(size(encoded_bits))" |
| 100 | + last_bit = 0 |
| 101 | + transition_bit = (transitions == :high) ? 1 : 0 |
| 102 | + for n ∈ eachindex(encoded_bits) |
| 103 | + current_bit = encoded_bits[n] |
| 104 | + if current_bit != last_bit |
| 105 | + decoded_bit = transition_bit |
| 106 | + else |
| 107 | + decoded_bit = 1 - transition_bit |
| 108 | + end |
| 109 | + last_bit = current_bit |
| 110 | + decoded_bits[n] = decoded_bit |
| 111 | + end |
| 112 | + return nothing |
| 113 | +end |
0 commit comments