File: kernel2d.R

package info (click to toggle)
r-cran-sparr 2.3-16-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 884 kB
  • sloc: makefile: 2
file content (23 lines) | stat: -rw-r--r-- 723 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Bivariate kernel function centered at 0,0 with isotropic bandwidth h
kernel2d <- function(x, y, h) {
  1/(2*pi*h*h)*exp(-0.5*(x*x+y*y)/(h*h))
}

# Approximation of definite integral of f over a grid with gridsize dx x dy via summation
dintegral <- function(f, dx, dy) {
  sum(f)*dx*dy
}

# Fast fourier transform of a 2D Gaussian based on the continuous FT, where
# sigma is the standard deviation, ds,dt is the time resolution in x,y
# and 2n*2n the number of points

kernel2d_fft <- function(sigma, ds, dt, n) {
  kernel_fft <- function(sigma., dt., n.) {
    f <- c(0:(n.-1),-n.:-1)*pi*sigma./(n.*dt.)
    exp(-0.5*f*f)/dt.
  }
  fZ.x <- kernel_fft(sigma, ds, n)
  fZ.y <- kernel_fft(sigma, dt, n)
  fZ.y %*% t(fZ.x)
}