Julia:如何计算包含NaN的向量的卷积



Julia 中的卷积函数具有以下行为:

               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.1 (2017-10-24 22:15 UTC)
 _/ |__'_|_|_|__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu
julia> conv([1,2,NaN],[1])
3-element Array{Float64,1}:
 NaN
 NaN
 NaN

这是一个错误吗?或者这与conv使用的FFT算法有关?

如果其中一个向量包含 NaN s,我如何计算 Julia 中的卷积(出于性能原因而不是手动(?在此示例中,结果应为:

3-element Array{Float64,1}:
   1.0
   2.0
 NaN  

这是因为 julia 中的 conv 函数使用 FFT。

"非手动"是什么意思?在 julia 中编写自己的卷积函数应该仍然很快。

例如

 function native_conv{T,V}(u::Array{T,1}, v::Array{V,1})
       m = length(u)
       n = length(v)
       w = zeros(promote_type(T,V), m+n-1)
       @inbounds begin
       for j in 1:m, k in 1:n
           w[j+k-1] += u[j]*v[k]
       end;end
       return w
  end

或者这可能更快(使用 FFT 将 NaN 设置为零然后重新规范化(。

编辑:当v(过滤器(很大时速度更快

function nanconv{T}(u::Array{T,1},v)
        nans = find(isnan, u)
        u = copy(u)
        u[nans] .= zero(T) # set nans2zero
        pass1 = conv(u,v)  # do convolution
        u[u.>0] .= one(T)   # 
        norm = conv(u,v)   # get normalizations (also gets rid of edge effects)
        u[:] .= one(T)     #
        norm .= norm./(conv(u,v)) # put edge effects back
        w = pass1 ./ norm       # normalize
        reshaped_nans = reshape_nans(nans,length(v))
        w[reshaped_nans] .= NaN           # reset Nans
        return w
end
function reshape_nans(nans,lv)
       out = zeros(Int, length(nans)*lv)
       j = 1;
       inc = lv - 1
       for i in nans
           inds = i:(i+inc)
           out[j:j+inc] = inds
           j += inc + 1 
       end
       out
end

最新更新