collect.absolute.energy: Find the region around the peak value that yields a...

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

View source: R/collect.absolute.energy.R

Description

finds the max value of a spectrum, then spreads out from that peak until it has determined a region whose area meets a fixed sum. Given the same sum, a low peak will yield a broader region than a high one.

Usage

1

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 [email protected]

See Also

spectrum collect.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
26
27
28
29
30
31
32
33
##---- 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) 
{
    total <- max(spk)
    N <- length(spk)
    lo <- hi <- match(total, spk)[1]
    while (total < desired.sum) {
        action.case <- switch(1 + 2 * (is.na(lo) || lo < 2 || 
            spk[lo - 1] < 0) + (is.na(hi) || is.na(spk[hi + 1]) || 
            spk[hi + 1] < 0), ifelse(spk[lo - 1] < spk[hi + 1], 
            1, 2), 1, 2, 3)
        if (action.case == 1) {
            lo <- lo - 1
            total <- total + spk[lo]
        }
        else if (action.case == 2) {
            hi <- hi + 1
            total <- total + spk[hi]
        }
        else {
            break
        }
    }
    if (is.na(lo) || lo < 1) 
        lo <- 1
    if (is.na(hi) || hi > N) 
        hi <- N
    return(c(lo, hi, total))
  }

landmarkacoustics/SoundPoints-R documentation built on Sept. 26, 2018, 9:08 a.m.