From 2dbb52546400a7f14465cf005fb9456713e07e02 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 19 Mar 2013 09:34:19 +0000 Subject: [PATCH 1/2] Switch kde to use rfft, fix interlacing and integration --- src/kde.jl | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/kde.jl b/src/kde.jl index 65a220bb1..478245ee6 100644 --- a/src/kde.jl +++ b/src/kde.jl @@ -45,26 +45,16 @@ function kde(data::Vector, npoints::Integer) end # Transform to Fourier basis - ft = fft(grid) + ft = rfft(grid) # Find transform of KDE by convolving grid with FT of kernel # Hardcodes a Gaussian kernel - jmax = npoints2 - 1 - smooth = Array(Complex{Float64}, npoints) - smooth[1] = ft[1] - rj = 0.0 - for j = 1:jmax - rj = rj + 1.0 - fac = exp(-fac1 * rj^2) - j1 = j + 1 - j2 = j1 + npoints2 - smooth[j1] = fac * ft[j1] - smooth[j2] = fac * ft[j2] + for j = 2:length(ft) + ft[j] *= exp(-fac1 * j^2) end - smooth[npoints2 + 1] = exp(-fac1 * npoints^2) * ft[npoints2 + 1] # Invert the Fourier transform to get the KDE - smooth = real(ifft(smooth)) + smooth = irfft(ft,npoints) # Fix any noise that crept in for j in 1:npoints @@ -74,7 +64,5 @@ function kde(data::Vector, npoints::Integer) end # Return the grid over which KDE was calculated and KDE - # TODO: Values of smooth oscillate improperly - # TODO: Values of smooth are on wrong scale return dlo:step:(dhi - step), smooth end From 82b077ac04071486b58b5916867d34bcf41983d8 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 20 Mar 2013 07:33:44 +0000 Subject: [PATCH 2/2] fix convolution --- src/kde.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kde.jl b/src/kde.jl index 478245ee6..cdace5506 100644 --- a/src/kde.jl +++ b/src/kde.jl @@ -50,7 +50,7 @@ function kde(data::Vector, npoints::Integer) # Find transform of KDE by convolving grid with FT of kernel # Hardcodes a Gaussian kernel for j = 2:length(ft) - ft[j] *= exp(-fac1 * j^2) + ft[j] *= exp(-fac1 * (j-1)^2) end # Invert the Fourier transform to get the KDE