在Julia中实施Savitzky Golay

时间:2019-03-15 01:59:19

标签: julia

link,我在Julia中遇到了SG过滤器的实现。当我执行函数apply_filter时,返回错误-

UndefVarError: apply_filter not defined

我认为这是Julia(?)早期版本的实现。到目前为止,我正在Julia 1.0中执行此操作。找不到有关已定义类型的文档,这是我猜测与错误有关的地方

2 个答案:

答案 0 :(得分:0)

我想警告用户有关在Julia中使用功能savitzkyGolay的信息。 Scipy实现的结果不匹配(必须经过社区的多次检查迭代)

map()

答案 1 :(得分:0)

如果有帮助,我已经简化了代码。

using Pkg, LinearAlgebra, DSP, Plots
function vandermonde(halfWindow, polyDeg)
    x=[1.0*i for i in -halfWindow:halfWindow]

    n = polyDeg+1
    m = length(x)

    V = zeros(m, n)

    for i = 1:m
        V[i,1] = 1.0
    end
    for j = 2:n
        for i = 1:m
            V[i,j] = x[i] * V[i,j-1]
        end
    end

    return V
end

function SG(halfWindow, polyDeg)

    V = vandermonde(halfWindow,polyDeg)
    Q,R=qr(V)
    n = polyDeg+1
    m = 2*halfWindow+1
    R1 = vcat(R, zeros(m-n,n))
    sg = R1\Q'

    for i in 1:(polyDeg+1)

        sg[i,:] = sg[i,:]*factorial(i-1)

    end

    return sg'
end

function apply_filter(filter,signal)

    halfWindow = round(Int,(length(filter)-1)/2)

    padded_signal = [signal[1]*ones(halfWindow);signal;signal[end]*ones(halfWindow)]

    filter_cross_signal = conv(filter[end:-1:1], padded_signal)

    return filter_cross_signal[2*halfWindow+1:end-2*halfWindow]
end

这是我的用法:

mean_speed_unfiltered = readdlm("mean_speeds_raw_-2.txt")
sg = SG(500,2); # halt-window, polynomal degree

t =  10*10^(-3)#s #time of the simulation
dt = 0.1/γ; #time step
Nt = convert(Int, round(t/dt)); #number of iteration

#Smooth the mean speed curve: 

mean_speeds_smoothed = apply_filter(sg[:,1],mean_speed_unfiltered)

png(plot([j*dt for j=0:Nt] , mean_speeds_smoothed, title = "Smoothed mean speed over 
time", xlabel = "t (s)"), "Mean_speed_filtered_SG")

derivative_mean_speeds_smoothed = apply_filter(sg[:,2],mean_speed_unfiltered)

plt1 = plot(mean_speeds_smoothed,derivative_mean_speeds_smoothed, title = "derivative mean speed over speed", xlabel = "<v>(t) (s)", ylabel = "d<v(t)>/dt")
png(plt1, "Force_SG_1D2Lasers")

但是在我看来https://gist.github.com/lnacquaroli/c97fbc9a15488607e236b3472bcdf097#file-savitzkygolay-jl-L34中提供的代码更快。