The R Project SVN R

Rev

Rev 61160 | Rev 68948 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 61160 Rev 61168
Line 12... Line 12...
12
}
12
}
13
\usage{
13
\usage{
14
convolve(x, y, conj = TRUE, type = c("circular", "open", "filter"))
14
convolve(x, y, conj = TRUE, type = c("circular", "open", "filter"))
15
}
15
}
16
\arguments{
16
\arguments{
17
  \item{x,y}{numeric sequences \emph{of the same length} to be
17
  \item{x, y}{numeric sequences \emph{of the same length} to be
18
    convolved.}
18
    convolved.}
19
  \item{conj}{logical; if \code{TRUE}, take the complex \emph{conjugate}
19
  \item{conj}{logical; if \code{TRUE}, take the complex \emph{conjugate}
20
    before back-transforming (default, and used for usual convolution).}
20
    before back-transforming (default, and used for usual convolution).}
21
  \item{type}{character; one of \code{"circular"}, \code{"open"},
21
  \item{type}{character; one of \code{"circular"}, \code{"open"},
22
    \code{"filter"} (beginning of word is ok).  For \code{circular}, the
22
    \code{"filter"} (beginning of word is ok).  For \code{circular}, the
Line 35... Line 35...
35
 
35
 
36
  Note that the usual definition of convolution of two sequences
36
  Note that the usual definition of convolution of two sequences
37
  \code{x} and \code{y} is given by \code{convolve(x, rev(y), type = "o")}.
37
  \code{x} and \code{y} is given by \code{convolve(x, rev(y), type = "o")}.
38
}
38
}
39
\value{
39
\value{
40
  If \code{r <- convolve(x,y, type = "open")}
40
  If \code{r <- convolve(x, y, type = "open")}
41
  and \code{n <- length(x)}, \code{m <- length(y)}, then
41
  and \code{n <- length(x)}, \code{m <- length(y)}, then
42
  \deqn{r_k = \sum_{i} x_{k-m+i} y_{i}}{r[k] = sum(i; x[k-m+i] * y[i])}
42
  \deqn{r_k = \sum_{i} x_{k-m+i} y_{i}}{r[k] = sum(i; x[k-m+i] * y[i])}
43
  where the sum is over all valid indices \eqn{i}, for
43
  where the sum is over all valid indices \eqn{i}, for
44
  \eqn{k = 1, \dots, n+m-1}.
44
  \eqn{k = 1, \dots, n+m-1}.
45
 
45
 
Line 59... Line 59...
59
\examples{
59
\examples{
60
require(graphics)
60
require(graphics)
61
 
61
 
62
x <- c(0,0,0,100,0,0,0)
62
x <- c(0,0,0,100,0,0,0)
63
y <- c(0,0,1, 2 ,1,0,0)/4
63
y <- c(0,0,1, 2 ,1,0,0)/4
64
zapsmall(convolve(x,y))         #  *NOT* what you first thought.
64
zapsmall(convolve(x, y))         #  *NOT* what you first thought.
65
zapsmall(convolve(x, y[3:5], type = "f")) # rather
65
zapsmall(convolve(x, y[3:5], type = "f")) # rather
66
x <- rnorm(50)
66
x <- rnorm(50)
67
y <- rnorm(50)
67
y <- rnorm(50)
68
# Circular convolution *has* this symmetry:
68
# Circular convolution *has* this symmetry:
69
all.equal(convolve(x,y, conj = FALSE), rev(convolve(rev(y),x)))
69
all.equal(convolve(x, y, conj = FALSE), rev(convolve(rev(y),x)))
70
 
70
 
71
n <- length(x <- -20:24)
71
n <- length(x <- -20:24)
72
y <- (x-10)^2/1000 + rnorm(x)/8
72
y <- (x-10)^2/1000 + rnorm(x)/8
73
 
73
 
74
Han <- function(y) # Hanning
74
Han <- function(y) # Hanning
75
       convolve(y, c(1,2,1)/4, type = "filter")
75
       convolve(y, c(1,2,1)/4, type = "filter")
76
 
76
 
77
plot(x,y, main="Using  convolve(.) for Hanning filters")
77
plot(x, y, main = "Using  convolve(.) for Hanning filters")
78
lines(x[-c(1  , n)      ], Han(y), col = "red")
78
lines(x[-c(1  , n)      ], Han(y), col = "red")
79
lines(x[-c(1:2, (n-1):n)], Han(Han(y)), lwd = 2, col = "dark blue")
79
lines(x[-c(1:2, (n-1):n)], Han(Han(y)), lwd = 2, col = "dark blue")
80
}
80
}
81
\keyword{math}
81
\keyword{math}
82
\keyword{dplot}
82
\keyword{dplot}