Skip to content

Commit 66acd7d

Browse files
authored
Fix conv for empty arrays (#628)
1 parent d08699a commit 66acd7d

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DSP"
22
uuid = "717857b8-e6f2-59f4-9121-6e50c889abd2"
3-
version = "0.8.2"
3+
version = "0.8.3"
44

55
[deps]
66
Bessels = "0e736298-9ec6-45e8-9647-e4fc86a2fe38"

src/dspbase.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -717,24 +717,24 @@ function conv!(
717717
return (first(au)+first(av) : last(au)+last(av)) .- offset
718718
end)
719719

720-
if algorithm===:auto
720+
if algorithm === :auto
721721
algorithm = T <: FFTTypes ? :fast : :direct
722722
end
723-
if algorithm===:fast
723+
if algorithm === :fast
724724
if length(u) * length(v) < 2^16 # TODO: better heuristic
725725
algorithm = :direct
726726
else
727727
algorithm = :fft
728728
end
729729
end
730-
if algorithm===:direct
730+
if algorithm === :direct || any(isempty, (u, v)) # ensure correct handling of empty arrays
731731
return _conv_td!(out, output_indices, u, v)
732732
else
733733
if output_indices != CartesianIndices(out)
734734
fill!(out, zero(eltype(out)))
735735
end
736736
os_nffts = length(u) >= length(v) ? map(optimalfftfiltlength, size(v), size(u)) : map(optimalfftfiltlength, size(u), size(v))
737-
if algorithm===:fft
737+
if algorithm === :fft
738738
if any(os_nffts .< size(output_indices))
739739
algorithm = :fft_overlapsave
740740
else

test/dsp.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# This file was formerly a part of Julia. License is MIT: https://julialang.org/license
22
# TODO: parameterize conv tests
33
using Test, OffsetArrays
4-
using DSP: filt, filt!, deconv, conv, xcorr,
5-
optimalfftfiltlength, unsafe_conv_kern_os!, _conv_kern_fft!
4+
using DSP: filt, filt!, deconv, conv, conv!, xcorr,
5+
optimalfftfiltlength, unsafe_conv_kern_os!, _conv_kern_fft!,
66
nextfastfft
77

88

@@ -38,6 +38,16 @@ end
3838

3939
@test optimalfftfiltlength(1, 3) == 1 # Should be at least the first input
4040

41+
# check that empty arrays are handled correctly (issue #168)
42+
for N in (1, 2, 3), algorithm in (:direct, :fft, :fft_simple, :fft_overlapsave)
43+
A = rand(ntuple(_ -> 5, N)...)
44+
B = Array{Float64}(undef, ntuple(_ -> 0, N))
45+
out = zeros(ntuple(_ -> 4, N))
46+
@test conv(A, B; algorithm) == out
47+
@test conv(B, A; algorithm) == out
48+
@test conv(B, B; algorithm) == B
49+
end
50+
4151
@testset "conv-1D" begin
4252
# Convolution
4353
a = [1, 2, 1, 2]

0 commit comments

Comments
 (0)