| 2 |
r |
1 |
\name{convolve}
|
| 15702 |
hornik |
2 |
\alias{convolve}
|
| 2 |
r |
3 |
\title{Fast Convolution}
|
| 15702 |
hornik |
4 |
\description{
|
|
|
5 |
Use the Fast Fourier Transform to compute the several kinds of
|
|
|
6 |
convolutions of two sequences.
|
|
|
7 |
}
|
| 2 |
r |
8 |
\usage{
|
| 5055 |
pd |
9 |
convolve(x, y, conj = TRUE, type = c("circular", "open", "filter"))
|
| 2 |
r |
10 |
}
|
|
|
11 |
\arguments{
|
| 15702 |
hornik |
12 |
\item{x,y}{numeric sequences \emph{of the same length} to be
|
|
|
13 |
convolved.}
|
|
|
14 |
\item{conj}{logical; if \code{TRUE}, take the complex \emph{conjugate}
|
|
|
15 |
before back-transforming (default, and used for usual convolution).}
|
|
|
16 |
\item{type}{character; one of \code{"circular"}, \code{"open"},
|
|
|
17 |
\code{"filter"} (beginning of word is ok). For \code{circular}, the
|
|
|
18 |
two sequences are treated as \emph{circular}, i.e., periodic.
|
| 5055 |
pd |
19 |
|
| 15702 |
hornik |
20 |
For \code{open} and \code{filter}, the sequences are padded with
|
|
|
21 |
\code{0}s (from left and right) first; \code{"filter"} returns the
|
|
|
22 |
middle sub-vector of \code{"open"}, namely, the result of running a
|
|
|
23 |
weighted mean of \code{x} with weights \code{y}.}
|
| 2 |
r |
24 |
}
|
| 5055 |
pd |
25 |
\details{
|
| 6632 |
maechler |
26 |
The Fast Fourier Transform, \code{\link{fft}}, is used for efficiency.
|
| 5055 |
pd |
27 |
|
| 6632 |
maechler |
28 |
The input sequences \code{x} and \code{y} must have the same length if
|
|
|
29 |
\code{circular} is true.
|
|
|
30 |
|
|
|
31 |
Note that the usual definition of convolution of two sequences
|
|
|
32 |
\code{x} and \code{y} is given by \code{convolve(x, rev(y), type = "o")}.
|
| 5055 |
pd |
33 |
}
|
| 2 |
r |
34 |
\value{
|
| 6632 |
maechler |
35 |
If \code{r <- convolve(x,y, type = "open")}
|
|
|
36 |
and \code{n <- length(x)}, \code{m <- length(y)}, then
|
|
|
37 |
\deqn{r_k = \sum_{i} x_{k-m+i} y_{i}}{r[k] = sum(i; x[k-m+i] * y[i])}
|
|
|
38 |
where the sum is over all valid indices \eqn{i}, for \eqn{k = 1,\dots, n+m-1}
|
| 1357 |
maechler |
39 |
|
| 6632 |
maechler |
40 |
If \code{type == "circular"}, \eqn{n = m} is required, and the above is
|
|
|
41 |
true for \eqn{i , k = 1,\dots,n} when
|
|
|
42 |
\eqn{x_{j} := x_{n+j}}{x[j] := x[n+j]} for \eqn{j < 1}.
|
| 2 |
r |
43 |
}
|
|
|
44 |
\references{
|
| 7324 |
ripley |
45 |
Brillinger, D. R. (1981)
|
| 6632 |
maechler |
46 |
\emph{Time Series: Data Analysis and Theory}, Second Edition.
|
|
|
47 |
San Francisco: Holden-Day.
|
| 2 |
r |
48 |
}
|
| 13062 |
maechler |
49 |
\seealso{\code{\link{fft}}, \code{\link{nextn}}, and particularly
|
| 27497 |
ripley |
50 |
\code{\link[stats]{filter}} (from the \pkg{stats} package) which may be
|
| 13062 |
maechler |
51 |
more appropriate.
|
| 2 |
r |
52 |
}
|
| 1357 |
maechler |
53 |
\examples{
|
|
|
54 |
x <- c(0,0,0,100,0,0,0)
|
|
|
55 |
y <- c(0,0,1, 2 ,1,0,0)/4
|
| 15702 |
hornik |
56 |
zapsmall(convolve(x,y)) # *NOT* what you first thought.
|
| 5055 |
pd |
57 |
zapsmall(convolve(x, y[3:5], type="f")) # rather
|
| 27733 |
ripley |
58 |
x <- rnorm(50)
|
|
|
59 |
y <- rnorm(50)
|
| 6098 |
pd |
60 |
# Circular convolution *has* this symmetry:
|
| 27733 |
ripley |
61 |
all.equal(convolve(x,y, conj = FALSE), rev(convolve(rev(y),x)))
|
| 5055 |
pd |
62 |
|
|
|
63 |
n <- length(x <- -20:24)
|
|
|
64 |
y <- (x-10)^2/1000 + rnorm(x)/8
|
|
|
65 |
|
|
|
66 |
Han <- function(y) # Hanning
|
|
|
67 |
convolve(y, c(1,2,1)/4, type = "filter")
|
|
|
68 |
|
|
|
69 |
plot(x,y, main="Using convolve(.) for Hanning filters")
|
|
|
70 |
lines(x[-c(1 , n) ], Han(y), col="red")
|
|
|
71 |
lines(x[-c(1:2, (n-1):n)], Han(Han(y)), lwd=2, col="dark blue")
|
| 1357 |
maechler |
72 |
}
|
| 286 |
maechler |
73 |
\keyword{math}
|
|
|
74 |
\keyword{dplot}
|