-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add NRZI encoding decoding #12
Merged
+183
−1
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a21f635
add NRZI encoding decoding
ErikBuer 6d0e7f3
Create bang methods for NRZI encode/decode and pre-alloc buffers
RGerzaguet 74ee151
Export bang methods for NRZI
RGerzaguet 83800f4
Update naming in doc
RGerzaguet 2127952
Adding test file for NRZI
RGerzaguet bc39152
correct name for testing file
RGerzaguet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RGerzaguet marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
""" | ||
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 = similar(bits) | ||
encodeNRZI!(encoded_bits,bits,transitions) | ||
return encoded_bits | ||
end | ||
|
||
function encodeNRZI!(encoded_bits::AbstractVector,bits::AbstractVector,transitions::Symbol=:low) | ||
@assert size(encoded_bits) == size(bits) "With NRZI encoding, input and output should have same size ($(size(encoded_bits)) ≂̸ $(size(bits))" | ||
last_bit = 0 | ||
transition_bit = (transitions == :high) ? 1 : 0 | ||
for n ∈ eachindex(bits) | ||
if bits[n] == transition_bit | ||
last_bit = last_bit ⊻ 1 | ||
end | ||
encoded_bits[n] = last_bit | ||
end | ||
return nothing | ||
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 = similar(encoded_bits) | ||
decodeNRZI!(decoded_bits,encoded_bits,transitions) | ||
return decoded_bits | ||
end | ||
|
||
|
||
function decodeNRZI!(decoded_bits::AbstractVector,encoded_bits::AbstractVector, transitions::Symbol=:low) | ||
@assert size(encoded_bits) == size(decoded_bits) "With NRZI encoding, input and output should have same size ($(size(decoded_bits)) ≂̸ $(size(encoded_bits))" | ||
last_bit = 0 | ||
transition_bit = (transitions == :high) ? 1 : 0 | ||
for n ∈ eachindex(encoded_bits) | ||
current_bit = encoded_bits[n] | ||
if current_bit != last_bit | ||
decoded_bit = transition_bit | ||
else | ||
decoded_bit = 1 - transition_bit | ||
end | ||
last_bit = current_bit | ||
decoded_bits[n] = decoded_bit | ||
end | ||
return nothing | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# ---------------------------------------------------- | ||
# --- Import modules | ||
# ---------------------------------------------------- | ||
using DigitalComm | ||
using Test | ||
# ---------------------------------------------------- | ||
# --- Tests | ||
# ---------------------------------------------------- | ||
# Note --> The mapping system is described in bitMapping.jl | ||
println("Tests for symbol mapper with NRZI sequences"); | ||
|
||
@testset "NRZI" begin | ||
# Create a bit squence (already tested) | ||
nbBits = 2 * 2048; | ||
bitSeq = genBitSequence(nbBits); | ||
# Pass trough the function | ||
buff = zeros(Complex{Float64},nbBits) | ||
# Call | ||
encodeNRZI!(buff,bitSeq); | ||
buff2 = encodeNRZI(bitSeq); | ||
@test all( buff .== buff2) | ||
# Ensure Tx // Rx is Ok | ||
@test all(bitSeq .== decodeNRZI(encodeNRZI(bitSeq))) | ||
@test all(bitSeq .== decodeNRZI(encodeNRZI(bitSeq,:high),:high)) | ||
# Some manual check for both transitions | ||
buff = [0x01;0x00;0x00;0x01;0x00;0x00;0x00;0x01 ]; | ||
@test all( encodeNRZI(buff,:low) .== [0;1;0;0;1;0;1;1]) # Transitions on 0 | ||
@test all( encodeNRZI(buff,:high) .== [1;1;1;0;0;0;0;1]) # Transitions on 1 | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok for this !