#################################################################### ## ## File: aux.r ## ## Description: Miscelaneous functions for clustering with kcca ## ## Modified: june 2010 ## #################################################################### ####################################################### # Transforms a matrix of data (one observation by row) # into an array where position[ , , i] gives # the smoothed modulus of the i-th cwt observation ######################################################## ##NOTE: renvoie une matrice 3D toCWT <- function(X, sw= 0, tw= 0, swabs= 0, nvoice= 12, noctave= 5, s0= 2, w0= 2*pi, lt= 24, dt= 0.5, spectra = FALSE, smooth = TRUE, scaled = FALSE, scalevector) { noctave <- adjust.noctave(lt, dt, s0, tw, noctave) if(missing(scalevector)) scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0 res <- lapply(1:nrow(X), function(n) { tsX <- ts( X[n,] ) tsCent <- tsX - mean(tsX) if(scaled) tsCent <- ts(scale(tsCent)) tsCent.cwt <- cwt.ts(tsCent, s0, noctave, nvoice, w0) tsCent.cwt } ) if( spectra ) res <- lapply(res, function(l) Mod(l)^2 ) if( smooth ) res <- lapply(res, smCWT, swabs = swabs, tw = tw, dt = dt, scalevector = scalevector) resArray <- array(NA, c(nrow(res[[1]]), ncol(res[[1]]), length(res))) for( l in 1:length(res) ) resArray[ , , l] <- res[[l]] resArray } # =============================================================== smCWT <- function(CWT, sw= 0, tw= 0, swabs= 0, nvoice= 12, noctave= 2, s0= 2, w0= 2*pi, lt= 24, dt= 0.5, scalevector ) { # noctave <- adjust.noctave(lt, dt, s0, tw, noctave) # scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0 wsp <- Mod(CWT) smwsp <- smooth.matrix(wsp, swabs) smsmwsp <- smooth.time(smwsp, tw, dt, scalevector) smsmwsp } # =============================================================== toDWT <- function(x, filter.number = 6, family = "DaubLeAsymm") { x2 <- spline(x, n = 2^ceiling( log(length(x), 2) ), method = 'natural')$y Dx2 <- wd(x2, family = family, filter.number = filter.number)$D Dx2 } # =============================================================== contrib <- function(x) { J <- log( length(x)+1, 2) nrj <- numeric(J) t0 <- 1 t1 <- 0 for( j in 1:J ) { t1 <- t1 + 2^(J-j) nrj[j] <- sqrt( sum( x[t0:t1]^2 ) ) t0 <- t1 + 1 } return(nrj) } # ========================================= distance for coh === coherence <- function( x, y) { J <- log(length(x) + 1, 2) t0 <- 1 sg2_x <- 0 sg2_y <- 0 sg_xy <- 0 for(j in 0:(J - 1)) { t1 <- t0 + 2^(J - j)/2 - 1 tt <- t0:t1 sg2_x <- sg2_x + mean(x[t0:t1]^2) sg2_y <- sg2_y + mean(y[t0:t1]^2) sg_xy <- sg_xy + mean(x[t0:t1] * y[t0:t1]) t0 <- t1 + 1 } res <- sg_xy^2 / sg2_x / sg2_y res } vect2mat <- function(vect){ vect <- as.vector(vect) matrix(vect[-(1:2)], delta, lscvect) } # ========================================= # myimg for graphics jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000")) myimg <- function(MAT, x = 1:nrow(MAT), y = 1:col(MAT), ... ) filled.contour( x = x, y = y, z = MAT, xlab= 'Time', ylab= 'scale', color.palette = jet.colors, ... )