-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from krishna-praneet/bp-syndrome-decoding
Bp syndrome decoding
- Loading branch information
Showing
3 changed files
with
107 additions
and
26 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,46 @@ | ||
using Random | ||
|
||
function parity_check_matrix(n, rw, cw) | ||
n_equations = (n * cw) ÷ rw | ||
""" | ||
parity_check_matrix(n, wr, wc) | ||
Computes a regular LDPC matrix using Callager's algorithm | ||
Block is created initially and then the columns are randomly | ||
permuted to create additional parity check n_equations | ||
# Arguments | ||
* `n`: The length of the code | ||
* `wr`: Row weight (Number of bits a parity check equations acts upon). Must divide n | ||
* `wc`: Column weight (Number of parity check equations for a given bit) | ||
# Examples | ||
```julia | ||
julia> using LDPC | ||
julia> H = parity_check_matrix(1000, 10, 9) | ||
``` | ||
""" | ||
function parity_check_matrix(n::Int, wr::Int, wc::Int) | ||
|
||
block_size = n_equations ÷ rw | ||
block = zeros(Int64, block_size, n) | ||
# For a regular LDPC matrix | ||
## wr = wc * (n / n-k) | ||
@assert n % wr == 0 | ||
|
||
n_equations = (n * wc) ÷ wr | ||
block_size = n_equations ÷ wc | ||
|
||
block = zeros(Bool, block_size, n) | ||
|
||
for i in 1:block_size | ||
for j in ((i-1)*cw + 1):((i)*cw) | ||
for j in ((i-1)*wr + 1):((i)*wr) | ||
block[i,j] = 1 | ||
end | ||
end | ||
|
||
H = block | ||
|
||
for i in 1:rw - 1 | ||
for i in 1:wc - 1 | ||
H = [H; block[:, shuffle(1:end)]] | ||
end | ||
|
||
return H | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
function syndrome_it_decode(pcm::Matrix, syndrome, max_iters::Int) | ||
|
||
# Get size of parity check matrix | ||
## n is codeword length, m is number of parity check equations | ||
m, n = size(pcm) | ||
|
||
# Initialise error and success | ||
error = zeros(Bool, n) | ||
success = false | ||
|
||
# Iterative decoding | ||
for iter in 1:max_iters | ||
curr_syn = (pcm * error) .% 2 | ||
|
||
# Check if the syndrome matches | ||
if curr_syn == vec(syndrome) | ||
success = true | ||
break | ||
end | ||
|
||
# Initialise votes | ||
votes = zeros(Int, n) | ||
votes = vec(votes) | ||
|
||
error_checks = .!(curr_syn .== syndrome) | ||
|
||
for i in 1:m | ||
if error_checks[i] == 1 | ||
votes += pcm[i, :] | ||
else | ||
votes -= pcm[i, :] | ||
end | ||
end | ||
|
||
max_idx = argmax(votes) | ||
error[max_idx] = 1 - error[max_idx] | ||
end | ||
|
||
return Bool.(error), success | ||
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