mdmultispec silently modifies input

Issue #27 closed
Andy Nowacki created an issue

mdmultispec modifies the input vector x in place without declaring this in the docs:

julia> using Multitaper

julia> x = collect(1:10)
       y = rand(10)
       y′ = copy(y)
       mdmultispec(x, y′)
       y == y′
false

This is because x is modified in place in multispec_coef at src/MDmwps.jl:48:

function multispec_coef(tt, x, u, n, nfft, nfft2)
  x     .-= mean(x)

Two possible solutions:

  1. Modify multispec_coef so that x̂ = x .- mean(x), and using the new vector `x̂` which has the mean removed. (Preferred option probably.)
  2. Change the name of multispec_coef to multispec_coef!, document that the x argument is updated, and do likewise for mdmultispecmdmultispec!

Comments (3)

  1. Andy Nowacki reporter

    This is great, thanks.

    Usually when bugs are found, I would also add a test to check things work as per the fix. I’m not sure that is so strictly needed here, though, and I’m happy with this fix.

  2. Log in to comment