使用 R 中的 savitzky-golay/sgolayfilt(信号)平滑栅格堆栈

Smoothen rasterstack using the savitzky-golay/sgolayfilt (signal) in R

我一直在尝试 运行 R 中 rasterstack 上的 savitzky-golar 过滤器无济于事。示例如下:

library (raster)
library (zoo)
library (signal)
r <- raster(ncol=10, nrow=10)
r[]=1:ncell(r)
S <- stack(r,r,r,r,r,r,r,r,r,r,r,r)

##function for filter
fun=function(x) { m = sgolayfilt(x,na.spline=T);m}
s1<-calc(S, fun)
##This was an alternative function I used:
fun <- function(x) {
 v=as.vector(x)
 z=substituteNA(v, type="mean")
 s1.ts2 = ts(z, start=c(2004,1), end=c(2004,12), frequency=12)
x=sgolayfilt(s1.ts2)}

下面的错误是我得到的:

Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : 
cannot use this function

此功能可能缺少什么?

首先使用一些示例数据检查您的函数。

示例数据

d <- S[1]
d
##layer.1 layer.2 layer.3 layer.4 layer.5 layer.6 layer.7 layer.8 layer.9 layer.10 layer.11 layer.12
##[1,]       1       1       1       1       1       1       1       1       1        1        1        1

现在测试

fun=function(x) { sgolayfilt(x, na.spline=T) }
fun(d)
##Error in sgolayfilt(x, na.spline = T) : unused argument (na.spline = T)

并修正

fun=function(x) { sgolayfilt(x) }
fun(d)
## [1] 1 1 1 1 1 1 1 1 1 1 1 1

下一个

fun2 <- function(x) {
   v=as.vector(x)
   z=substituteNA(v, type="mean")
   s1.ts2 = ts(z, start=c(2004,1), end=c(2004,12), frequency=12)
   sgolayfilt(s1.ts2)  
   # note that I removed the assignment to x
}

fun2(S)
## Error in fun2(S) : could not find function "substituteNA"

我不知道你想在这里做什么,所以我无法修复它。