I have been experimenting with convolve().
What I know about convolution I learned from engineering (they call this
stuff linear systems theory), not statistics, so maybe this is all just a
matter of different conventions. BUT I notice very weird things with
convolve().
1. First example, from the classic Bracewell The Fourier transform and its
applications, chap 3 (p.32 in 2nd edition):
{2 2 3 3 4} * {1 1 2} = {2 4 9 10 13 10 8}
where * denotes convolution (discrete in this case). See bottom of this
email for his definition of convolution.
In R I get:
x<-c(2,2,3,3,4)
h<-c(1,1,2)
convolve(x,h,type="o")
[1] 4 6 10 11 14 7 4
I think "open" is the right type. I want to get length(x)+length(h)-1
terms in the convolution output, which is what "open" gives. I
definitely
don't want "circular"
BTW, the help for convolve says:
If `r <- convolve(x,y, conj=TRUE, type)' and `n <-
length(x)', then
r[k] = sum(i=1,..,n; x[i] * y[k-i])
for k = 1,...,n.
There seems to be a typo: shouldn't it be
r <- convolve(x,y, conj=TRUE, type="open") or "filter" or
whatever
2.
x<-seq(1,100)
y<-ifelse(x<=50, 0, 1)
#y is a discrete unit step or Heaviside function
h<-dnorm(x, mean=50, sd=10)
# the filter is a Gaussian
plot(convolve(y,h, type="open"))
#weird
points(pnorm(x,mean=50,sd=10))
#what I expected, since convolution with a step is integration
The definition of convolution used by Bracewell (p. 32) is
out[i-1] = Sum f[j] g[i-j]
j
where out[] is the convolution (he calls it serial product here).
This definition seems essentially the same as the one in the R help.
Is there something wrong with convolve(), or is it just me?
Thanks very much for any help.
Bill
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thu, 18 Nov 1999, Bill Simpson wrote:> 1. First example, from the classic Bracewell The Fourier transform and its > applications, chap 3 (p.32 in 2nd edition): > {2 2 3 3 4} * {1 1 2} = {2 4 9 10 13 10 8} > where * denotes convolution (discrete in this case). See bottom of this > email for his definition of convolution. > In R I get: > x<-c(2,2,3,3,4) > h<-c(1,1,2) > convolve(x,h,type="o") > [1] 4 6 10 11 14 7 4 > I think "open" is the right type. I want to get length(x)+length(h)-1 > terms in the convolution output, which is what "open" gives. I definitely > don't want "circular"The difference between the definitions is a sign change in the index of the filter: f[i-j] vs. f[j-i] (if the filter is symmetric then it wouldn't matter). If you do convolve(x,rev(h),type="o") you'll get the answer from Bracewell. The definition in R makes more sense to me, but that's personal I guess.> > h<-dnorm(x, mean=50, sd=10) > # the filter is a Gaussian > > plot(convolve(y,h, type="open")) > #weird > points(pnorm(x,mean=50,sd=10)) > #what I expected, since convolution with a step is integrationHere the problem is that the filter is "running off the end". If you extend the filter with ones y <- c(y,rep(1,100)) and redo the analysis you'll see that it levels off at 1 for a while and then drops back down. Hope that helps ... (as usual, someone else has probably answered this one already). Ben -- -------------------------------------------- Ben Bolker bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker 318 Carr Hall/Box 118525 tel: (352) 392-5697 Gainesville, FL 32611-8525 fax: (352) 392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._