# EXAMPLE - Estimating the Sinc function in the interval (-3pi, 3pi)
# Load the LATTICE package
require(lattice)
### (1) Create a 3D plot of the signal to be estimated by SKZS
# Create a random sample of size 250 for X = (X1, X2)
u <- seq(-3*pi, 3*pi, 3*pi/50)
v <- u
x1 <- sample(u, size = 250, replace=TRUE)
x2 <- sample(v, size = 250, replace=TRUE)
# Store x1 and x2 into a data frame
d <- data.frame(cbind(x1,x2))
# Keep only the unique (x1,x2) data points
df <- unique(d)
# Create the lattice of points
df <- expand.grid(x1 = x1, x2 = x2)
# Apply the Sinc function to x1 and x2 and store the result as z
df$z <- sin(sqrt(df$x1^2 + df$x2^2)) / sqrt(df$x1^2 + df$x2^2)
df$z[is.na(df$z)] <- 1
# Any point outside the circle of radius 3pi is set to 0. This
# provides a better picture of the outcome solely for the purposes
# of this example.
dst <- sqrt((df$x1 - 0)^2 + (df$x2 - 0)^2)
df$dist <- dst
df$z[df$dist > 3*pi] <- 0
# 3D plot of signal to be estimated
wireframe(z ~ x1 * x2, df, main = "Signal to be estimated", drape = TRUE,
colorkey = TRUE, scales = list(arrows = FALSE))
### (2) Create a 3D plot of the signal buried in noise
ez <- rnorm(length(df$z), mean = 0, sd = 1) * 1/2
df$z_noisy <- ez + df$z
wireframe(z_noisy ~ x1 * x2, df, main = "Signal buried in noise", drape = TRUE,
colorkey = TRUE, scales = list(arrows = FALSE))
### (3) Create the data set to be used in SKZS --- n = 4000
# same process as in (1)
x1 <- sample(u, size = 4000, replace = TRUE)
x2 <- sample(v, size = 4000, replace = TRUE)
d <- data.frame(cbind(x1,x2))
df <- unique(d)
df$z <- sin(sqrt(df$x1^2 + df$x2^2)) / sqrt(df$x1^2 + df$x2^2)
df$z[is.na(df$z)] <- 1
dst <- sqrt((df$x1 - 0)^2 + (df$x2 - 0)^2)
df$dist <- dst
df$z[df$dist > 3*pi] <- 0
ez <- rnorm(length(df$z),mean=0,sd=1)*1/2
df$z_noisy <- ez + df$z
dfn <- df[,-3:-4]
### (4) Create a 2D view of the 3D plots above
par(mfrow = c(2,1))
plot(df$z ~ df$x1, main = "2D plot of the signal to be estimated
n = 4000",
xlab = "x", ylab = "Z(x)")
plot(df$z_noisy ~ df$x1, main = "2D plot of the signal buried in noise
n = 4000",
xlab = "x", ylab = "Z(x)")
### (5) Execute SKZS on the data...arguments were chosen arbitrarily.
# Try other argument values to test the outcome
skzs(data=dfn, y=3, x1=1, x2=2, delta1=3, delta2=3, h1=3*pi/60, h2=3*pi/60, k=1,
show.edges=FALSE, plot=TRUE)
Run the code above in your browser using DataLab