Skip to content
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 Butterworth Filter #242

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

ashwinn-v
Copy link

In response to feature request #240
Please let me know if there are any fixes to be done on the code.

@codecov
Copy link

codecov bot commented Jan 17, 2022

Codecov Report

Merging #242 (a848ed0) into master (424523c) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #242      +/-   ##
==========================================
+ Coverage   92.14%   92.17%   +0.02%     
==========================================
  Files          12       12              
  Lines        1642     1648       +6     
==========================================
+ Hits         1513     1519       +6     
  Misses        129      129              
Impacted Files Coverage Δ
src/ImageFiltering.jl 68.75% <ø> (ø)
src/kernel.jl 99.27% <100.00%> (+0.03%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 424523c...a848ed0. Read the comment docs.

@ashwinn-v ashwinn-v changed the title Butter Add Butterworth Filter Jan 17, 2022
Copy link
Member

@johnnychen94 johnnychen94 left a comment

Choose a reason for hiding this comment

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

Docs and tests are needed.

Some docstrings in ImageFiltering are written in Julia pre-1.0 age, so you can follow the guideline in https://docs.julialang.org/en/v1/manual/documentation/#man-documentation. I personally prefer a verbose docstring, e.g., the docstring for Gabor in #235. You can also follow how unit tests are written in that PR.

src/kernel.jl Outdated Show resolved Hide resolved
ashwinn-v and others added 3 commits January 17, 2022 20:01
Style fix

Co-authored-by: Johnny Chen <johnnychen94@hotmail.com>
#Citation
Selesnick, Ivan W., and C. Sidney Burrus. "Generalized digital Butterworth filter design." IEEE Transactions on signal processing 46.6 (1998): 1688-1694.
"""

Copy link
Member

Choose a reason for hiding this comment

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

Extra empty line here would cause ?Kernel.butterworth giving nothing.

Suggested change

@@ -509,6 +509,26 @@ moffat(α::Real, β::Real) = moffat(α, β, ceil(Int, (α*2*sqrt
sum(x.^2)
end

"""
butterworth(n,Wn,ls{tuple}) -> k
Copy link
Member

Choose a reason for hiding this comment

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

indentation, and :: for type annotation.

Suggested change
butterworth(n,Wn,ls{tuple}) -> k
butterworth(n, Wn, ls::Dims) -> k

@@ -509,6 +509,26 @@ moffat(α::Real, β::Real) = moffat(α, β, ceil(Int, (α*2*sqrt
sum(x.^2)
end

"""
butterworth(n,Wn,ls{tuple}) -> k
Returns a multidimensional dimensional Butterworth kernel contained in an OffsetArray(::Matrix{Float64})
Copy link
Member

Choose a reason for hiding this comment

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

one extra new line after the signature.

Suggested change
Returns a multidimensional dimensional Butterworth kernel contained in an OffsetArray(::Matrix{Float64})
Returns a multidimensional dimensional Butterworth kernel contained in an OffsetArray(::Matrix{Float64})

Comment on lines +516 to +518
- `n` is the order of the filter
- `Wn` is the normalized cutoff frequency
- `ls` is the size of the kernel
Copy link
Member

Choose a reason for hiding this comment

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

You don't need indentation for the markdown list

Suggested change
- `n` is the order of the filter
- `Wn` is the normalized cutoff frequency
- `ls` is the size of the kernel
- `n` is the order of the filter
- `Wn` is the normalized cutoff frequency
- `ls` is the size of the kernel


- `n` is the order of the filter
- `Wn` is the normalized cutoff frequency
- `ls` is the size of the kernel
Copy link
Member

Choose a reason for hiding this comment

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

why you use ls for kernel size instead of sz? The former seems to come from the math equation of butterworth kernel. Can you also add the equation to the docstring so that people immediately knows what n, Wn, ls means and how they affects the results?

@. 1/(1+(sqrt(2)-1)*(df(R)/Wn)^nn)
end

butterworth(n::Real, Wn::Real, ls::Integer) = butterworth(n, Wn, (ls,ls))
Copy link
Member

Choose a reason for hiding this comment

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

This convenient method only makes sense when butterworth is 2D only; otherwise, we should not add it.

Comment on lines +245 to +246
@test Kernel.butterworth(a,b,(3,3)) == Kernel.butterworth(a,b,3)
@test Kernel.butterworth(a,b,(4,4)) == Kernel.butterworth(a,b,4)
Copy link
Member

Choose a reason for hiding this comment

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

We also need some numerical tests here, can you test against one or two results generated from other frameworks that has butterworth implemented or manual calculated results? Also do remember to add a comment on how the references are generated.

We also need to test that the kernel axis are "centered". For instance, the axes should be (-1:1, -1:1) when kernel size is (3, 3). Also need to test the even case as well.

Selesnick, Ivan W., and C. Sidney Burrus. "Generalized digital Butterworth filter design." IEEE Transactions on signal processing 46.6 (1998): 1688-1694.
"""

function butterworth(n::Real, Wn::Real, ls::Tuple{Integer,Integer})
Copy link
Member

Choose a reason for hiding this comment

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

It looks like if we do

Suggested change
function butterworth(n::Real, Wn::Real, ls::Tuple{Integer,Integer})
function butterworth(n::Real, Wn::Real, ls::NTuple{N,Integer}) where N

we immediately get a N-dimensional implementation as the docstring says: "multidimensional dimensional Butterworth kernel". Can you check this and if it works, add 1d and 3d tests? Also don't forget to update the docstring OffsetArray(::Matrix{Float64}).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants