1 ####################################################################
5 ## Description: Miscelaneous functions for clustering with kcca
9 ####################################################################
12 #######################################################
14 # Transforms a matrix of data (one observation by row)
15 # into an array where position[ , , i] gives
16 # the smoothed modulus of the i-th cwt observation
18 ########################################################
21 ##NOTE: renvoie une matrice 3D
22 toCWT <- function(X, sw= 0, tw= 0, swabs= 0,
23 nvoice= 12, noctave= 5,
24 s0= 2, w0= 2*pi, lt= 24, dt= 0.5,
25 spectra = FALSE, smooth = TRUE,
28 { noctave <- adjust.noctave(lt, dt, s0, tw, noctave)
29 if(missing(scalevector))
30 scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0
31 res <- lapply(1:nrow(X), function(n)
33 tsCent <- tsX - mean(tsX)
34 if(scaled) tsCent <- ts(scale(tsCent))
35 tsCent.cwt <- cwt.ts(tsCent, s0, noctave, nvoice, w0)
38 if( spectra ) res <- lapply(res, function(l) Mod(l)^2 )
39 if( smooth ) res <- lapply(res, smCWT, swabs = swabs,
41 scalevector = scalevector)
42 resArray <- array(NA, c(nrow(res[[1]]), ncol(res[[1]]),
44 for( l in 1:length(res) ) resArray[ , , l] <- res[[l]]
49 # ===============================================================
51 smCWT <- function(CWT, sw= 0, tw= 0, swabs= 0,
52 nvoice= 12, noctave= 2, s0= 2, w0= 2*pi,
53 lt= 24, dt= 0.5, scalevector )
55 # noctave <- adjust.noctave(lt, dt, s0, tw, noctave)
56 # scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0
58 smwsp <- smooth.matrix(wsp, swabs)
59 smsmwsp <- smooth.time(smwsp, tw, dt, scalevector)
64 # ===============================================================
66 toDWT <- function(x, filter.number = 6, family = "DaubLeAsymm")
67 { x2 <- spline(x, n = 2^ceiling( log(length(x), 2) ),
69 Dx2 <- wd(x2, family = family, filter.number = filter.number)$D
73 # ===============================================================
75 contrib <- function(x)
76 { J <- log( length(x)+1, 2)
82 nrj[j] <- sqrt( sum( x[t0:t1]^2 ) )
89 # ========================================= distance for coh ===
91 coherence <- function( x, y)
92 { J <- log(length(x) + 1, 2)
98 { t1 <- t0 + 2^(J - j)/2 - 1
100 sg2_x <- sg2_x + mean(x[t0:t1]^2)
101 sg2_y <- sg2_y + mean(y[t0:t1]^2)
102 sg_xy <- sg_xy + mean(x[t0:t1] * y[t0:t1])
105 res <- sg_xy^2 / sg2_x / sg2_y
110 vect2mat <- function(vect){
111 vect <- as.vector(vect)
112 matrix(vect[-(1:2)], delta, lscvect)
116 # ========================================= # myimg for graphics
117 jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF",
118 "cyan", "#7FFF7F", "yellow",
119 "#FF7F00", "red", "#7F0000"))
121 myimg <- function(MAT, x = 1:nrow(MAT), y = 1:col(MAT), ... )
122 filled.contour( x = x, y = y, z = MAT,
123 xlab= 'Time', ylab= 'scale',
124 color.palette = jet.colors,