Commit | Line | Data |
---|---|---|
ad642dc6 BA |
1 | #################################################################### |
2 | ## | |
3 | ## File: aux.r | |
4 | ## | |
5 | ## Description: Miscelaneous functions for clustering with kcca | |
6 | ## | |
7 | ## Modified: june 2010 | |
8 | ## | |
9 | #################################################################### | |
10 | ||
11 | ||
12 | ####################################################### | |
13 | ||
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 | |
17 | ||
18 | ######################################################## | |
19 | ||
20 | ||
572d139a | 21 | ##NOTE: renvoie une matrice 3D |
ad642dc6 BA |
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, | |
26 | scaled = FALSE, | |
27 | scalevector) | |
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) | |
32 | { tsX <- ts( X[n,] ) | |
33 | tsCent <- tsX - mean(tsX) | |
34 | if(scaled) tsCent <- ts(scale(tsCent)) | |
35 | tsCent.cwt <- cwt.ts(tsCent, s0, noctave, nvoice, w0) | |
36 | tsCent.cwt | |
37 | } ) | |
38 | if( spectra ) res <- lapply(res, function(l) Mod(l)^2 ) | |
39 | if( smooth ) res <- lapply(res, smCWT, swabs = swabs, | |
40 | tw = tw, dt = dt, | |
41 | scalevector = scalevector) | |
42 | resArray <- array(NA, c(nrow(res[[1]]), ncol(res[[1]]), | |
43 | length(res))) | |
44 | for( l in 1:length(res) ) resArray[ , , l] <- res[[l]] | |
45 | resArray | |
46 | } | |
47 | ||
48 | ||
49 | # =============================================================== | |
50 | ||
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 ) | |
54 | { | |
55 | # noctave <- adjust.noctave(lt, dt, s0, tw, noctave) | |
56 | # scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0 | |
57 | wsp <- Mod(CWT) | |
58 | smwsp <- smooth.matrix(wsp, swabs) | |
59 | smsmwsp <- smooth.time(smwsp, tw, dt, scalevector) | |
60 | smsmwsp | |
61 | } | |
62 | ||
63 | ||
64 | # =============================================================== | |
65 | ||
66 | toDWT <- function(x, filter.number = 6, family = "DaubLeAsymm") | |
67 | { x2 <- spline(x, n = 2^ceiling( log(length(x), 2) ), | |
68 | method = 'natural')$y | |
69 | Dx2 <- wd(x2, family = family, filter.number = filter.number)$D | |
70 | Dx2 | |
71 | } | |
72 | ||
73 | # =============================================================== | |
74 | ||
75 | contrib <- function(x) | |
76 | { J <- log( length(x)+1, 2) | |
77 | nrj <- numeric(J) | |
78 | t0 <- 1 | |
79 | t1 <- 0 | |
80 | for( j in 1:J ) { | |
81 | t1 <- t1 + 2^(J-j) | |
82 | nrj[j] <- sqrt( sum( x[t0:t1]^2 ) ) | |
83 | t0 <- t1 + 1 | |
84 | } | |
85 | return(nrj) | |
86 | } | |
87 | ||
88 | ||
89 | # ========================================= distance for coh === | |
90 | ||
91 | coherence <- function( x, y) | |
92 | { J <- log(length(x) + 1, 2) | |
93 | t0 <- 1 | |
94 | sg2_x <- 0 | |
95 | sg2_y <- 0 | |
96 | sg_xy <- 0 | |
97 | for(j in 0:(J - 1)) | |
98 | { t1 <- t0 + 2^(J - j)/2 - 1 | |
99 | tt <- t0:t1 | |
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]) | |
103 | t0 <- t1 + 1 | |
104 | } | |
105 | res <- sg_xy^2 / sg2_x / sg2_y | |
106 | res | |
107 | } | |
108 | ||
109 | ||
110 | vect2mat <- function(vect){ | |
111 | vect <- as.vector(vect) | |
112 | matrix(vect[-(1:2)], delta, lscvect) | |
113 | } | |
114 | ||
115 | ||
116 | # ========================================= # myimg for graphics | |
117 | jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", | |
118 | "cyan", "#7FFF7F", "yellow", | |
119 | "#FF7F00", "red", "#7F0000")) | |
120 | ||
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, | |
125 | ... ) | |
126 | ||
127 |