Skip to content

Commit

Permalink
feat: add Hadamard codes
Browse files Browse the repository at this point in the history
  • Loading branch information
mchitre committed Jun 6, 2024
1 parent 56c5f36 commit 23fa317
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/dsp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Optim: optimize, minimizer, BFGS

export fir, removedc, removedc!, demon
export upconvert, downconvert, rrcosfir, rcosfir
export mseq, gmseq, circconv, goertzel, pll
export mseq, gmseq, circconv, goertzel, pll, hadamard
export sfilt, sfiltfilt, sresample, mfilter, findsignal
export istft, whiten, filt, filtfilt, resample, delay!, compose

Expand Down Expand Up @@ -296,6 +296,30 @@ function gmseq(m, θ=atan(√(2^maximum(m)-1)))
cos(θ) .+ 1im * sin(θ) .* x
end

"""
hadamard(k)
Generate a Walsh-Hadamard matrix of size `2ᵏ × 2ᵏ`. Each row of the matrix
is orthogonal to all other rows.
"""
function hadamard(k)
n = 2^k
[(-1)^count_ones(x&y) for x in 0:n-1, y in 0:n-1]
end

"""
hadamard(i, k)
Generate a vector with the entries of row `i` of a Walsh-Hadamard matrix of
size `2ᵏ × 2ᵏ`. Rows are numbered from `0` to `2ᵏ-1`, so that `i = 1` is the
first non-trivial (not all ones) Hadamard sequence.
"""
function hadamard(i, k)
n = 2^k
0 i < n || throw(ArgumentError("i must be in the range 0 to 2^k-1"))
[(-1)^count_ones(x&i) for x in 0:n-1]
end

"""
$(SIGNATURES)
Computes the circular convolution of `x` and `y`. Both vectors must be the same
Expand Down
9 changes: 9 additions & 0 deletions test/tests-core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,15 @@ function test_dsp()
@test mseq((1,3)) == mseq(3)
@test all(gmseq(3, 0) .== 1.0)

for j [1,2,4,8]
H = hadamard(j)
@test H isa Matrix{Int}
@test H' * H == 2^j * I(2^j)
for i 0:2^j-1
@test hadamard(i, j) == H[i+1,:]
end
end

x = cw(10kHz, 0.1s, 80kHz)
@test abs(goertzel(x, 10kHz)) length(x) atol=1e-6
@test abs(goertzel(x, 9kHz)) 0 atol=1e-6
Expand Down

2 comments on commit 23fa317

@mchitre
Copy link
Member Author

@mchitre mchitre commented on 23fa317 Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/108386

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.0 -m "<description of version>" 23fa31767f9c2363520f840e75d77d799f555219
git push origin v0.8.0

Please sign in to comment.