回复 第28楼 的 Ihavenothing:
convolve有很多限制,其中一个就是要求两个向量等长。
还好有人做了改进,出现了一个
<br />
convolve2 <- function(x, y, type = c("circular", "open", "filter"))<br />
{<br />
type <- match.arg(type)<br />
nx <- length(x)<br />
ny <- length(y)<br />
if (type == "circular") {<br />
nz <- max(nx, ny)<br />
} else {<br />
nz0 <- nx + ny - 1<br />
nz <- 2^ceiling(log2(nz0))<br />
}<br />
if (nz > nx)<br />
x[(nx+1):nz] <- as.integer(0)<br />
if (nz > ny)<br />
y[(ny+1):nz] <- as.integer(0)<br />
fz <- fft(x) * fft(y)<br />
z <- fft(fz, inverse=TRUE) / nz<br />
if (type == "open") {<br />
z <- z[1:nz0]<br />
} else {<br />
if (type == "filter")<br />
z <- z[1:nx]<br />
}<br />
if (is.numeric(x) && is.numeric(y))<br />
z <- Re(z)<br />
if (is.integer(x) && is.integer(y))<br />
z <- as.integer(round(z))<br />
z<br />
}<br />
这样对上面的例子就有
<br />
x<-c(1,4,3)<br />
y<-c(2,1)<br />
convolve2(x,y,type="o")<br />
[1] 2 9 10 3
</p>