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

Fix multi-column filt for ndims(x)>2 #600

Merged
merged 1 commit into from
Dec 2, 2024
Merged

Conversation

martinholters
Copy link
Member

Our filt (and filt!) support multi-column input, working column-wise. That is extremely useful for e.g. stereo audio signals. The functions accept arbitrary-dimensional input, but only work properly for at most two dimensions.

Master:

julia> H = PolynomialRatio([0.1, 0.1], [1, -0.8]);

julia> filt(H, ones(5,3,2))
5×3×2 Array{Float64, 3}:
[:, :, 1] =
 0.1      0.1      0.1
 0.28     0.28     0.28
 0.424    0.424    0.424
 0.5392   0.5392   0.5392
 0.63136  0.63136  0.63136

[:, :, 2] =
 6.90139e-310  6.90139e-310  6.90139e-310
 6.90139e-310  6.90139e-310  6.90139e-310
 6.90139e-310  6.90139e-310  6.90139e-310
 6.90139e-310  6.90139e-310  6.90139e-310
 6.90139e-310  6.90139e-310  6.90139e-310

julia> filt(SecondOrderSections(H), ones(5,3,2))
5×3×2 Array{Float64, 3}:
[:, :, 1] =
 0.1      0.1      0.1
 0.28     0.28     0.28
 0.424    0.424    0.424
 0.5392   0.5392   0.5392
 0.63136  0.63136  0.63136

[:, :, 2] =
 0.1      0.1      0.1
 0.28     0.28     0.28
 0.424    0.424    0.424
 0.5392   0.5392   0.5392
 0.63136  0.63136  0.63136

While SecondOrderSections looks ok, running with -check-bounds=yes reveals that it's not quite sound either:

julia> filt(H, ones(5,3,2))
ERROR: BoundsError: attempt to access 5×3×2 Array{Float64, 3} at index [1, 1]
# [...]

julia> filt(SecondOrderSections(H), ones(5,3,2))
ERROR: BoundsError: attempt to access 5×3×2 Array{Float64, 3} at index [1, 1]
# [...]

I think once upon the time, Julia supported partial linear indexing, treating the last index in a getindex as a linear index for all remaining dimensions. Nowadays, using a CartesianIndex seems best for addressing the "column", so do that.

Copy link

codecov bot commented Dec 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.90%. Comparing base (1edcf64) to head (3380f14).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #600   +/-   ##
=======================================
  Coverage   97.90%   97.90%           
=======================================
  Files          19       19           
  Lines        3252     3252           
=======================================
  Hits         3184     3184           
  Misses         68       68           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wheeheee
Copy link
Member

wheeheee commented Dec 2, 2024

I suspect I removed that behaviour in a previous PR I made. But IIRC that required inbounds to work, so better this.

@martinholters martinholters merged commit b3f9da6 into master Dec 2, 2024
11 checks passed
@martinholters martinholters deleted the mh/fix-multi-col-filt branch December 2, 2024 11:01
martinholters added a commit that referenced this pull request Dec 2, 2024
martinholters added a commit that referenced this pull request Dec 3, 2024
* Fix multi-column `_small_filt_fir!` for `ndims(x)>2`

Missed in #600.

* Consistently require mutli-column state to match input layout

* `CartesianIndex()`

likely to lower into better code

---------

Co-authored-by: wheeheee <[email protected]>
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.

2 participants