collect.energy: finds a region the under the highest point in a 1-D landscape...

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/collect.energy.R

Description

goes to the max of the input vector, then walks out in either direction until the user-specified amplitude sum is achieved

Usage

1
collect.energy(spk, desired.sum)

Arguments

spk

a vector that corresponds to a peak trace.

desired.sum

the area under the curve that you are looking for

Details

this function assumes all of spk >= 0, and will perform oddly if any values are negative.

Value

a vector with three entries:

lo

the index of the beginning of the region (inclusive)

hi

the index of the end of the region (inclusive)

total

the sum of the heights within the region

Author(s)

Benjamin N. Taft ben.taft@landmarkacoustics.com

See Also

spectrum collect.absolute.energy

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (spk, desired.sum) 
{
    ends <- rep(match(max(spk), spk), 2)
    N <- length(spk)
    total <- 0
    current.step <- 1
    while (total < desired.sum) {
        temp.next <- c(ifelse(ends[1] > 1, spk[ends[1] - 1], 
            -Inf), ifelse(ends[2] < N, spk[ends[2] + 1], -Inf))
        now <- 1 + (temp.next[1] < temp.next[2])
        if (is.infinite(temp.next[now])) 
            break
        height <- spk[ends[now]] - temp.next[now]
        width <- ifelse(height > 0, current.step, 1)
        ends[now] <- ends[now] + ifelse(now == 1, -1, 1)
        total <- total + width * height
        current.step <- current.step + 1
    }
    return(c(ends, total))
  }

landmarkacoustics/SoundPoints-R documentation built on May 29, 2019, 9:14 a.m.