| 1 | #point avec Jairo: |
| 2 | #rentrer dans code C cwt continue Rwave |
| 3 | #passer partie sowas à C |
| 4 | #fct qui pour deux series (ID, medoides) renvoie distance WER (Rwave ou à moi) |
| 5 | #transformee croisee , smoothing lissage 3 composantes , + calcul pour WER |
| 6 | #attention : code fait pour des series temps desynchronisees ! (deltat, dt == 1,2 ...) |
| 7 | #determiner nvoice noctave (entre octave + petit et + grand) |
| 8 | |
| 9 | library("Rwave") |
| 10 | |
| 11 | #Entrée : courbes synchrones, soit après étape 1 itérée, soit après chaqure étape 1 |
| 12 | #TODO: bout de code qui calcule les courbes synchrones après étapes 1+2 à partir des ID médoïdes |
| 13 | |
| 14 | #toCWT: (aux) |
| 15 | ##NOTE: renvoie une matrice 3D |
| 16 | toCWT <- function(X, sw= 0, tw= 0, swabs= 0, nvoice= 12, noctave= 5, s0= 2, w0= 2*pi, |
| 17 | lt= 24, dt= 0.5, spectra = FALSE, smooth = TRUE, scaled = FALSE, scalevector) |
| 18 | { |
| 19 | noctave <- adjust.noctave(lt, dt, s0, tw, noctave) |
| 20 | if(missing(scalevector)) |
| 21 | scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0 |
| 22 | res <- lapply(1:nrow(X), function(n) { |
| 23 | tsX <- ts( X[n,] ) |
| 24 | tsCent <- tsX - mean(tsX) |
| 25 | if(scaled) |
| 26 | tsCent <- ts(scale(tsCent)) |
| 27 | tsCent.cwt <- cwt.ts(tsCent, s0, noctave, nvoice, w0) |
| 28 | tsCent.cwt |
| 29 | }) |
| 30 | if( spectra ) |
| 31 | res <- lapply(res, function(l) Mod(l)^2 ) |
| 32 | if( smooth ) |
| 33 | res <- lapply(res, smCWT, swabs = swabs, tw = tw, dt = dt, scalevector = scalevector) |
| 34 | resArray <- array(NA, c(nrow(res[[1]]), ncol(res[[1]]), length(res))) |
| 35 | for( l in 1:length(res) ) |
| 36 | resArray[ , , l] <- res[[l]] |
| 37 | resArray |
| 38 | } |
| 39 | |
| 40 | #from sowas |
| 41 | cwt.ts <- function(ts,s0,noctave=5,nvoice=10,w0=2*pi) |
| 42 | { |
| 43 | if (class(ts)!="ts") |
| 44 | stop("# This function needs a time series object as input. You may construct this by using the function ts(data,start,deltat). Try '?ts' for help.\n") |
| 45 | |
| 46 | t=time(ts) |
| 47 | dt=t[2]-t[1] |
| 48 | s0unit=s0/dt*w0/(2*pi) |
| 49 | s0log=as.integer((log2(s0unit)-1)*nvoice+1.5) |
| 50 | if (s0log<1) |
| 51 | { |
| 52 | cat(paste("# s0unit = ",s0unit,"\n",sep="")) |
| 53 | cat(paste("# s0log = ",s0log,"\n",sep="")) |
| 54 | cat("# s0 too small for w0! \n") |
| 55 | } |
| 56 | totnoct=noctave+as.integer(s0log/nvoice)+1 |
| 57 | |
| 58 | #cwt from package Rwave |
| 59 | totts.cwt=cwt(ts,totnoct,nvoice,w0,plot=0) |
| 60 | ts.cwt=totts.cwt[,s0log:(s0log+noctave*nvoice)] |
| 61 | |
| 62 | #Normalization |
| 63 | sqs <- sqrt(2^(0:(noctave*nvoice)/nvoice)*s0) |
| 64 | smat <- matrix(rep(sqs,length(t)),nrow=length(t),byrow=TRUE) |
| 65 | |
| 66 | ts.cwt*smat |
| 67 | } |
| 68 | |
| 69 | #NOTE: vect2mat = as.matrix ?! (dans aux.R) |
| 70 | vect2mat <- function(vect) |
| 71 | { |
| 72 | vect <- as.vector(vect) |
| 73 | matrix(vect[-(1:2)], delta, lscvect) |
| 74 | } |
| 75 | |
| 76 | #fonction smCWT (dans aux.R) |
| 77 | smCWT <- function(CWT, sw= 0, tw= 0, swabs= 0, nvoice= 12, noctave= 2, s0= 2, w0= 2*pi, |
| 78 | lt= 24, dt= 0.5, scalevector ) |
| 79 | { |
| 80 | #noctave <- adjust.noctave(lt, dt, s0, tw, noctave) |
| 81 | #scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0 |
| 82 | wsp <- Mod(CWT) |
| 83 | smwsp <- smooth.matrix(wsp, swabs) |
| 84 | smsmwsp <- smooth.time(smwsp, tw, dt, scalevector) |
| 85 | smsmwsp |
| 86 | } |
| 87 | |
| 88 | #dans sowas.R (...donc on ne lisse pas à ce niveau ?) |
| 89 | smooth.matrix <- function(wt,swabs) |
| 90 | { |
| 91 | if (swabs != 0) |
| 92 | { |
| 93 | smwt <- t(filter(t(wt),rep(1,2*swabs+1)/(2*swabs+1))) |
| 94 | } else |
| 95 | { |
| 96 | smwt <- wt |
| 97 | } |
| 98 | smwt |
| 99 | } |
| 100 | |
| 101 | smooth.time <- function(wt,tw,dt,scalevector) |
| 102 | { |
| 103 | smwt <- wt |
| 104 | if (tw != 0) |
| 105 | { |
| 106 | for (i in 1:length(scalevector)) |
| 107 | { |
| 108 | twi <- as.integer(scalevector[i]*tw/dt) |
| 109 | smwt[,i] <- filter(wt[,i],rep(1,2*twi+1)/(2*twi+1)) |
| 110 | } |
| 111 | } |
| 112 | smwt |
| 113 | } |
| 114 | |
| 115 | step2 = function(conso) |
| 116 | { |
| 117 | #(Benjamin) |
| 118 | #à partir de là, "conso" == courbes synchrones |
| 119 | n <- nrow(conso) |
| 120 | delta <- ncol(conso) |
| 121 | |
| 122 | #17000 colonnes coeff 1, puis 17000 coeff 2... [non : dans chaque tranche du cube] |
| 123 | # #NOTE: delta et lscvect pourraient etre gardés à part (communs) |
| 124 | |
| 125 | #TODO: automatic tune of these parameters ? (for other users) |
| 126 | nvoice <- 4 |
| 127 | # # noctave4 = 2^13 = 8192 half hours ~ 180 days |
| 128 | noctave4 <- adjust.noctave(N = delta, dt = 1, s0 = 2, tw = 0, noctave = 13) |
| 129 | # # 4 here represent 2^5 = 32 half-hours ~ 1 day |
| 130 | scalevector4 <- 2^(4:(noctave4 * nvoice) / nvoice) * 2 |
| 131 | lscvect4 <- length(scalevector4) |
| 132 | lscvect <- lscvect4 # i should clean my code: werFam demands a lscvect |
| 133 | |
| 134 | # observations node with CWT |
| 135 | Xcwt4 <- toCWT(conso, noctave = noctave4, dt = 1, scalevector = scalevector4, lt = delta, |
| 136 | smooth = FALSE, nvoice = nvoice) |
| 137 | |
| 138 | #matrix: |
| 139 | ############Xcwt2 <- matrix(0.0, nrow= n, ncol= 2 + delta * lscvect) |
| 140 | Xcwt2 <- matrix(NA_complex_, nrow= n, ncol= 2 + length((c(Xcwt4[,,1])))) |
| 141 | |
| 142 | #NOTE: delta et lscvect pourraient etre gardés à part (communs) |
| 143 | for(i in 1:n) |
| 144 | Xcwt2[i,] <- c(delta, lscvect, Xcwt4[,,i] / max(Mod(Xcwt4[,,i])) ) |
| 145 | #rm(conso, Xcwt4); gc() |
| 146 | |
| 147 | ## _.b WER^2 distances ######## |
| 148 | Xwer_dist <- matrix(0.0, n, n) |
| 149 | for(i in 1:(n - 1)) |
| 150 | { |
| 151 | mat1 <- vect2mat(Xcwt2[i,]) |
| 152 | |
| 153 | for(j in (i + 1):n) |
| 154 | { |
| 155 | mat2 <- vect2mat(Xcwt2[j,]) |
| 156 | num <- Mod(mat1 * Conj(mat2)) |
| 157 | WX <- Mod(mat1 * Conj(mat1)) |
| 158 | WY <- Mod(mat2 * Conj(mat2)) |
| 159 | smsmnum <- smCWT(num, scalevector = scalevector4) |
| 160 | smsmWX <- smCWT(WX, scalevector = scalevector4) |
| 161 | smsmWY <- smCWT(WY, scalevector = scalevector4) |
| 162 | wer2 <- sum(colSums(smsmnum)^2) / |
| 163 | sum( sum(colSums(smsmWX) * colSums(smsmWY)) ) |
| 164 | Xwer_dist[i, j] <- sqrt(delta * lscvect * (1 - wer2)) |
| 165 | Xwer_dist[j, i] <- Xwer_dist[i, j] |
| 166 | } |
| 167 | } |
| 168 | diag(Xwer_dist) <- numeric(n) |
| 169 | Wwer_dist |
| 170 | } |