- file = ".coeffs"
- #.........
- #C function (from data.frame, type of IDs ??! force integers ? [yes])
- #return raw vector
- #take raw vector, append it (binary mode) to a file
-#TODO: appendCoeffs() en C --> serialize et append to file
+ errWarn <- function(ignored)
+ paste("Cannot convert argument' ",substitute(x),"' to logical", sep="")
+ if (!is.logical(x))
+ tryCatch({x <- as.logical(x)[1]; if (is.na(x)) stop()},
+ warning=errWarn, error=errWarn)
+ x
+}
+
+#' curvesToContribs
+#'
+#' Compute the discrete wavelet coefficients for each series, and aggregate them in
+#' energy contribution across scales as described in https://arxiv.org/abs/1101.4744v2
+#'
+#' @param curves [big.]matrix of series (in columns), of size L x n
+#' @inheritParams claws
+#'
+#' @return A matrix of size log(L) x n containing contributions in columns
+#'
+#' @export
+curvesToContribs <- function(series, wav_filt, contrib_type)
+{
+ series <- as.matrix(series)
+ L <- nrow(series)
+ D <- ceiling( log2(L) )
+ # Series are interpolated to all have length 2^D
+ nb_sample_points <- 2^D
+ apply(series, 2, function(x) {
+ interpolated_curve <- spline(1:L, x, n=nb_sample_points)$y
+ W <- wavelets::dwt(interpolated_curve, filter=wav_filt, D)@W
+ # Compute the sum of squared discrete wavelet coefficients, for each scale
+ nrj <- rev( sapply( W, function(v) ( sqrt( sum(v^2) ) ) ) )
+ if (contrib_type!="absolute")
+ nrj <- nrj / sum(nrj)
+ if (contrib_type=="logit")
+ nrj <- - log(1 - nrj)
+ unname( nrj )
+ })