Commit | Line | Data |
---|---|---|
1c6f223e BA |
1 | library("Rwave") |
2 | ||
d7d55bc1 BA |
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) | |
d03c0621 | 6 | { |
d03c0621 BA |
7 | if(missing(scalevector)) |
8 | scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0 | |
d7d55bc1 BA |
9 | s0log=as.integer((log2( s0*w0/(2*pi) )-1)*nvoice+1.5) |
10 | totnoct=noctave+as.integer(s0log/nvoice)+1 | |
d03c0621 | 11 | res <- lapply(1:nrow(X), function(n) { |
d7d55bc1 BA |
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 | |
d03c0621 BA |
19 | }) |
20 | if( spectra ) | |
21 | res <- lapply(res, function(l) Mod(l)^2 ) | |
22 | if( smooth ) | |
d7d55bc1 | 23 | res <- lapply(res, smCWT, swabs = swabs, tw = tw, scalevector = scalevector) |
d03c0621 BA |
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 | |
1c6f223e BA |
28 | } |
29 | ||
d7d55bc1 BA |
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 ) | |
d03c0621 | 33 | { |
d03c0621 BA |
34 | wsp <- Mod(CWT) |
35 | smwsp <- smooth.matrix(wsp, swabs) | |
d7d55bc1 | 36 | smsmwsp <- smooth.time(smwsp, tw, scalevector) |
d03c0621 BA |
37 | smsmwsp |
38 | } | |
1c6f223e | 39 | |
d03c0621 BA |
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 | } | |
1c6f223e | 52 | |
d7d55bc1 | 53 | smooth.time <- function(wt,tw,scalevector) |
d03c0621 BA |
54 | { |
55 | smwt <- wt | |
56 | if (tw != 0) | |
57 | { | |
58 | for (i in 1:length(scalevector)) | |
59 | { | |
d7d55bc1 | 60 | twi <- as.integer(scalevector[i]*tw) |
d03c0621 BA |
61 | smwt[,i] <- filter(wt[,i],rep(1,2*twi+1)/(2*twi+1)) |
62 | } | |
1c6f223e | 63 | } |
d03c0621 BA |
64 | smwt |
65 | } | |
66 | ||
d7d55bc1 | 67 | #Entrée : courbes synchrones, soit après étape 1 itérée, soit après chaqure étape 1 |
d03c0621 BA |
68 | step2 = function(conso) |
69 | { | |
d03c0621 | 70 | n <- nrow(conso) |
d7d55bc1 | 71 | m <- ncol(conso) |
d03c0621 BA |
72 | |
73 | #TODO: automatic tune of these parameters ? (for other users) | |
74 | nvoice <- 4 | |
d7d55bc1 BA |
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 | |
d03c0621 BA |
79 | lscvect4 <- length(scalevector4) |
80 | lscvect <- lscvect4 # i should clean my code: werFam demands a lscvect | |
81 | ||
82 | # observations node with CWT | |
d7d55bc1 | 83 | Xcwt4 <- toCWT(conso, noctave = noctave, scalevector = scalevector4, |
d03c0621 | 84 | smooth = FALSE, nvoice = nvoice) |
3ccd1e39 | 85 | |
d03c0621 | 86 | #matrix: |
d03c0621 BA |
87 | Xcwt2 <- matrix(NA_complex_, nrow= n, ncol= 2 + length((c(Xcwt4[,,1])))) |
88 | ||
d03c0621 | 89 | for(i in 1:n) |
d7d55bc1 BA |
90 | Xcwt2[i,] <- c(m, lscvect, Xcwt4[,,i] / max(Mod(Xcwt4[,,i])) ) |
91 | ||
92 | rm(conso, Xcwt4) ; gc() | |
d03c0621 | 93 | |
3ccd1e39 BA |
94 | lscvect = dim(Xcwt4)[2] |
95 | ||
d03c0621 BA |
96 | Xwer_dist <- matrix(0.0, n, n) |
97 | for(i in 1:(n - 1)) | |
1c6f223e | 98 | { |
d7d55bc1 | 99 | mat1 <- matrix(as.vector(Xcwt2[i,])[-(1:2)], m, lscvect) |
c6556868 BA |
100 | |
101 | for(j in (i + 1):n) | |
d03c0621 | 102 | { |
d7d55bc1 | 103 | mat2 <- matrix(as.vector(Xcwt2[j,])[-(1:2)], m, lscvect) |
d03c0621 BA |
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) / | |
d7d55bc1 BA |
111 | sum( sum(colSums(smsmWX) * colSums(smsmWY)) ) |
112 | Xwer_dist[i, j] <- sqrt(m * lscvect * (1 - wer2)) | |
d03c0621 BA |
113 | Xwer_dist[j, i] <- Xwer_dist[i, j] |
114 | } | |
1c6f223e | 115 | } |
d03c0621 | 116 | diag(Xwer_dist) <- numeric(n) |
c6556868 | 117 | Xwer_dist |
1c6f223e | 118 | } |