Skip to content

Commit

Permalink
make code construction much faster for larger codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fe-r-oz committed Jan 17, 2025
1 parent c6c5bde commit 00ddaab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
33 changes: 21 additions & 12 deletions src/ecc/codes/classical/hamming.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,30 @@ struct Hamming <: ClassicalCode
end

function parity_checks(h::Hamming)
n = 2 ^ h.r - 1
rows = Int[]
cols = Int[]
vals = Int[]
for j in 1:n
columnsⱼ = bitstring(j)[end - h.r + 1:end]
for i in 1:h.r
if parse(Int, columnsⱼ[i]) == 1
push!(rows, i)
push!(cols, j)
push!(vals, 1)
n = 2^h.r - 1 # Number of columns in H
max_elements = n * h.r # Max non-zero entries in H
# Pre-allocate arrays for sparse matrix indices and values
rows = Vector{Int}(undef, max_elements)
cols = Vector{Int}(undef, max_elements)
vals = Vector{Int}(undef, max_elements)
idx = 1 # Tracks position in arrays
@inbounds for j in 1:n
mask = 1 << (h.r - 1) # Initialize mask for MSB
@simd for i in 1:h.r
if j & mask != 0 # Check if the i-th bit is 1
rows[idx] = i
cols[idx] = j
vals[idx] = 1
idx += 1
end
mask >>= 1 # Shift mask to next bit
end
end
H = sparse(rows, cols, vals, h.r, n)
# Resize arrays to actual number of non-zero elements
rows = rows[1:idx-1]
cols = cols[1:idx-1]
vals = vals[1:idx-1]
return sparse(rows, cols, vals, h.r, n)
end

code_n(h::Hamming) = 2 ^ h.r - 1
Expand Down
2 changes: 1 addition & 1 deletion test/test_ecc_hamming.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
end

@testset "Testing Hamming codes properties" begin
for r in 3:15
for r in 3:20
n = 2 ^ r - 1
k = 2 ^ r - 1 - r
H = parity_checks(Hamming(r))
Expand Down

0 comments on commit 00ddaab

Please sign in to comment.