JuliaDSP / DSP.jl

Filter design, periodograms, window functions, and other digital signal processing functionality
https://docs.juliadsp.org/stable/contents/
Other
381 stars 110 forks source link

Resample segmentation fault for rate::AbstractFloat #262

Closed 1oly closed 8 months ago

1oly commented 5 years ago

Hello,

I'm getting a segmentation fault for resample when rate is a certain float value (example coming from an actual script where s and y are audio signals):

using DSP
s = rand(1920000);
y = rand(2629632);
rate = length(s)/length(y)
X = resample(y,rate)

by using Rational type, I don't see a segmentation fault:

using DSP
s = rand(1920000);
y = rand(2629632);
rate = length(s)//length(y) # Rational here
X = resample(y,rate)

I tried to look in resample_filter https://github.com/JuliaDSP/DSP.jl/blob/270e6f25de3f9176493c3bfed863bf0ec94289ac/src/Filters/design.jl#L606 but cannot reproduce manually.

Will be happy to help out if this is not an issue on my side.

Thanks!

Error:

signal (11): Segmentation fault: 11
in expression starting at no file:0
setindex! at ./array.jl:767 [inlined]
filt! at /Users/oliver/.julia/packages/DSP/efNaR/src/Filters/stream_filt.jl:635
filt at /Users/oliver/.julia/packages/DSP/efNaR/src/Filters/stream_filt.jl:648
resample at /Users/oliver/.julia/packages/DSP/efNaR/src/Filters/stream_filt.jl:703
resample at /Users/oliver/.julia/packages/DSP/efNaR/src/Filters/stream_filt.jl:708
unknown function (ip: 0x11f37f318)
jl_fptr_trampoline at /Users/osx/buildbot/slave/package_osx64/build/src/gf.c:1864
do_call at /Users/osx/buildbot/slave/package_osx64/build/src/interpreter.c:323
eval_stmt_value at /Users/osx/buildbot/slave/package_osx64/build/src/interpreter.c:362 [inlined]
eval_body at /Users/osx/buildbot/slave/package_osx64/build/src/interpreter.c:759
jl_interpret_toplevel_thunk_callback at /Users/osx/buildbot/slave/package_osx64/build/src/interpreter.c:885
unknown function (ip: 0xfffffffffffffffe)
unknown function (ip: 0x10e2c7caf)
unknown function (ip: 0xffffffffffffffff)
jl_interpret_toplevel_thunk at /Users/osx/buildbot/slave/package_osx64/build/src/interpreter.c:894
jl_toplevel_eval_flex at /Users/osx/buildbot/slave/package_osx64/build/src/toplevel.c:764
jl_toplevel_eval at /Users/osx/buildbot/slave/package_osx64/build/src/toplevel.c:773 [inlined]
jl_toplevel_eval_in at /Users/osx/buildbot/slave/package_osx64/build/src/toplevel.c:793
eval at ./boot.jl:328
eval_user_input at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:85
macro expansion at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/REPL/src/REPL.jl:117 [inlined]
#26 at ./task.jl:259
jl_apply at /Users/osx/buildbot/slave/package_osx64/build/src/./julia.h:1571 [inlined]
start_task at /Users/osx/buildbot/slave/package_osx64/build/src/task.c:572
Allocations: 5752859 (Pool: 5751833; Big: 1026); GC: 14
Segmentation fault: 11

and versioninfo:

Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin14.5.0)
  CPU: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

and Pkg.status:

  [537997a7] AbstractPlotting v0.9.6 #master (https://github.com/JuliaPlots/AbstractPlotting.jl.git)
  [03ede2b8] AeroAcoustics v0.0.4 [`../../dev/AeroAcoustics`]
  [c7e460c6] ArgParse v0.6.2
  [c52e3926] Atom v0.7.15
  [6e4b80f9] BenchmarkTools v0.4.2
  [b99e7846] BinaryProvider v0.5.3
  [5ae59095] Colors v0.9.5
  [717857b8] DSP v0.5.2
  [a93c6f00] DataFrames v0.17.1
  [b4f34e82] Distances v0.8.0
  [e30172f5] Documenter v0.21.5
  [8f5d6c58] EzXML v0.9.1
  [7a1cc6ca] FFTW v0.2.4
  [add2ef01] GDAL v0.2.0
  [e9467ef8] GLMakie v0.0.5 #master (https://github.com/JuliaPlots/GLMakie.jl.git)
  [28b8d3ca] GR v0.38.1+ #master (https://github.com/jheinen/GR.jl.git)
  [f67ccb44] HDF5 v0.11.0
  [7073ff75] IJulia v1.17.0
  [86fae568] ImageView v0.8.2
  [c601a237] Interact v0.9.1
  [42fd0dbc] IterativeSolvers v0.8.1
  [aa1ae85d] JuliaInterpreter v0.1.1 #master (https://github.com/JuliaDebug/JuliaInterpreter.jl)
  [e5e0dc1b] Juno v0.5.5
  [b964fa9f] LaTeXStrings v1.0.3
  [4345ca2d] Loess v0.5.0
  [ee78f7c6] Makie v0.9.3 #master (https://github.com/JuliaPlots/Makie.jl.git)
  [dbd62bd0] MakieGallery v0.0.6
  [4d0d745f] PDFIO v0.1.1
  [8314cec4] PGFPlotsX v0.3.5
  [9b87118b] PackageCompiler v0.6.3+ [`~/.julia/dev/PackageCompiler`]
  [d96e819e] Parameters v0.10.3
  [f0f68f2c] PlotlyJS v0.12.3
  [91a5bcdd] Plots v0.23.1
  [c46f51b8] ProfileView v0.4.0
  [92933f4c] ProgressMeter v0.9.0
  [d330b81b] PyPlot v2.8.0
  [295af30f] Revise v1.1.0
  [90137ffa] StaticArrays v0.10.3
  [a759f4b9] TimerOutputs v0.5.0
  [b8865327] UnicodePlots v1.1.0
  [8149f6b0] WAV v1.0.1
martinholters commented 5 years ago

The @inbounds here seems fishy: https://github.com/JuliaDSP/DSP.jl/blob/42634c097f22f8b48b822a144b6734df9e569552/src/Filters/stream_filt.jl#L635 Removing it, I get

julia> X = resample(y,rate)
ERROR: BoundsError: attempt to access 1920000-element Array{Float64,1} at index [1920001]
Stacktrace:
 [1] setindex! at ./array.jl:767 [inlined]
 [2] filt!(::Array{Float64,1}, ::FIRFilter{DSP.Filters.FIRArbitrary{Float64}}, ::Array{Float64,1}) at DSP/src/Filters/stream_filt.jl:635
 [3] filt(::FIRFilter{DSP.Filters.FIRArbitrary{Float64}}, ::Array{Float64,1}) at DSP/src/Filters/stream_filt.jl:648
 [4] resample(::Array{Float64,1}, ::Float64, ::Array{Float64,1}) at DSP/src/Filters/stream_filt.jl:703
 [5] resample(::Array{Float64,1}, ::Float64) at DSP/src/Filters/stream_filt.jl:708
 [6] top-level scope at none:0

There's two issues here:

  1. filt! should not assume buffer to be of sufficient size. If we keep the @inbounds, we need to verify before the loop.
  2. In this case, the buffer should have been allocated with (at least) the required size. I'm not quite into the resampling machinery, though, so I don't know where/how to fix this.
1oly commented 5 years ago

Thanks for the quick reply and for tracking down the issue to @inbounds. Been looking a bit more and found this line: https://github.com/JuliaDSP/DSP.jl/blob/42634c097f22f8b48b822a144b6734df9e569552/src/Filters/stream_filt.jl#L365 from this commit: https://github.com/JuliaDSP/DSP.jl/commit/6a4e6d3fe5aa08d738aa3060099b8517252e25af

Looks like this has been an issue before since tests were disabled and I do get different results for inputlength for Arbitrary or Rational here : https://github.com/JuliaDSP/DSP.jl/blob/42634c097f22f8b48b822a144b6734df9e569552/src/Filters/stream_filt.jl#L699

galenlynch commented 5 years ago

@1oly #272 should hopefully fix the segfaults, but not address the underlying problem. However, using master should produce more useful errors. Please let us know if you get any more insight into what is causing these errors.

1oly commented 5 years ago

Thanks for addressing the segfault. Will get back if I find the culprit...

wheeheee commented 8 months ago

Closing in favour of #317