fa553560356f8566b50f9ab536911ff5610dea2b
[epclust.git] / epclust / R / stage2.R
1 library("Rwave")
2
3 #precondition: ( log2(s0*w0/(2*pi)) - 1 ) * nvoice + 1.5 >= 1
4 toCWT <- function(X, tw=0, swabs=0, nvoice=12, noctave=5, s0=2, w0=2*pi,
5 spectra=FALSE, smooth=TRUE, scaled=FALSE, scalevector)
6 {
7 if(missing(scalevector))
8 scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0
9 s0log=as.integer((log2( s0*w0/(2*pi) )-1)*nvoice+1.5)
10 totnoct=noctave+as.integer(s0log/nvoice)+1
11 res <- lapply(1:nrow(X), function(n) {
12 ts <- scale(ts( X[n,] ), center=TRUE, scale=scaled)
13 totts.cwt = Rwave::cwt(ts,totnoct,nvoice,w0,plot=0)
14 ts.cwt=totts.cwt[,s0log:(s0log+noctave*nvoice)]
15 #Normalization
16 sqs <- sqrt(2^(0:(noctave*nvoice)/nvoice)*s0)
17 smat <- matrix(rep(sqs,length(t)),nrow=length(t),byrow=TRUE)
18 ts.cwt*smat
19 })
20 if( spectra )
21 res <- lapply(res, function(l) Mod(l)^2 )
22 if( smooth )
23 res <- lapply(res, smCWT, swabs = swabs, tw = tw, scalevector = scalevector)
24 resArray <- array(NA, c(nrow(res[[1]]), ncol(res[[1]]), length(res)))
25 for( l in 1:length(res) )
26 resArray[ , , l] <- res[[l]]
27 resArray
28 }
29
30 #smooth cwt result
31 smCWT <- function(CWT, tw= 0, swabs= 0, nvoice= 12, noctave= 2, s0= 2, w0= 2*pi,
32 lt= 24, scalevector )
33 {
34 wsp <- Mod(CWT)
35 smwsp <- smooth.matrix(wsp, swabs)
36 smsmwsp <- smooth.time(smwsp, tw, scalevector)
37 smsmwsp
38 }
39
40 #dans sowas.R (...donc on ne lisse pas à ce niveau ?)
41 smooth.matrix <- function(wt,swabs)
42 {
43 if (swabs != 0)
44 {
45 smwt <- t(filter(t(wt),rep(1,2*swabs+1)/(2*swabs+1)))
46 } else
47 {
48 smwt <- wt
49 }
50 smwt
51 }
52
53 smooth.time <- function(wt,tw,scalevector)
54 {
55 smwt <- wt
56 if (tw != 0)
57 {
58 for (i in 1:length(scalevector))
59 {
60 twi <- as.integer(scalevector[i]*tw)
61 smwt[,i] <- filter(wt[,i],rep(1,2*twi+1)/(2*twi+1))
62 }
63 }
64 smwt
65 }
66
67 #Entrée : courbes synchrones, soit après étape 1 itérée, soit après chaqure étape 1
68 step2 = function(conso)
69 {
70 n <- nrow(conso)
71 m <- ncol(conso)
72
73 #TODO: automatic tune of these parameters ? (for other users)
74 nvoice <- 4
75 # noctave = 2^13 = 8192 half hours ~ 180 days ; ~log2(ncol(conso))
76 noctave = 13
77 # 4 here represent 2^5 = 32 half-hours ~ 1 day
78 scalevector4 <- 2^(4:(noctave * nvoice) / nvoice) * 2
79 lscvect4 <- length(scalevector4)
80 lscvect <- lscvect4 # i should clean my code: werFam demands a lscvect
81
82 # observations node with CWT
83 Xcwt4 <- toCWT(conso, noctave = noctave, scalevector = scalevector4,
84 smooth = FALSE, nvoice = nvoice)
85
86 #matrix:
87 Xcwt2 <- matrix(NA_complex_, nrow= n, ncol= 2 + length((c(Xcwt4[,,1]))))
88
89 for(i in 1:n)
90 Xcwt2[i,] <- c(m, lscvect, Xcwt4[,,i] / max(Mod(Xcwt4[,,i])) )
91
92 rm(conso, Xcwt4) ; gc()
93
94 lscvect = dim(Xcwt4)[2]
95
96 Xwer_dist <- matrix(0.0, n, n)
97 for(i in 1:(n - 1))
98 {
99 mat1 <- matrix(as.vector(Xcwt2[i,])[-(1:2)], m, lscvect)
100
101 for(j in (i + 1):n)
102 {
103 mat2 <- matrix(as.vector(Xcwt2[j,])[-(1:2)], m, lscvect)
104 num <- Mod(mat1 * Conj(mat2))
105 WX <- Mod(mat1 * Conj(mat1))
106 WY <- Mod(mat2 * Conj(mat2))
107 smsmnum <- smCWT(num, scalevector = scalevector4)
108 smsmWX <- smCWT(WX, scalevector = scalevector4)
109 smsmWY <- smCWT(WY, scalevector = scalevector4)
110 wer2 <- sum(colSums(smsmnum)^2) /
111 sum( sum(colSums(smsmWX) * colSums(smsmWY)) )
112 Xwer_dist[i, j] <- sqrt(m * lscvect * (1 - wer2))
113 Xwer_dist[j, i] <- Xwer_dist[i, j]
114 }
115 }
116 diag(Xwer_dist) <- numeric(n)
117 Xwer_dist
118 }