Skip to content

Commit

Permalink
add NRZI encoding decoding
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Buer <[email protected]>
  • Loading branch information
ErikBuer committed Jan 31, 2024
1 parent 347028d commit a21f635
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 1 deletion.
15 changes: 14 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
push!(LOAD_PATH, "../src/")

using Documenter, DigitalComm
using Documenter

# Running `julia --project docs/make.jl` can be very slow locally.
# To speed it up during development, one can use make_local.jl instead.
# The code below checks whether it's being called from make_local.jl or not.
const LOCAL = get(ENV, "LOCAL", "false") == "true"

if LOCAL
include("../src/DigitalComm.jl")
using .DigitalComm
else
using DigitalComm
ENV["GKSwstype"] = "100" # Prevents warnings in the doc build on github actions.
end

DocMeta.setdocmeta!(DigitalComm, :DocTestSetup, :(using DigitalComm); recursive=true)

Expand Down
9 changes: 9 additions & 0 deletions docs/make_local.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Assumes it's being run from project root.

using Pkg
Pkg.activate("docs/")
Pkg.instantiate()

ENV["LOCAL"] = "true"

include("make.jl")
10 changes: 10 additions & 0 deletions docs/src/base.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Function overview

## Common functions

```@autodocs
Expand All @@ -6,6 +8,14 @@ Pages = ["DigitalComm.jl"]
Order = [:function, :type]
```

## NRZI Encoding

```@autodocs
Modules = [DigitalComm]
Pages = ["NRZI.jl"]
Order = [:function, :type]
```

## Quadrature Amplitude Modulation

```@autodocs
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Currently, the package support the following elements
- Bit manipulation
- Generation of random binary sequence
- Conversion between binary sequences and octal sequences
- NRZI encoding and decoding
- Modulation // demodulation
- Quadrature Amplitude Modulation (QAM) with 4-QAM (QPSK), 16-QAM, 64-QAM and 256-QAM.
- Hard demapper for the x-QAM formats
Expand Down
3 changes: 3 additions & 0 deletions src/DigitalComm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export genByteSequence! , genByteSequence;
include("bitMapping.jl");
export bitMappingQAM! , bitMappingQAM;

include("NRZI.jl");
export encodeNRZI , decodeNRZI;

# --- QAM Hard demapper
include("bitDeMapping.jl");
export bitDemappingQAM! , bitDemappingQAM;
Expand Down
106 changes: 106 additions & 0 deletions src/NRZI.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""
encodeNRZI(bits::AbstractVector, transitions::Symbol=:low)::AbstractVector
Map a bit sequence to Non-Return-to-Zero Inverted (NRZI) encoded bits.
Expects a vector of bits, e.g. `[0, 1, 1, 0, 0, 1, 0, 1, 0, 1]`.
# Arguments
- `bits::AbstractVector`: Vector of bits to encode.
- `transitions::Symbol`: Symbol indicating the symbol to transition on (`:low`/`:high`). Defaults to `:low`.
# Returns
- `encoded_bits::AbstractVector`: Vector of encoded bits.
# Examples
```jldoctest
julia> encoded_bits = encodeNRZI(Int32[0, 1, 1, 0, 0, 1], :low);
julia> transpose(encoded_bits)
1×6 transpose(::Vector{Int32}) with eltype Int32:
1 1 1 0 1 1
```
The example below shows how the `transitions` argument affects the encoded bit sequence.
```jldoctest
julia> encoded_bits = encodeNRZI(Int32[0, 1, 1, 0, 0, 1], :high);
julia> transpose(encoded_bits)
1×6 transpose(::Vector{Int32}) with eltype Int32:
0 1 0 0 0 1
```
"""
function encodeNRZI(bits::AbstractVector, transitions::Symbol=:low)::AbstractVector
encoded_bits = eltype(bits)[]

last_bit = 0

transition_bit = (transitions == :high) ? 1 : 0

for bit in bits
if bit == transition_bit
last_bit = last_bit 1
end
push!(encoded_bits, last_bit)
end

return encoded_bits
end

"""
decodeNRZI(bits::AbstractVector, transitions::Symbol=:low)::AbstractVector
Decode a Non-Return-to-Zero Inverted (NRZI) encoded bit sequence.
Expects a vector of bits, e.g. `[0, 1, 1, 0, 0, 1, 0, 1, 0, 1]`.
# Arguments
- `bits::AbstractVector`: Vector of bits to encode.
- `transitions::Symbol`: Symbol represented by a transition in the NRZI coded sequence (`:low`/`:high`). Defaults to `:low`.
# Returns
- `decoded_bits::AbstractVector`: Vector of decoded bits. The first bit of the output depends on a value of a memory bit in the decoder.
this value is set to `0`.
# Examples
```jldoctest
julia> decoded_bits = decodeNRZI(Int32[1, 1, 1, 0, 1, 1], :low);
julia> transpose(decoded_bits)
1×6 transpose(::Vector{Int32}) with eltype Int32:
0 1 1 0 0 1
```
The example below shows how the `transitions` argument affects the decoded bit sequence.
```jldoctest
julia> decoded_bits = decodeNRZI(Int32[0, 1, 0, 0, 0, 1], :high);
julia> transpose(decoded_bits)
1×6 transpose(::Vector{Int32}) with eltype Int32:
0 1 1 0 0 1
```
"""
function decodeNRZI(encoded_bits::AbstractVector, transitions::Symbol=:low)::AbstractVector
decoded_bits = eltype(encoded_bits)[]
last_bit = 0

transition_bit = (transitions == :high) ? 1 : 0

for current_bit in encoded_bits
if current_bit != last_bit
decoded_bit = transition_bit
else
decoded_bit = 1 - transition_bit
end
push!(decoded_bits, decoded_bit)
last_bit = current_bit
end

return decoded_bits
end

0 comments on commit a21f635

Please sign in to comment.