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

feat:add modes to conv function (#399) #403

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions src/dspbase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,31 @@ end

# May switch argument order
"""
conv(u,v)
conv(u,v; mode = :full)

Convolution of two arrays. Uses either FFT convolution or overlap-save,
depending on the size of the input. `u` and `v` can be N-dimensional arrays,
with arbitrary indexing offsets, but their axes must be a `UnitRange`.

:full — Return the full convolution.

:same — Return the central part of the convolution, which is the same size as u.

:valid — Return only parts of the convolution that are computed without zero-padded edges.
"""
function conv(u::AbstractArray{T, N},
function conv(u, v; mode::Symbol = :full)
if mode == :full
conv_full(u, v)
elseif mode == :same
conv_same(u, v)
elseif mode == :valid
conv_valid(u, v)
else
throw(ArgumentError("mode keyword argument must be :full or :same or :valid"))
end
end

function conv_full(u::AbstractArray{T, N},
v::AbstractArray{T, N}) where {T<:BLAS.BlasFloat, N}
su = size(u)
sv = size(v)
Expand All @@ -696,35 +714,57 @@ function conv(u::AbstractArray{T, N},
end
end

function conv(u::AbstractArray{<:BLAS.BlasFloat, N},
function conv_full(u::AbstractArray{<:BLAS.BlasFloat, N},
v::AbstractArray{<:BLAS.BlasFloat, N}) where N
fu, fv = promote(u, v)
conv(fu, fv)
conv_full(fu, fv)
end

conv(u::AbstractArray{<:Integer, N}, v::AbstractArray{<:Integer, N}) where {N} =
round.(Int, conv(float(u), float(v)))
conv_full(u::AbstractArray{<:Integer, N}, v::AbstractArray{<:Integer, N}) where {N} =
round.(Int, conv_full(float(u), float(v)))

conv(u::AbstractArray{<:Number, N}, v::AbstractArray{<:Number, N}) where {N} =
conv(float(u), float(v))
conv_full(u::AbstractArray{<:Number, N}, v::AbstractArray{<:Number, N}) where {N} =
conv_full(float(u), float(v))

function conv(u::AbstractArray{<:Number, N},
function conv_full(u::AbstractArray{<:Number, N},
v::AbstractArray{<:BLAS.BlasFloat, N}) where N
conv(float(u), v)
conv_full(float(u), v)
end

function conv(u::AbstractArray{<:BLAS.BlasFloat, N},
function conv_full(u::AbstractArray{<:BLAS.BlasFloat, N},
v::AbstractArray{<:Number, N}) where N
conv(u, float(v))
conv_full(u, float(v))
end

function conv(A::AbstractArray{<:Number, M},
function conv_full(A::AbstractArray{<:Number, M},
B::AbstractArray{<:Number, N}) where {M, N}
if (M < N)
conv(cat(A, dims=N)::AbstractArray{eltype(A), N}, B)
conv_full(cat(A, dims=N)::AbstractArray{eltype(A), N}, B)
else
@assert M > N
conv(A, cat(B, dims=M)::AbstractArray{eltype(B), M})
conv_full(A, cat(B, dims=M)::AbstractArray{eltype(B), M})
end
end

function conv_same(u::AbstractArray{T, N}, v::AbstractArray{T, N}) where {T, N}
su = size(u)
sv = size(v)
conv_res = conv_full(u, v)
if N != 1
conv_res[Int(floor.(sv[1]/2 + 1)):Int(floor.(sv[1]/2) + su[1]), Int(floor.(sv[2]/2 + 1)):Int(floor.(sv[2]/2) + su[2]), axes(conv_res)[3:end]...]
Copy link
Member

Choose a reason for hiding this comment

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

Why only apply this to the first two dimensions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I always think that n-dimensional convolution only needs to be cut on the first two dimensions.

It may take me a few days to have free time to fix it.

else
conv_res[Int(floor.(sv[1]/2 + 1)):Int(floor.(sv[1]/2) + su[1])]
end
end

function conv_valid(u::AbstractArray{T, N}, v::AbstractArray{T, N}) where {T, N}
su = size(u)
sv = size(v)
conv_res = conv_full(u, v)
if N != 1
conv_res[sv[1]:su[1], sv[2]:su[2], axes(conv_res)[3:end]...]
else
conv_res[sv[1]:su[1]]
end
end

Expand Down
4 changes: 4 additions & 0 deletions test/dsp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ end
im_expectation = [1, 3, 6, 6, 5, 3]
a32 = convert(Array{Int32}, a)
@test conv(a, b) == expectation
@test conv(a, b, mode=:same) == expectation[2:5]
@test conv(a, b, mode=:valid) == expectation[3:4]
@test conv(a32, b) == expectation
fa = convert(Array{Float64}, a)
f32a = convert(Array{Float32}, a)
Expand Down Expand Up @@ -90,6 +92,8 @@ end
# Real Integers
a32 = convert(Array{Int32}, a)
@test conv(a, b) == expectation
@test conv(a, b, mode=:same) == expectation[2:4, 2:4]
@test conv(a, b, mode=:valid) == expectation[2:3, 2:3]
@test conv(a32, b) == expectation
# Floats
fa = convert(Array{Float64}, a)
Expand Down