Skip to content

Commit

Permalink
fix(dsp): fix bug in decompose causing bare arrays to fail
Browse files Browse the repository at this point in the history
  • Loading branch information
mchitre committed Nov 24, 2024
1 parent b0f5fb7 commit 044cbb6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/dsp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -825,11 +825,12 @@ refinement, the amplitudes are simply the matched filter outputs at the arrival
julia> x = compose(mseq(12), [0.1, 0.2], [1.0, 0.7]; duration=1.0, fs=8000)
julia> x += 0.1 * randn(size(x))
julia> decompose(mseq(12), x)
(Float32[0.1, 0.2], [1.0020050686753323, 0.7006076693773022])
(time=[0.1, 0.2], amplitude=[1.00201, 0.70061], index=[801, 1601])
```
"""
function decompose(r::AbstractVector, x::AbstractVector, n=0; threshold=0.1, refine=true)
function decompose(r::AbstractVector, x::AbstractVector, n=0; threshold=0.01, refine=true)
# use OMP algorithm for sparse signal decomposition
fs = framerate(x)
Λ = Int[]
a = Array{eltype(x)}(undef, 0)
xᵣ = x
Expand All @@ -843,14 +844,14 @@ function decompose(r::AbstractVector, x::AbstractVector, n=0; threshold=0.1, ref
push!(a, mfo[λ])
if refine
sol = optimize(a, BFGS()) do a
= compose(r, time(Λ, x), a; duration=duration(x), fs=framerate(x))
= compose(r, .- 1) ./ fs, a; duration=duration(x), fs)
sum(abs2, x - x̂)
end
next_a = minimizer(sol)
else
next_a = a
end
= compose(r, time(Λ, x), next_a; duration=duration(x), fs=framerate(x))
= compose(r, .- 1) ./ fs, next_a; duration=duration(x), fs)
prev_rms = rms(xᵣ[λ:min+length(r)-1, end)])
xᵣ = x -
if (prev_rms - rms(xᵣ[λ:min+length(r)-1, end)])) / prev_rms < threshold
Expand All @@ -862,7 +863,7 @@ function decompose(r::AbstractVector, x::AbstractVector, n=0; threshold=0.1, ref
a = next_a
end
ndx = sortperm(Λ)
(time(Λ[ndx], x), a[ndx])
(time=(Λ[ndx] .- 1) ./ fs, amplitude=a[ndx], index=Λ[ndx])
end

"""
Expand Down
4 changes: 4 additions & 0 deletions test/tests-core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,10 @@ function test_dsp()
@test t [0.1, 0.2]
@test eltype(a) == ComplexF64
@test a [1.0, 0.7] atol=0.1
t, a, ndx = decompose(mseq(12), samples(x), 2)
@test t [800.0, 1600.0]
@test ndx == [801, 1601]
@test a [1.0, 0.7] atol=0.01

end

Expand Down

2 comments on commit 044cbb6

@mchitre
Copy link
Member Author

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/120083

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.10.1 -m "<description of version>" 044cbb60882c0a3bc47a50eec801056b789a6ba6
git push origin v0.10.1

Please sign in to comment.