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 toCWT <- function(X, sw= 0, tw= 0, swabs= 0,
22 nvoice= 12, noctave= 5,
23 s0= 2, w0= 2*pi, lt= 24, dt= 0.5,
24 spectra = FALSE, smooth = TRUE,
27 { noctave <- adjust.noctave(lt, dt, s0, tw, noctave)
28 if(missing(scalevector))
29 scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0
30 res <- lapply(1:nrow(X), function(n)
32 tsCent <- tsX - mean(tsX)
33 if(scaled) tsCent <- ts(scale(tsCent))
34 tsCent.cwt <- cwt.ts(tsCent, s0, noctave, nvoice, w0)
37 if( spectra ) res <- lapply(res, function(l) Mod(l)^2 )
38 if( smooth ) res <- lapply(res, smCWT, swabs = swabs,
40 scalevector = scalevector)
41 resArray <- array(NA, c(nrow(res[[1]]), ncol(res[[1]]),
43 for( l in 1:length(res) ) resArray[ , , l] <- res[[l]]
48 # ===============================================================
50 smCWT <- function(CWT, sw= 0, tw= 0, swabs= 0,
51 nvoice= 12, noctave= 2, s0= 2, w0= 2*pi,
52 lt= 24, dt= 0.5, scalevector )
54 # noctave <- adjust.noctave(lt, dt, s0, tw, noctave)
55 # scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0
57 smwsp <- smooth.matrix(wsp, swabs)
58 smsmwsp <- smooth.time(smwsp, tw, dt, scalevector)
63 # ===============================================================
65 toDWT <- function(x, filter.number = 6, family = "DaubLeAsymm")
66 { x2 <- spline(x, n = 2^ceiling( log(length(x), 2) ),
68 Dx2 <- wd(x2, family = family, filter.number = filter.number)$D
72 # ===============================================================
74 contrib <- function(x)
75 { J <- log( length(x)+1, 2)
81 nrj[j] <- sqrt( sum( x[t0:t1]^2 ) )
88 # ========================================= distance for coh ===
90 coherence <- function( x, y)
91 { J <- log(length(x) + 1, 2)
97 { t1 <- t0 + 2^(J - j)/2 - 1
99 sg2_x <- sg2_x + mean(x[t0:t1]^2)
100 sg2_y <- sg2_y + mean(y[t0:t1]^2)
101 sg_xy <- sg_xy + mean(x[t0:t1] * y[t0:t1])
104 res <- sg_xy^2 / sg2_x / sg2_y
109 vect2mat <- function(vect){
110 vect <- as.vector(vect)
111 matrix(vect[-(1:2)], delta, lscvect)
115 # ========================================= # myimg for graphics
116 jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF",
117 "cyan", "#7FFF7F", "yellow",
118 "#FF7F00", "red", "#7F0000"))
120 myimg <- function(MAT, x = 1:nrow(MAT), y = 1:col(MAT), ... )
121 filled.contour( x = x, y = y, z = MAT,
122 xlab= 'Time', ylab= 'scale',
123 color.palette = jet.colors,