#Contains cumper, which is the cumulative periodogram routine
#Calls minfunc, below.
#data is the time series.
#im is the N/m, upper cut-off.
#l is the lower cut-off
#1997, Vadim Teverovsky.

cumper <- function(data, im = 4, l = 10)
{
        n <- length(data)
        m <- n %/% im
        pgram <- cumsum(per(data)[(l + 2):(m + 1)])
        p <- c(1, 0.5)
        fit <- nlminb(start = p, objective = minfunc, n1 = n, m = m, l1 = l, 
                peri = pgram, lower = c( - Inf, 0), upper = c(Inf, 1))$
                parameters
        return(fit[2])
}

minfunc <- function(param, n1, m, l1, peri)
{
        g <- param[1]
        h <- param[2]
        z <- c((l1 + 1):m)
        freq <- (2 * pi)/n1 * z
        freq1 <- (2 * pi)/n1 * l1
        result <- sum((peri - g * freq^(2 - 2 * h) + g * freq1^(2 - 2 * h))^2)
        #       cat("H = ", h, "G = ", g, "R = ", result, "\n")
        drop(result)
}
