[R] integration of a discrete function
Petr Pikal
petr.pikal at precheza.cz
Mon Apr 22 09:01:04 CEST 2002
On 20 Apr 2002 at 10:52, Giuseppe Pagnoni wrote:
> Dear R list
>
> I am looking for a function in R that computes the integration of a
> discrete curve, such as a power spectrum, in a specified interval (in
> my case, that would be 'power in a certain frequency band'). I found
> only functions, such as 'integrate', that perform adaptive quadrature
> on analytic functions, and not on a curve specified as a set of (x,y)
> pairs. I have the impression that I am not looking in the right
> place.... or I should code the algorithm myself?
>
I am sure there are some functions elsewhere, but here I constructed simple
functions which I use to compute an area for peaks from let say Xray diffraction
spectrum. It was intended for my use only and it is not documented at all.
The first one is used for selection from graphics (i.e. you have to use
plot(x,y)
first, use mouse pointer to specify an area of interest (one upper and oposite
lower corner, therefore the function replot) and using the mouse pointer again to
specify the lower and upper margin for area computing. Second function integ1
will enable you to set lower and upper margin directlz bz numbers.
Both functions will give you two numbers - area above from line x (y=0) and area
from above the line connecting "lowest" points in required interval (x1,x2).
Hope this helps.
#------------------------------------------------------------------
integ<-function (x,y)
{
replot(x,y)
meze<-locator(2)
dm<-meze$x[1]
hm<-meze$x[2]
abline(v=c(dm,hm),col=2)
vyber<-x<=hm&x>=dm
l<-length(x[vyber])
v<-diff(x[vyber])
z<-y[vyber][1:l-1]+y[vyber][2:l]
o<-z*v/2
osum<-sum(o)
o1<-(y[x==min(x[vyber])]+y[x==max(x[vyber])])*(max(x[vyber])-
min(x[vyber]))/2
cista<-osum-o1
return(c(osum,cista))
}
integ1<-function (x,y,dm=-Inf,hm=+Inf)
{
if(dm==-Inf)dm<-min(x)
if(hm==+Inf)hm<-max(x)
vyber<-x<=hm&x>=dm
l<-length(x[vyber])
v<-diff(x[vyber])
z<-y[vyber][1:l-1]+y[vyber][2:l]
o<-z*v/2
osum<-sum(o)
o1<-(y[x==min(x[vyber])]+y[x==max(x[vyber])])*(max(x[vyber])-
min(x[vyber]))/2
cista<-osum-o1
return(c(osum,cista))
}
#---------------------------------------------------------------------------------
replot <- function(x, y, type="l")
{
body <- locator(2)
plot(x, y, xlim=range(body$x), ylim=range(body$y), type=type)
}
> thanks for any suggestion
>
> Giuseppe
>
>
> -----------------
> Giuseppe Pagnoni
> Emory University
> Dept.of Psychiatry and Behavioral Sciences
> 1639 Pierce Dr.
> Suite 4000, WMB Bldg.
> Atlanta, GA, 30322
> phone: 404-7128431
> fax: 404-7273233
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.- 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._
Petr Pikal
petr.pikal at precheza.cz
p.pik at volny.cz
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list