Skip to content

Commit

Permalink
use more similars
Browse files Browse the repository at this point in the history
  • Loading branch information
wheeheee committed Feb 23, 2024
1 parent ffde816 commit 789f574
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/Filters/filt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function filt!(out::AbstractArray, f::SecondOrderSections{:z}, x::AbstractArray,
end

filt(f::SecondOrderSections{:z,T,G}, x::AbstractArray{S}, si=_zerosi(f, x)) where {T,G,S<:Number} =
filt!(Array{promote_type(T, G, S)}(undef, size(x)), f, x, si)
filt!(similar(x, promote_type(T, G, S)), f, x, si)

## Biquad
_zerosi(::Biquad{:z,T}, ::AbstractArray{S}) where {T,S} =
Expand Down Expand Up @@ -112,7 +112,7 @@ function filt!(out::AbstractArray, f::Biquad{:z}, x::AbstractArray,
end

filt(f::Biquad{:z,T}, x::AbstractArray{S}, si=_zerosi(f, x)) where {T,S<:Number} =
filt!(Array{promote_type(T, S)}(undef, size(x)), f, x, si)
filt!(similar(x, promote_type(T, S)), f, x, si)

## For arbitrary filters, convert to SecondOrderSections
filt(f::FilterCoefficients{:z}, x) = filt(convert(SecondOrderSections, f), x)
Expand Down Expand Up @@ -227,7 +227,7 @@ Apply the [stateful filter](@ref stateful-filter-objects) `f` on `x`.
output array `out` to `filt!(out, f, x)`.
"""
filt(f::DF2TFilter{<:FilterCoefficients{:z},<:Array{T}}, x::AbstractVector{V}) where {T,V} =
filt!(Vector{promote_type(T, V)}(undef, length(x)), f, x)
filt!(similar(x, promote_type(T, V)), f, x)

# Fall back to SecondOrderSections
DF2TFilter(coef::FilterCoefficients{:z}) = DF2TFilter(convert(SecondOrderSections, coef))
Expand Down Expand Up @@ -435,7 +435,7 @@ function tdfilt!(out::AbstractArray, h::AbstractVector{H}, x::AbstractArray) whe
end

filt(h::AbstractVector{H}, x::AbstractArray{T}) where {H,T} =
filt!(Array{promote_type(H, T)}(undef, size(x)), h, x)
filt!(similar(x, promote_type(H, T)), h, x)

#
# fftfilt and filt
Expand All @@ -449,7 +449,7 @@ using an FFT-based overlap-save algorithm.
"""
function fftfilt(b::AbstractVector{T}, x::AbstractArray{T},
nfft::Integer=optimalfftfiltlength(length(b), length(x))) where T<:Real
_fftfilt!(Array{T}(undef, size(x)), b, x, nfft)
_fftfilt!(similar(x, T), b, x, nfft)
end

"""
Expand Down Expand Up @@ -516,7 +516,7 @@ end
# Filter x using FIR filter b, heuristically choosing to perform convolution in
# the time domain using tdfilt or in the frequency domain using fftfilt
function filt(b::AbstractVector{T}, x::AbstractArray{T}) where T<:Number
filt_choose_alg!(Array{T}(undef, size(x)), b, x)
filt_choose_alg!(similar(x, T), b, x)
end

# Like filt but mutates output array
Expand Down
4 changes: 2 additions & 2 deletions src/dspbase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ state vector `si` (defaults to zeros).
"""
function filt(b::Union{AbstractVector, Number}, a::Union{AbstractVector, Number},
x::AbstractArray{T}, si::AbstractArray{S} = _zerosi(b,a,T)) where {T,S}
filt!(Array{promote_type(eltype(b), eltype(a), T, S)}(undef, size(x)), b, a, x, si)
filt!(similar(x, promote_type(eltype(b), eltype(a), T, S)), b, a, x, si)
end

# in-place filtering: returns results in the out argument, which may shadow x
Expand Down Expand Up @@ -63,7 +63,7 @@ function filt!(out::AbstractArray, b::Union{AbstractVector, Number}, a::Union{Ab
else
initial_si = si
si = similar(si, axes(si, 1))
for col = 1:ncols
for col in axes(x, 2)
# Reset the filter state
copyto!(si, view(initial_si, :, N > 1 ? col : 1))
if as > 1
Expand Down
2 changes: 1 addition & 1 deletion src/multitaper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ end
function allocate_output(config::MTCoherenceConfig{T}) where {T}
n_chan = config.cs_config.n_channels
n_freqs = length(config.cs_config.freq)
return Array{real(T)}(undef, n_chan, n_chan, n_freqs)
return Array{real(T),3}(undef, n_chan, n_chan, n_freqs)
end

"""
Expand Down
4 changes: 2 additions & 2 deletions test/dsp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ end
@test conv(f32a, b) fexp
@test conv(fb, a) fexp

offset_arr = OffsetArray{Int}(undef, -1:2)
offset_arr = OffsetVector{Int}(undef, -1:2)
offset_arr[:] = a
@test conv(offset_arr, 1:3) == OffsetVector(expectation, 0:5)
# Issue #352
Expand Down Expand Up @@ -106,7 +106,7 @@ end
@test conv(f32a, b) fexp
@test conv(fb, a) fexp

offset_arr = OffsetArray{Int}(undef, -1:1, -1:1)
offset_arr = OffsetMatrix{Int}(undef, -1:1, -1:1)
offset_arr[:] = a
@test conv(offset_arr, b) == OffsetArray(expectation, 0:3, 0:3)
end
Expand Down
2 changes: 1 addition & 1 deletion test/multitaper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ end
sin_1 = sin.(2π * 12.0 * t) # 12 Hz sinusoid signal
sin_2 = sin.(2π * 12.0 * t .+ π)
noise = vec(readdlm(joinpath(@__DIR__, "data", "noise.txt"))) # generated by `rand(1024) * 2 .- 1`
more_noisy = Array{Float64}(undef, 1, 2, n_samples)
more_noisy = Array{Float64,3}(undef, 1, 2, n_samples)
more_noisy[1, 1, :] = sin_1
more_noisy[1, 2, :] .= sin_1 .+ 3 * noise

Expand Down

0 comments on commit 789f574

Please sign in to comment.