Skip to content

Commit

Permalink
reuse FIRFilters for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
wheeheee committed Jan 23, 2025
1 parent 7498be6 commit f00fd55
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
39 changes: 18 additions & 21 deletions src/Filters/stream_filt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -682,14 +682,14 @@ are plotted on top of each other, they correlate very well, but one
signal will have more samples than the other.
"""
function resample(x::AbstractVector, rate::Union{Integer,Rational}, h::Vector)
_resample(x, rate, FIRFilter(h, rate))
_resample!(x, rate, FIRFilter(h, rate))
end

function resample(x::AbstractVector, rate::AbstractFloat, h::Vector, Nϕ::Integer=32)
_resample(x, rate, FIRFilter(h, rate, Nϕ))
_resample!(x, rate, FIRFilter(h, rate, Nϕ))

Check warning on line 689 in src/Filters/stream_filt.jl

View check run for this annotation

Codecov / codecov/patch

src/Filters/stream_filt.jl#L688-L689

Added lines #L688 - L689 were not covered by tests
end

function _resample(x::AbstractVector, rate::Real, self::FIRFilter)
function _resample!(x::AbstractVector, rate::Real, self::FIRFilter)
# Get delay, in # of samples at the output rate, caused by filtering processes
τ = timedelay(self)

Expand All @@ -716,14 +716,9 @@ end
Constructs a filter with `resample_filter` using the optional arguments `args`,
and resamples the signal `x` with it.
"""
function resample(x::AbstractVector, rate::Union{Integer,Rational}, args...)
h = resample_filter(rate, args...)
resample(x, rate, h)
end

function resample(x::AbstractVector, rate::AbstractFloat, Nϕ::Integer=32, args...)
h = resample_filter(rate, Nϕ, args...)
resample(x, rate, h, Nϕ)
function resample(x::AbstractVector, rate::Real, args::Real...)
sf = FIRFilter(rate, args...)
_resample!(x, rate, sf)
end

"""
Expand All @@ -736,20 +731,22 @@ to be used in constructing `FIRArbitrary` can be supplied
as an optional argument, which defaults to 32.
"""
function resample(x::AbstractArray, rate::Union{Integer,Rational}, h::Vector; dims)
mapslices(x; dims) do v
resample(v, rate, h)
end
sf = FIRFilter(h, rate)
return _resample!(x, rate, sf; dims)
end
function resample(x::AbstractArray, rate::AbstractFloat, h::Vector, Nϕ=32; dims)
mapslices(x; dims) do v
resample(v, rate, h, Nϕ)
end
sf = FIRFilter(h, rate, Nϕ)
_resample!(x, rate, sf; dims)

Check warning on line 739 in src/Filters/stream_filt.jl

View check run for this annotation

Codecov / codecov/patch

src/Filters/stream_filt.jl#L737-L739

Added lines #L737 - L739 were not covered by tests
end

resample(x::AbstractArray, rate::Union{Integer,Rational}, args...; dims) =
resample(x, rate, resample_filter(rate, args...); dims)
resample(x::AbstractArray, rate::AbstractFloat, Nϕ=32, args...; dims) =
resample(x, rate, resample_filter(rate, Nϕ, args...), Nϕ; dims)
resample(x::AbstractArray, rate::Real, args::Real...; dims) =
_resample!(x, rate, FIRFilter(rate, args...); dims)

_resample!(x::AbstractArray, rate::Real, sf::FIRFilter; dims) =
mapslices(x; dims) do v
reset!(sf)
_resample!(v, rate, sf)
end

#
# References
Expand Down
2 changes: 1 addition & 1 deletion test/resample.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,6 @@ end
# check that non-Int / Rational{Int} ratios get converted properly
x = rand(200)
@test resample(x, Int32(3)) == resample(x, 3)
@test resample(x, 1 // Int32(3)) == resample(x, 1 // 3)
@test resample(x, Rational{Int32}(1, 3)) == resample(x, 1 // 3)
@test resample(x, Rational{Int32}(2, 3)) == resample(x, 2 // 3)
end

0 comments on commit f00fd55

Please sign in to comment.