1 #Ligne 83 dans epclust/R/computeWerDists.R :
3 # [X,period,scale,coix] =
4 #wavelet(x(:,2),dt,Args.Pad,Args.Dj,Args.S0,Args.J1,Args.Mother);%#ok
5 #[Y,period,scale,coiy] = wavelet(y(:,2),dt,Args.Pad,Args.Dj,Args.S0,Args.J1,Args.Mother);
7 #%Smooth X and Y before truncating! (minimize coi)
11 #%sX=smoothwavelet(sinv(:,ones(1,nx)).*(abs(X).^2),dt,period,Args.Dj,scale);
12 #%sY=smoothwavelet(sinv(:,ones(1,ny)).*(abs(Y).^2),dt,period,Args.Dj,scale);
16 #%% ----------------------- Wavelet coherence ---------------------------------
17 #%sWxy=smoothwavelet(sinv(:,ones(1,n)).*Wxy,dt,period,Args.Dj,scale);
18 #%Rsq=abs(sWxy).^2./(sX.*sY);
22 #https://github.com/grinsted/wavelet-coherence/blob/master/wtc.m
26 library(cluster) # pam
27 #library(flexclust) # kcca
30 toCWT <- function(X, sw= 0, tw= 0, swabs= 0,
31 nvoice= 12, noctave= 5,
32 s0= 2, w0= 2*pi, lt= 24, dt= 0.5,
33 spectra = FALSE, smooth = TRUE,
36 { noctave <- adjust.noctave(lt, dt, s0, tw, noctave)
37 if(missing(scalevector))
38 scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0
39 res <- lapply(1:nrow(X), function(n)
41 tsCent <- tsX - mean(tsX)
42 if(scaled) tsCent <- ts(scale(tsCent))
43 tsCent.cwt <- cwt.ts(tsCent, s0, noctave, nvoice, w0)
46 if( spectra ) res <- lapply(res, function(l) Mod(l)^2 )
47 if( smooth ) res <- lapply(res, smCWT, swabs = swabs,
49 scalevector = scalevector)
50 resArray <- array(NA, c(nrow(res[[1]]), ncol(res[[1]]),
52 for( l in 1:length(res) ) resArray[ , , l] <- res[[l]]
57 # ===============================================================
59 smCWT <- function(CWT, sw= 0, tw= 0, swabs= 0,
60 nvoice= 12, noctave= 2, s0= 2, w0= 2*pi,
61 lt= 24, dt= 0.5, scalevector )
63 # noctave <- adjust.noctave(lt, dt, s0, tw, noctave)
64 # scalevector <- 2^(0:(noctave * nvoice) / nvoice) * s0
66 smwsp <- smooth.matrix(wsp, swabs)
67 smsmwsp <- smooth.time(smwsp, tw, dt, scalevector)
72 # ===============================================================
74 toDWT <- function(x, filter.number = 6, family = "DaubLeAsymm")
75 { x2 <- spline(x, n = 2^ceiling( log(length(x), 2) ),
77 Dx2 <- wd(x2, family = family, filter.number = filter.number)$D
81 # ===============================================================
83 contrib <- function(x)
84 { J <- log( length(x)+1, 2)
90 nrj[j] <- sqrt( sum( x[t0:t1]^2 ) )
97 # ========================================= distance for coh ===
99 coherence <- function( x, y)
100 { J <- log(length(x) + 1, 2)
106 { t1 <- t0 + 2^(J - j)/2 - 1
108 sg2_x <- sg2_x + mean(x[t0:t1]^2)
109 sg2_y <- sg2_y + mean(y[t0:t1]^2)
110 sg_xy <- sg_xy + mean(x[t0:t1] * y[t0:t1])
113 res <- sg_xy^2 / sg2_x / sg2_y
118 vect2mat <- function(vect){
119 vect <- as.vector(vect)
120 matrix(vect[-(1:2)], delta, lscvect)
124 # ========================================= # myimg for graphics
125 jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF",
126 "cyan", "#7FFF7F", "yellow",
127 "#FF7F00", "red", "#7F0000"))
129 myimg <- function(MAT, x = 1:nrow(MAT), y = 1:col(MAT), ... )
130 filled.contour( x = x, y = y, z = MAT,
131 xlab= 'Time', ylab= 'scale',
132 color.palette = jet.colors,
136 #TODO: [plus tard] alternative à sowa (package disparu) : cwt..
137 #source("sowas-superseded.r") # auxiliary CWT functions
140 cwt.ts <- function(ts,s0,noctave=5,nvoice=10,w0=2*pi){
142 if (class(ts)!="ts"){
144 cat("# 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")
152 s0unit=s0/dt*w0/(2*pi)
153 s0log=as.integer((log2(s0unit)-1)*nvoice+1.5)
156 cat(paste("# s0unit = ",s0unit,"\n",sep=""))
157 cat(paste("# s0log = ",s0log,"\n",sep=""))
158 cat("# s0 too small for w0! \n")
160 totnoct=noctave+as.integer(s0log/nvoice)+1
162 totts.cwt=cwt(ts,totnoct,nvoice,w0,plot=0)
164 ts.cwt=totts.cwt[,s0log:(s0log+noctave*nvoice)]
167 sqs <- sqrt(2^(0:(noctave*nvoice)/nvoice)*s0)
168 smat <- matrix(rep(sqs,length(t)),nrow=length(t),byrow=TRUE)
176 #####################################
178 #####################################
180 wsp <- function(ts,s0=1,noctave=5,nvoice=10,w0=2*pi,sw=0,tw=0,swabs=0,siglevel=0.95,critval=NULL,nreal=1000,arealsiglevel=0.9,kernel=0,markt=-999,marks=-999,logscale=FALSE,plot=TRUE,units="",device="screen",file="wsp",color=TRUE,pwidth=10,pheight=7,labsc=1,labtext="",sigplot=3){
182 if (class(ts)!="ts"){
184 cat("# 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")
189 if (sw!=0 & swabs==0)
190 swabs <- as.integer(sw*nvoice)
191 if (swabs!=0 & sw==0)
194 sllist <- checkarealsiglevel(sw,tw,w0,arealsiglevel,siglevel,0)
195 arealsiglevel <- sllist$arealsiglevel
196 siglevel <- sllist$siglevel
203 s0 <- adjust.s0(s0,dt)
204 dnoctave <- as.integer(log(s0/s0rem-0.000000000001)/log(2))+1
206 noctave <- adjust.noctave(length(ts),dt,s0,tw,noctave)
207 scalevector <- 2^(0:(noctave*nvoice)/nvoice)*s0
208 tsanom <- ts-mean(ts)
210 #WAVELET TRANSFORMATION
211 ts.cwt <- cwt.ts(tsanom,s0,noctave,nvoice,w0)
214 wsp <- smooth.matrix(Mod(ts.cwt)^2,swabs)
215 smwsp <- smooth.time(wsp,tw,dt,scalevector)
217 #POINTWISE SIGNIFICANCE TEST
218 if (is.null(critval)==FALSE){ # is critval empty?
219 if (dim(critval)[2]!=dim(smwsp)[2]){ # is critval of the wrong dimension?
220 if (siglevel[1]!=0 & nreal!=0) critval <-
221 criticalvaluesWSP(tsanom,s0,noctave,nvoice,w0,swabs,tw,siglevel,nreal)
222 #critval is of wrong dimension and siglevel and nreal are given
224 critval <- NULL # no test possible
226 cat("# dimension of critval is wrong \n")
227 cat("# areawise test only possible with pointwise test \n")
231 else{ # critval is empty, but nreal or siglevel is given
232 if (siglevel[1]!=0 & nreal!=0) critval <-
233 criticalvaluesWSP(tsanom,s0,noctave,nvoice,w0,swabs,tw,siglevel,nreal)
237 cat("# areawise test only possible with pointwise test \n")
241 #AREAL SIGNIFICANCE TEST
242 if (arealsiglevel!=0){
245 cvmat <- matrix(rep(v,length(t)),nrow=length(t),byrow=TRUE)
246 atest <- arealtest(smwsp/cvmat,dt,s0,noctave,nvoice,w0,swabs,tw,siglevel,arealsiglevel,kernel,0)
248 kernel <- atest$kernel
252 smwsp <- addvalues(nvoice,dnoctave,smwsp,NA)
253 critval <- addvalues(nvoice,dnoctave,critval,1)
254 #at <- addvalues(nvoice,dnoctave,at,NA)
255 noctave <- noctave+dnoctave
257 scalevector <- 2^(0:(noctave*nvoice)/nvoice)*s0
262 list(modulus=smwsp,phase=NULL,time=t,s0=s0,noctave=noctave,nvoice=nvoice,w0=w0,scales=scalevector,critval=critval,at=at,kernel=kernel)
264 class(wclist) <- "wt"
268 plot(wclist,markt,marks,NULL,NULL,logscale,FALSE,units,"Wavelet Power Spectrum",device,file,FALSE,color,pwidth,pheight,labsc,labtext,sigplot)
276 #####################################
278 #####################################
281 function(ts1,ts2,s0=1,noctave=5,nvoice=10,w0=2*pi,sw=0,tw=0,swabs=0,siglevel=0.95,arealsiglevel=0.9,kernel=0,markt=-999,marks=-999,sqr=FALSE,phase=TRUE,plot=TRUE,units="",device="screen",file="wcoh",split=FALSE,color=TRUE,pwidth=10,pheight=7,labsc=1,labtext="",sigplot=3){
283 if (class(ts1)!="ts"){
285 cat("# This function needs two time series objects as input. You may construct them by using the function ts(data,start,deltat). Try '?ts' for help.\n")
290 if (sw!=0 & swabs==0)
291 swabs <- as.integer(sw*nvoice)
292 if (swabs!=0 & sw==0)
295 sllist <- checkarealsiglevel(sw,tw,w0,arealsiglevel,siglevel,1)
296 arealsiglevel <- sllist$arealsiglevel
297 siglevel <- sllist$siglevel
299 if (sw==0 & tw==0 & swabs==0) {
300 cat("# coherence without averaging makes no sense! \n")
305 if (phase==FALSE) phs <- NULL
309 tsadjust <- adjust.timeseries(ts1,ts2)
317 s0 <- adjust.s0(s0,dt)
318 dnoctave <- as.integer(log(s0/s0rem-0.000000000001)/log(2))+1
320 noctave <- adjust.noctave(length(ts1),dt,s0,tw,noctave)
322 scalevector <- 2^(0:(noctave*nvoice)/nvoice)*s0
324 ts1anom <- ts1-mean(ts1)
325 ts2anom <- ts2-mean(ts2)
327 ts1.cwt <- cwt.ts(ts1anom,s0,noctave,nvoice,w0)
328 ts2.cwt <- cwt.ts(ts2anom,s0,noctave,nvoice,w0)
330 cosp <- Re(ts1.cwt)*Re(ts2.cwt) + Im(ts1.cwt)*Im(ts2.cwt)
331 quad <- Im(ts1.cwt)*Re(ts2.cwt) - Re(ts1.cwt)*Im(ts2.cwt)
332 wsp1 <- Mod(ts1.cwt)^2
333 wsp2 <- Mod(ts2.cwt)^2
335 smcosp <- smooth.matrix(cosp,swabs)
336 smquad <- smooth.matrix(quad,swabs)
337 smwsp1 <- smooth.matrix(wsp1,swabs)
338 smwsp2 <- smooth.matrix(wsp2,swabs)
340 smsmcosp <- smooth.time(smcosp,tw,dt,scalevector)
341 smsmquad <- smooth.time(smquad,tw,dt,scalevector)
342 smsmwsp1 <- smooth.time(smwsp1,tw,dt,scalevector)
343 smsmwsp2 <- smooth.time(smwsp2,tw,dt,scalevector)
346 wcoh <- sqrt((smsmcosp^2+smsmquad^2)/(smsmwsp1*smsmwsp2))
348 wcoh <- (smsmcosp^2+smsmquad^2)/(smsmwsp1*smsmwsp2)
351 phs <- atan2(smsmquad,smsmcosp)
354 #POINTWISE SIGNIFICANCE TEST
355 if (siglevel[1]!=0) critval <- criticalvaluesWCO(s0,noctave,nvoice,w0,swabs,tw,siglevel)
357 if (sqr==TRUE & is.null(critval)==FALSE)
360 #AREAWISE SIGNIFICANCE TEST
361 if (arealsiglevel!=0){
362 atest <- arealtest(wcoh/critval,dt,s0,noctave,nvoice,w0,swabs,tw,siglevel,arealsiglevel,kernel,1)
364 kernel <- atest$kernel
376 wcoh <- addvalues(nvoice,dnoctave,wcoh,NA)
377 phs <- addvalues(nvoice,dnoctave,phs,NA)
378 noctave <- noctave+dnoctave
380 scalevector <- 2^(0:(noctave*nvoice)/nvoice)*s0
383 wclist <- list(modulus=wcoh,phase=phs,s0=s0,noctave=noctave,nvoice=nvoice,w0=w0,time=t,scales=scalevector,critval=critval,at=at,kernel=kernel)
385 class(wclist) <- "wt"
387 if (plot) plot(wclist,markt,marks,NULL,NULL,FALSE,phase,units,"Wavelet Coherence",device,file,split,color,pwidth,pheight,labsc,labtext,sigplot)
395 #####################################
397 #####################################
399 wcs <- function(ts1,ts2,s0=1,noctave=5,nvoice=10,w0=2*pi,sw=0,tw=0,swabs=0,markt=-999,marks=-999,logscale=FALSE,phase=TRUE,plot=TRUE,units="",device="screen",file="wcsp",split=FALSE,color=TRUE,pwidth=10,pheight=7,labsc=1,labtext=""){
401 if (class(ts1)!="ts"){
403 cat("# This function needs two time series objects as input. You may construct them by using the function ts(data,start,deltat). Try '?ts' for help. \n")
408 if (sw!=0 & swabs==0)
409 swabs <- as.integer(sw*nvoice)
410 if (swabs!=0 & sw==0)
413 tsadjust <- adjust.timeseries(ts1,ts2)
421 s0 <- adjust.s0(s0,dt)
422 dnoctave <- as.integer(log(s0/s0rem-0.000000000001)/log(2))+1
424 noctave <- adjust.noctave(length(ts1),dt,s0,tw,noctave)
426 scalevector <- 2^(0:(noctave*nvoice)/nvoice)*s0
428 ts1anom <- ts1-mean(ts1)
429 ts2anom <- ts2-mean(ts2)
431 ts1.cwt <- cwt.ts(ts1anom,s0,noctave,nvoice,w0)
432 ts2.cwt <- cwt.ts(ts2anom,s0,noctave,nvoice,w0)
434 cosp <- Re(ts1.cwt)*Re(ts2.cwt) + Im(ts1.cwt)*Im(ts2.cwt)
435 quad <- Im(ts1.cwt)*Re(ts2.cwt) - Re(ts1.cwt)*Im(ts2.cwt)
437 smcosp <- smooth.matrix(cosp,swabs)
438 smquad <- smooth.matrix(quad,swabs)
440 smsmcosp <- smooth.time(smcosp,tw,dt,scalevector)
441 smsmquad <- smooth.time(smquad,tw,dt,scalevector)
443 wcsp <- smsmcosp^2+smsmquad^2
446 phs <- atan2(smsmquad,smsmcosp)
455 wcsp <- addvalues(nvoice,dnoctave,wcoh,NA)
456 phs <- addvalues(nvoice,dnoctave,phs,NA)
457 noctave <- noctave+dnoctave
459 scalevector <- 2^(0:(noctave*nvoice)/nvoice)*s0
462 wclist <- list(modulus=wcsp,phase=phs,s0=s0,noctave=noctave,nvoice=nvoice,w0=w0,time=t,scales=scalevector,critval=NULL,at=NULL,kernel=NULL)
464 class(wclist) <- "wt"
466 if (plot) plot(wclist,markt,marks,NULL,NULL,logscale,phase,units,"Wavelet Cross Spectrum",device,file,split,color,pwidth,pheight,labsc,labtext)
474 ##########################################
475 # POINTWISE SIGNIFICANCE TEST
476 ##########################################
478 rawWSP <- function(ts,s0=1,noctave=5,nvoice=20,w0=2*pi,swabs=0,tw=0,scalevector){
480 tsanom <- ts-mean(ts)
482 ts.cwt <- cwt.ts(tsanom,s0,noctave,nvoice,w0)
486 smwsp <- smooth.matrix(wsp,swabs)
487 smsmwsp <- smooth.time(smwsp,tw,deltat(ts),scalevector)
493 criticalvaluesWSP <- function(ts,s0=1,noctave=5,nvoice=10,w0=2*pi,swabs=0,tw=0,siglevel=0.95,nreal=1000){
498 s0 <- adjust.s0(s0,dt)
499 noctave <- adjust.noctave(length(ts),dt,s0,tw,noctave)
501 scalevector <- 2^(0:(noctave*nvoice)/nvoice)*s0
503 cat("# calculating critical values by means of MC-simulations \n")
505 s0unit=s0/dt*w0/(2*pi)
506 s0log=as.integer((log2(s0unit)-1)*nvoice+1.5)
508 wsp0 <- rawWSP(ts,s0,noctave,nvoice,w0,swabs,tw,scalevector)
512 n1 <- 1 + 2*as.integer( sqrt(2) * 2^((S-swabs+s0log-1)/nvoice+1) )
513 n2 <- max(scalevector)*tw*2/dt*1.1
514 n3 <- 2*tw*s0*2^noctave/dt+100
518 cv <- matrix(rep(0,S*length(siglevel)),ncol=S)
519 rmatrix <- matrix(0,nrow=nreal,ncol=S)
521 # Fitting AR1-process (red noise) to data
522 arts0 <- ar(ts,order.max=1)
523 sdts0 <- sd(ts[1:length(ts)])
526 se <- sqrt(arts0$var)
530 se <- sqrt(sdts0*sdts0*(1-arts0$ar*arts0$ar))
532 tsMC <- ts(data=rep(0,n),start=t[1],frequency=1/dt)
537 tsMC[1:n] <- arima.sim(list(ar = arts0$ar), n+1, sd = se)[2:(n+1)]
539 rmatrix[i,] <- rawWSP(tsMC,s0,noctave,nvoice,w0,swabs,tw,scalevector)[center,]
543 for (s in (1+swabs):(S-swabs)) rmatrix[,s] <- sort(rmatrix[,s])
545 for (i in 1:length(siglevel)){
546 sigindex <- as.integer(nreal*siglevel[i])
547 cvv <- rmatrix[sigindex,]
558 criticalvaluesWCO <- function(s0,noctave,nvoice,w0,swabs,tw,siglevel=0.95){
560 cv=rep(0,length(siglevel))
562 for (i in 1:length(siglevel)){
564 if (siglevel[i]!=0.9 && siglevel[i]!=0.95 && siglevel[i]!=0.99) siglevel[i] <- 0.95
566 if (siglevel[i]==0.9){
567 cat("# significance level set to 0.90 \n")
568 sw <- 1.0*swabs/nvoice
569 cv[i] <- 0.00246872*w0^2*sw + 0.0302419*w0*sw^2 + 0.342056*sw^3 -
570 0.000425853*w0^2 - 0.101749*w0*sw - 0.703537*sw^2 +
571 0.00816219*w0 + 0.442342*sw + 0.970315
574 if (siglevel[i]==0.95){
575 cat("# significance level set to 0.95 \n")
576 sw <- swabs*100.0/3.0/nvoice
577 cv[i] <- 0.0000823*w0^3 + 0.0000424*w0^2*sw + 0.0000113*w0*sw^2 +
578 0.0000154*sw^3 - 0.0023*w0^2 - 0.00219*w0*sw - 0.000751*sw^2 +
579 0.0205*w0 + 0.0127*sw + 0.95
582 if (siglevel[i]==0.99){
583 cat("# significance level set to 0.99 \n")
584 sw <- 1.0*swabs/nvoice
585 cv[i] <- 0.00102304*w0^2*sw + 0.00745772*w0*sw^2 + 0.230035*sw^3 -
586 0.000361565*w0^2 - 0.0502861*w0*sw - 0.440777*sw^2 +
587 0.00694998*w0 + 0.29643*sw + 0.972964
590 if (cv[i]>1) cv[i] <- 1
592 cat(paste("# significance testing, cv=",cv[i]," \n",sep=""))
600 #############################
601 # AREAWISE SIGNIFICANCE TEST
602 #############################
604 slide <- function(data,kernellist,s0,noctave,nvoice,cv){
606 # slides kernel over data set
607 #----------------------------
608 # data: matrix containing data
609 # kernellist: matrix containing kernel
611 # noctave: number of octaves
612 # nvoice: number of voices per octave
613 # cv: critical value, all values higher are set to one
615 #Time: (rows) n,i the kernel is to be scaled in this direction
620 kernel <- kernellist$bitmap
627 kbin <- tobin(kernel)
635 mark <- matrix(rep(0,dn*dm),nrow=dn)
637 for (j in 1:(dm-km+1)){
639 s <- s0*2^((j+js-1)/nvoice)
640 kscn <- as.integer(kn*s/sm);
641 if (kscn==0) kscn <- 1
643 ksc <- scaleKernel(kbin,kscn)
646 for (i in 1:(dn-kscn+1)){
648 subbin <- dbin[i:(i+kscn-1),j:(j+kscm-1)]
650 if (sum(ksc*subbin)==sum(ksc))
651 mark[i:(i+kscn-1),j:(j+kscm-1)] <- mark[i:(i+kscn-1),j:(j+kscm-1)]+ksc
663 arealtest <- function(wt,dt=1,s0=1,noctave=5,nvoice=20,w0=2*pi,swabs=0,tw=0,siglevel,arealsiglevel=0.9,kernel=0,type=0){
665 slp <- slope(w0,swabs,tw,nvoice,siglevel,arealsiglevel,type)
667 if (length(kernel)<2){
668 maxarea <- s0*2^noctave*slp/10*nvoice/dt
669 cat(paste("# calculating kernel (maxarea=",maxarea,")\n",sep=""))
671 kernelRoot(s0,w0,a=maxarea,noctave,nvoice,swabs,tw,dt)
673 cat("# building kernel bitmap \n")
675 kernelBitmap(cvkernel,s0,w0,noctave,nvoice,swabs,tw,dt)
679 cat("# performing arealtest \n")
680 sl <- slide(wt,kernel,s0,noctave,nvoice,1)
682 list(at=sl,kernel=kernel)
686 #############################
688 #############################
690 plotat <- function(t,wt,at,sigplot){
697 contour(x=t,z=at,levels=0.5,add=TRUE,drawlabels=FALSE,axes=FALSE,lwd=linewidth,col="black")
703 plotcv <- function(t,wt,cv){
705 if (length(dim(cv))==0)
707 contour(x=t,z=wt,levels=c(cv),drawlabels=FALSE,axes=FALSE,add=TRUE,col="black",lwd=1)
711 for(i in 1:nrow(cv)){
715 m <- matrix(rep(v,length(t)),nrow=length(t),byrow=TRUE)
717 contour(x=t,z=wt/m,levels=1,drawlabels=FALSE,axes=FALSE,add=TRUE,col="black",lwd=1)
725 plotcoi <- function(t,s0,noctave,w0){
728 tvl <- tv[tv-tv[1]<(tv[length(tv)]-tv[1])/2]
729 tvr <- tv[tv-tv[1]>=(tv[length(tv)]-tv[1])/2]
731 lines(tvl,log2(((tvl-tv[1])*4*pi/((w0+sqrt(2+w0*w0))*sqrt(2)))/s0)/noctave,col="black")
732 lines(tvr,log2(((tv[length(tv)]-tvr)*4*pi/((w0+sqrt(2+w0*w0))*sqrt(2)))/s0)/noctave,col="black")
736 plotmarks <- function(t,s0,noctave,markt,marks){
740 for (i in 1:length(markt)){
741 lines(c(markt[i],markt[i]),c(0,1),lty="dotted")
748 for (i in 1:length(marks)){
749 lines(c(t[1],t[length(t)]),c(log2(marks[i]/s0)/noctave,log2(marks[i]/s0)/noctave),lty="dotted")
757 #####################
759 #####################
761 plot.wt <- function(wt,markt=-999,marks=-999,t1=NULL,t2=NULL,logscale=FALSE,phase=FALSE,units="",plottitle="",device="screen",file="wt",split=FALSE,color=TRUE,pwidth=10,pheight=5,labsc=1.5,labtext="",sigplot=3,xax=NULL,xlab=NULL,yax=NULL,ylab=NULL){
763 plotwt(wt$modulus,wt$phase,wt$time,wt$s0,wt$noctave,wt$w0,wt$critval,wt$at,markt,marks,t1,t2,logscale,phase,units,plottitle,device,file,split,color,pwidth,pheight,labsc,labtext,sigplot,xax,xlab,yax,ylab)
768 function(wt,phs,t,s0,noctave,w0,cv=NULL,at=NULL,markt=-999,marks=-999,t1=NULL,t2=NULL,logscale=FALSE,phase=FALSE,units="",plottitle="Wavelet Plot",device="screen",file="wavelet",split=FALSE,color=TRUE,pwidth=10,pheight=7,labsc=1,labtext="",sigplot=1,xax=NULL,xlab=NULL,yax=NULL,ylab=NULL){
770 if (is.null(phs)) phase <- FALSE
773 if (labsc>1) mgpv[1] <- 3-(labsc-1.5)
776 colors <- wtcolors(ncolors)
777 if (color==FALSE) colors <- gray((0:ncolors)/ncolors*0.7+0.3)
779 rangev <- (0:(ncolors-1))/(ncolors-1)
780 rangebar <- matrix(rangev,nrow=2,ncol=64,byrow=TRUE)
782 s <- 2^(0:(noctave))*s0
783 sn <- (0:(noctave))/noctave
785 deltat <- (t[length(t)]-t[1])/(length(t)-1)
787 if ((!is.null(t1)) | (!is.null(t2))){
788 if (t1<t2 & t1>=t[1] & t2<=t[length(t)]){
792 i1 <- (t1-t[1])/deltat+1
793 i2 <- (t2-t[1])/deltat+1
795 t <- t[t>=t1 & t<=t2]
798 if (phase) phs <- phs[i1:i2,]
799 if (length(at)>1) at <- at[i1:i2,]
808 if (device=="ps" && split==FALSE){
810 file <- paste(file,".eps",sep="")
812 postscript(file,onefile=TRUE,horizontal=TRUE,paper="special",width=pwidth,height=pheight)
813 cat(paste("# writing",file," \n"))
817 if (device=="ps" && split==TRUE){
819 file1 <- paste(file,".mod.eps",sep="")
821 postscript(file1,onefile=TRUE,horizontal=TRUE,paper="special",width=pwidth,height=pheight)
822 cat(paste("# writing",file1," \n"))
826 if (phase==TRUE && split==FALSE) nlo <- layout(matrix(c(1,2,3,4),2,2,byrow=TRUE),widths=c(4,1))
827 else nlo <- layout(matrix(c(1,2),ncol=2,byrow=TRUE),widths=c(4,1))
836 image(x=t,z=log10(wt),col = colors,axes=FALSE,xlab="Time",ylab="Scale",frame.plot=TRUE,cex.lab=labsc,mgp=mgpv)
838 image(x=t,z=log10(wt),col = colors,axes=FALSE,xlab=paste("Time ","[",units,"]",sep=""),ylab=paste("Scale ","[",units,"]",sep=""),frame.plot=TRUE,cex.lab=labsc,mgp=mgpv)
842 image(x=t,z=wt,col = colors,axes=FALSE,xlab="Time",ylab="Scale",frame.plot=TRUE,cex.lab=labsc,mgp=mgpv)
844 image(x=t,z=wt,col = colors,axes=FALSE,xlab=paste("Time ","[",units,"]",sep=""),ylab=paste("Scale ","[",units,"]",sep=""),frame.plot=TRUE,cex.lab=labsc,mgp=mgpv)
847 text(t[1+as.integer(length(t)*0.1)],0.8,labtext,cex=2)
849 if (sigplot==1 | sigplot==3){
850 if (is.null(cv)==FALSE){ #Critical values
851 if (cv[1]!=0 & cv[1]!=-1) plotcv(t,wt,cv)
855 if (sigplot>1) plotat(t,wt,at,sigplot)
857 if (!cut) plotcoi(t,s0,noctave,w0) #cone of influence
858 plotmarks(t,s0,noctave,markt,marks) #additional guiding lines
861 axis(side=1,at=axTicks(1),cex.axis=labsc)
864 axis(side=1,xax,labels=as.character(xax),cex.axis=labsc)
866 axis(side=1,xax,labels=xlab,cex.axis=labsc)
869 axis(side=2,sn,labels=as.character(s),cex.axis=labsc)
872 axis(side=2,yax,labels=as.character(yax),cex.axis=labsc)
874 axis(side=2,yax,labels=ylab,cex.axis=labsc)
876 title(main=plottitle,cex=labsc)
878 image(z=rangebar,axes=FALSE,col=colors,frame.plot=TRUE,cex.lab=labsc,mgp=mgpv)
880 if (is.null(cv)==FALSE){
881 if (length(dim(cv))==0){
882 for (i in 1:length(cv))
883 if (cv[i]!=0) lines(c(-1,2),c(cv[i],cv[i]))
888 axis(side=2,(0:5)/5,labels=c("0","","","","","1"),cex.axis=labsc)
890 labelv <- substr(as.character(c(0:5)*(max(log10(wt),na.rm=TRUE)-min(log10(wt),na.rm=TRUE))/5),1,4)
891 axis(side=2,(0:5)/5,labels=labelv,cex.axis=labsc)
900 if (device=="ps" && split==TRUE){
904 file2 <- paste(file,".phs.eps",sep="")
906 postscript(file2,onefile=TRUE,horizontal=TRUE,paper="special",width=10,height=5)
907 cat(paste("# writing",file2," \n"))
911 colors <- rainbow(ncolors)
913 dummy <- gray((0:ncolors)/ncolors)
914 colors[1:(ncolors/2)] <- dummy[(ncolors/2+1):ncolors]
915 colors[(ncolors/2+1):ncolors] <- dummy[1:(ncolors/2)]
919 image(x=t,z=phs,col=colors,axes=FALSE,xlab="Time",ylab="Scale",frame.plot=TRUE,cex.lab=labsc,mgp=mgpv)
921 image(x=t,z=phs,col=colors,axes=FALSE,xlab=paste("Time ","[",units,"]",sep=""),ylab=paste("Scale ","[",units,"]",sep=""),frame.plot=TRUE,cex.lab=labsc,mgp=mgpv)
923 if (is.null(cv)==FALSE) plotcv(t,wt,cv)
924 if (sigplot>1) plotat(t,wt,at,sigplot)
925 if (!cut) plotcoi(t,s0,noctave,w0)
926 plotmarks(t,s0,noctave,markt,marks)
929 axis(side=1,at=axTicks(1),cex.axis=labsc)
932 axis(side=1,xax,labels=as.character(xax),cex.axis=labsc)
934 axis(side=1,xax,labels=xlab,cex.axis=labsc)
937 axis(side=2,sn,labels=as.character(s),cex.axis=labsc)
940 axis(side=2,yax,labels=as.character(yax),cex.axis=labsc)
942 axis(side=2,yax,labels=ylab,cex.axis=labsc)
947 image(z=rangebar,axes=FALSE,col=colors,frame.plot=TRUE,cex.lab=labsc,mgp=mgpv)
948 axis(side=2,(0:4)/4,labels=c("-PI","","0","","PI"),cex.axis=labsc)
952 if (device=="ps") dev.off()
956 ##############################
958 ##############################
960 createwavesurrogates <- function(nsurr=1,wt=1,n,dt=1,s0=1,noctave=5,nvoice=10,w0=2*pi){
962 surrmat <- matrix(rep(0,n*nsurr),ncol=nsurr)
966 cat(paste("# Creating realization ",i,"\n",sep=""))
969 xts <- ts(x,start=0,deltat=dt)
971 xwt <- cwt.ts(xts,s0,noctave,nvoice,w0)
974 surri <- as.vector(invmorlet(wtsurr,0,dt,s0,noctave,nvoice,w0))
976 surrmat[,i] <- Re(surri)
984 surrwave <- function(x,...)
985 UseMethod("surrwave")
987 surrwave.wt <- function(wt,nsurr=1,spec=TRUE){
991 dt <- (wt$time[13]-t0)/12
993 noctave <- wt$noctave
1001 surrmat <- createwavesurrogates(nsurr,wt,n,dt,s0,noctave,nvoice,w0)
1003 surrts <- ts(surrmat,start=t0,deltat=dt)
1009 surrwave.matrix <- function(mat,nsurr=1,t0=0,dt=1,s0=1,noctave=5,nvoice=10,w0=2*pi,sw=0,tw=0,swabs=0,spec=FALSE){
1011 if (sw!=0 & swabs==0)
1012 swabs <- as.integer(sw*nvoice)
1014 scalevector <- 2^(0:(noctave*nvoice)/nvoice)*s0
1016 if ((noctave*nvoice+1)!=dim(mat)[2])
1017 cat("# ERROR! nscales unequal noctave*nvoice+1 ! \n")
1024 mat <- sqrt(Mod(mat))
1026 wtsm <- smooth.matrix(mat,swabs)
1027 wt <- smooth.time(wtsm,tw,dt,scalevector)
1029 surrmat <- createwavesurrogates(nsurr,wt,n,dt,s0,noctave,nvoice,w0)
1031 surrts <- ts(surrmat,start=t0,deltat=dt)
1037 surrwave.character <-
1038 function(file,nsurr=1,t0=0,dt=1,s0=1,noctave=5,nvoice=10,w0=2*pi,sw=0,tw=0,swabs=0,transpose=TRUE,spec=FALSE){
1040 if (sw!=0 & swabs==0)
1041 swabs <- as.integer(sw*nvoice)
1043 scalevector <- 2^(0:(noctave*nvoice)/nvoice)*s0
1045 if (transpose==FALSE)
1046 mat <- matrix(scan(file,comment.char="#"),ncol=nvoice*noctave+1,byrow=TRUE)
1048 mat <- matrix(scan(file,comment.char="#"),ncol=nvoice*noctave+1,byrow=FALSE)
1050 if ((noctave*nvoice+1)!=dim(mat)[2])
1051 cat("# ERROR! nscales unequal noctave*nvoice+1 ! \n")
1058 mat <- sqrt(Mod(mat))
1060 wtsm <- smooth.matrix(mat,swabs)
1061 wt <- smooth.time(wtsm,tw,dt,scalevector)
1063 surrmat <- createwavesurrogates(nsurr,wt,n,dt,s0,noctave,nvoice,w0)
1065 surrts <- ts(surrmat,start=t0,deltat=dt)
1071 surrwave.ts <- function(ts,nsurr=1,s0=1,noctave=5,nvoice=10,w0=2*pi,sw=0,tw=0,swabs=0){
1076 if (sw!=0 & swabs==0)
1077 swabs <- as.integer(sw*nvoice)
1079 scalevector <- 2^(0:(noctave*nvoice)/nvoice)*s0
1081 wt <- Mod(cwt.ts(ts,s0,noctave,nvoice,w0))
1083 wtsm <- smooth.matrix(wt,swabs)
1084 wt <- smooth.time(wtsm,tw,dt,scalevector)
1086 surrmat <- createwavesurrogates(nsurr,wt,n,dt,s0,noctave,nvoice,w0)
1088 surrts <- ts(surrmat,start=t0,deltat=dt)
1094 invmorlet <- function(wt,t0=0,dt=1,s0=1,noctave=5,nvoice=10,w0=2*pi){
1096 if ((noctave*nvoice+1)!=dim(wt)[2])
1097 cat("# ERROR! nscales unequal noctave*nvoice+1 ! \n")
1109 z <- .C("invmorlet",
1110 as.double(as.vector(wtRe)),
1111 as.double(as.vector(wtIm)),
1115 as.integer(noctave),
1118 tsRetmp = as.double(tsRe),
1119 tsImtmp = as.double(tsIm),
1122 invvec=complex(real=z$tsRetmp,imaginary=z$tsImtmp)
1123 invts <- ts(data=invvec,start=t0,deltat=dt)
1129 #################################
1131 #################################
1133 readmatrix <- function(file,M){
1135 A <- matrix(scan(file,comment.char="#"),ncol=M,byrow=TRUE)
1142 readts <- function(file){
1144 A <- matrix(scan(file,comment.char="#"),ncol=2,byrow=TRUE)
1148 Adum[is.na(Adum)] <- 0
1150 t <- Adum %*% c(1,0)
1156 if ((f>11) && (f<13)) f <- 12
1158 timeseries<-ts(data=x,start=t[1],frequency=f)
1164 writematrix <- function(file,data,header="# R Matrix"){
1167 write(t(data),file,ncol(data),append=TRUE)
1171 ############################
1173 ############################
1175 smooth.matrix <- function(wt,swabs){
1178 smwt <- t(filter(t(wt),rep(1,2*swabs+1)/(2*swabs+1)))
1186 smooth.time <- function(wt,tw,dt,scalevector){
1191 for (i in 1:length(scalevector)){
1193 twi <- as.integer(scalevector[i]*tw/dt)
1194 smwt[,i] <- filter(wt[,i],rep(1,2*twi+1)/(2*twi+1))
1204 adjust.noctave <- function(N,dt,s0,tw,noctave){
1207 dumno <- as.integer((log(N*dt)-log(2*tw*s0))/log(2))
1209 cat("# noctave adjusted to time smoothing window \n")
1218 adjust.s0 <- function(s0,dt){
1222 cat(paste("# s0 set to ",s0," \n"))
1229 adjust.timeseries <- function(ts1,ts2){
1231 if (length(ts1)!=length(ts2)){
1232 tsint <- ts.intersect(ts1,ts2)
1234 ts1 <- ts(data=tsint[,1],start=time(tsint)[1],frequency=1/dt)
1235 ts2 <- ts(data=tsint[,2],start=time(tsint)[1],frequency=1/dt)
1239 list(ts1=ts1,ts2=ts2)
1243 checkarealsiglevel <- function(sw,tw,w0,arealsiglevel,siglevel,type){
1251 if (length(swv[swv==sw])==0 || length(twv[twv==tw])==0 ||
1252 length(w0v[w0v==w0])==0){
1254 cat("# areawise test for spectrum currently \n")
1255 cat("# only possible for \n")
1258 cat("# w0 = 2pi \n")
1259 cat("# No areawise test performed \n")
1269 if (length(swv[swv==sw])==0 || length(twv[twv==tw])==0 ||
1270 length(w0v[w0v==w0])==0){
1272 cat("# areawise test for coherence currently \n")
1273 cat("# only possible for \n")
1274 cat("# sw = 0.5 \n")
1275 cat("# tw = 1.5 \n")
1276 cat("# w0 = 2pi \n")
1277 cat("# No areawise test performed \n")
1282 if (arealsiglevel!=0){
1283 arealsiglevel <- 0.9
1285 cat("# currently only siglevel=0.95 and arealsiglevel=0.9 possible for areawise test \n")
1288 list(siglevel=siglevel,arealsiglevel=arealsiglevel)
1292 ########################
1294 as.wt <- function(modulus,phase=NULL,s0=NULL,noctave=NULL,nvoice=NULL,w0=NULL,dt=1,time=NULL,scales=NULL,critval=NULL,at=NULL,kernel=NULL,N=NULL,t0=NULL){
1296 if (is.null(scales))
1301 if ((!gotscales) & (!is.null(s0)) & (!is.null(noctave)) &(!is.null(nvoice))){
1303 scales=2^(0:(noctave*nvoice)/nvoice)*s0
1306 if (gotscales & (is.null(s0) | is.null(noctave) |
1309 noctave <- log(scales[length(scales)]/s0)/log(2)
1310 nvoice <- (length(scales)-1)/noctave
1315 cat("# ERROR! No scales given! \n")
1317 if (is.null(time)) gottimes <- FALSE
1318 else gottimes <- TRUE
1320 if ((!gottimes) & (!is.null(dt)) & (!is.null(t0)) &(!is.null(N))){
1322 time=(0:(N-1))*dt+t0
1326 cat("# ERROR! No time vector given! \n")
1328 wcolist <- list(modulus=modulus,phase=phase,s0=s0,noctave=noctave,nvoice=nvoice,w0=w0,time=time,scales=scales,critval=critval,at=at,kernel=kernel)
1330 class(wcolist) <- "wt"
1336 ########################
1338 wtcolors <- function(ncolors){
1340 upside <- rainbow(ncolors,start=0,end=.7)
1341 #upside <- heat.colors(ncolors+5)
1342 #upside <- upside[1:ncolors]
1347 for (i in 1:ncolors){
1348 down[i] <- upside[ncolors+1-i]
1355 ####################
1357 createwgn <- function(N,sig,dt){
1359 timeseries<-ts(data=rnorm(N,0,sig),start=0,deltat=dt)
1366 createar <- function(N,a,sig,dt){
1368 if (a==0) a <- 0.000000001
1370 se <- sqrt(sig*sig*(1-a*a))
1371 tsMC <- ts(data=rep(0,N),start=0,deltat=dt)
1373 tsMC[1:N] <- arima.sim(list(ar = a), N, sd = se)
1377 ######################
1379 rk <- function(N=1000,s=8,noctave=5,nvoice=10,w0=2*pi,plot=TRUE){
1383 sunit <- s*(w0+sqrt(2+w0*w0))/(4*pi)
1386 #s0unit <- s0*(w0+sqrt(2+w0*w0))/(4*pi)
1387 s0unit=s0/dt*w0/(2*pi) #(CORRECT)
1388 s0log <- as.integer((log2(s0unit)-1)*nvoice+1.5)
1390 totnoct <- noctave+as.integer(s0log/nvoice)+1
1392 x <- morlet(N,N/2,sunit,w0)
1394 totts.cwt <- Mod(cwt(x,totnoct,nvoice,w0,plot=0))
1395 wt=totts.cwt[,s0log:(s0log+noctave*nvoice)]
1399 if (plot==TRUE) plotwt(wt,0,t,s0,noctave,w0,units="",plottitle="Reproducing Kernel")
1407 addvalues <- function(nvoice,dnoctave,x,value){
1411 dnc <- nvoice*dnoctave
1413 y <- matrix(rep(value,nr*(nc+dnc)),nrow=nr)
1415 y[,(dnc+1):(dnc+nc)] <- x
1421 ####################
1423 scalematrix <- function(wt){
1425 # scales matrix, such that the maximal value is one
1426 # wt: matrix to be scaled
1428 mval <- max(wt,na.rm=TRUE)
1437 foldKernel <- function(F,swabs,tw,s,dt){
1439 # folds a matrix (e.g. kernel with smoothing window
1441 # swabs: smooting window width
1443 smsF <- smooth.matrix(F,swabs)
1444 smsF[is.na(smsF)] <- 0
1446 smtsF <- smooth.time(smsF,tw,dt,s)
1448 smtsF[is.na(smtsF)] <- 0
1450 scF <- scalematrix(smtsF)
1456 kernelBitmap <- function(c,s0=1,w0=6,noctave=6,nvoice=20,swabs=0,tw=0,dt=1){
1457 # produces kernel bitmap
1458 # c: height of contour, that defines area
1460 # noctave: number of octaves
1461 # nvoice: number of voices per octave
1462 # swabs: smoothing window length in scale direction
1466 is <- noctave*nvoice
1468 x <- s0*2^(((1:(nvoice*(noctave+2)))-1)/nvoice)
1470 t <- max(2000,max(x)*tw*2/dt*1.1)
1472 y <- ((0:(2*t))-t)/2*dt
1474 X <- matrix(x,ncol=nvoice*(noctave+2),nrow=2*t+1,byrow=T)
1475 Y <- matrix(y,ncol=nvoice*(noctave+2),nrow=2*t+1)
1477 F <- sqrt(2*s*X/(s*s+X*X))*exp(-0.5*(Y*Y+w0*w0*(X-s)*(X-s))/(s*s+X*X))
1479 F <- foldKernel(F,swabs,tw,x,dt)
1485 is2 <- nvoice*(noctave+1)
1489 L <- F[1:(2*t+1),is1]
1491 while (length(L[L!=0])==0) {
1493 L <- F[1:(2*t+1),is1]
1496 L <- F[1:(2*t+1),is2]
1498 while (length(L[L!=0])==0) {
1500 L <- F[1:(2*t+1),is2]
1504 L <- F[it1,1:(nvoice*(noctave+2))]
1506 while (length(L[L!=0])==0) {
1508 L <- F[it1,1:(nvoice*(noctave+2))]
1511 L <- F[it2,1:(nvoice*(noctave+2))]
1513 while (length(L[L!=0])==0) {
1515 L <- F[it2,1:(nvoice*(noctave+2))]
1518 kernel <- list(bitmap=F[(it1-1):(it2+1),(is1-1):(is2+1)],is=is-is1)
1524 kernelRoot <- function(s0=1,w0=6,a=0,noctave=6,nvoice=20,swabs=0,tw=0,dt=1){
1532 while (abs(da/a)>tol){
1534 da <- kernelArea(cntr,s0,w0,a,noctave,nvoice,swabs,tw,dt)
1537 cntr <- mean(c(cntr,cmax))
1541 cntr <- mean(c(cntr,cmin))
1549 kernelArea <- function(cntr,s0=1,w0=6,a=0,noctave=6,nvoice=20,swabs=0,tw=0,dt=1){
1551 # calulates area of reproducing kernel for smoothed data at scale s0*2^noctave
1552 # cntr: height of contour line to define kernel area. This
1553 # parameter is to be estimated!
1555 # w0: parameter of Morlet Wavelet
1556 # a: area offset (only needed, when finding root. Is set to
1558 # noctave: number of octaves
1559 # nvoice: number of voices per octave
1560 # swabs: smoothing window width in scale direction
1565 x <- s0*2^(((1:(nvoice*(noctave+2)))-1)/nvoice)
1567 t <- max(2000,max(x)*tw*2/dt*1.1)
1569 y <- ((0:(2*t))-t)/2*dt
1571 X <- matrix(x,ncol=nvoice*(noctave+2),nrow=2*t+1,byrow=T)
1572 Y <- matrix(y,ncol=nvoice*(noctave+2),nrow=2*t+1)
1574 F <- sqrt(2*s*X/(s*s+X*X))*exp(-0.5*(Y*Y+w0*w0*(X-s)*(X-s))/(s*s+X*X))
1576 F <- foldKernel(F,swabs,tw,x,dt)
1581 area <- length(F[F==1])-a
1588 tobin <- function(x){
1590 # sets nonzero values to one
1599 scaleKernel <- function(kernel,l){
1601 # scales kernel length in time direction proportional to scale
1602 # kernel: data bitmap of width n
1603 # l: new width of kernel
1608 newKernel <- matrix(rep(0,m*l),nrow=l)
1610 d <- as.double(n)/as.double(l)
1613 j <- as.integer((i-0.5)*d)
1615 newKernel[i,1:m] <- kernel[j,1:m]
1622 slope <- function(w0,swabs,tw,nvoice,siglevel,arealsiglevel,type){
1626 if (type==0){ # wavelet spectrum
1628 if (tw == 0 & sw == 0 & w0 == 1 *pi) slp <- 5.82518 # w = 18.35831
1629 if (tw == 1.5 & sw == 0 & w0 == 1 *pi) slp <- 24.69852 # w = 14.30709
1630 if (tw == 3 & sw == 0 & w0 == 1 *pi) slp <- 35.48368 # w = 14.72354
1631 if (tw == 0 & sw == 5 & w0 == 1 *pi) slp <- 7.347707 # w = 17.96942
1632 if (tw == 1.5 & sw == 5 & w0 == 1 *pi) slp <- 28.24291 # w = 12.65993
1633 if (tw == 3 & sw == 5 & w0 == 1 *pi) slp <- 51.13723 # w = 10.96359
1634 if (tw == 0 & sw == 10 & w0 == 1 *pi) slp <- 10.47856 # w = 15.5941
1635 if (tw == 1.5 & sw == 10 & w0 == 1 *pi) slp <- 45.07387 # w = 15.29793
1636 if (tw == 3 & sw == 10 & w0 == 1 *pi) slp <- 52.82886 # w = 12.72361
1638 if (tw == 0 & sw == 0 & w0 == 2 *pi) slp <- 8.718912 # w = 17.75933
1639 if (tw == 1.5 & sw == 0 & w0 == 2 *pi) slp <- 11.88006 # w = 15.39648
1640 if (tw == 3 & sw == 0 & w0 == 2 *pi) slp <- 26.55977 # w = 1.064384
1641 if (tw == 0 & sw == 5 & w0 == 2 *pi) slp <- 14.64761 # w = 16.27518
1642 if (tw == 1.5 & sw == 5 & w0 == 2 *pi) slp <- 28.27798 # w = 14.57059
1643 if (tw == 3 & sw == 5 & w0 == 2 *pi) slp <- 63.54121 # w = 12.83778
1644 if (tw == 0 & sw == 10 & w0 == 2 *pi) slp <- 27.78735 # w = 11.95813
1645 if (tw == 1.5 & sw == 10 & w0 == 2 *pi) slp <- 41.27260 # w = 12.03379
1646 if (tw == 3 & sw == 10 & w0 == 2 *pi) slp <- 67.37015 # w = 10.63935
1650 if (type==1){ # wavelet coherence
1652 if (tw==0 & sw==0 & w0==pi) slp <- 999 #not valid
1653 if (tw==1.5 & sw==0 & w0==pi) slp <- 1
1654 if (tw==3 & sw==0 & w0==pi) slp <- 1
1655 if (tw==0 & sw==0.5 & w0==pi) slp <- 1
1656 if (tw==1.5 & sw==0.5 & w0==pi) slp <- 1
1657 if (tw==3 & sw==0.5 & w0==pi) slp <- 1
1658 if (tw==0 & sw==1 & w0==pi) slp <- 1
1659 if (tw==1.5 & sw==1 & w0==pi) slp <- 1
1660 if (tw==3 & sw==1 & w0==pi) slp <- 1
1662 if (tw==0 & sw==0 & w0==2*pi) slp <- 999 #not valid
1663 if (tw==1.5 & sw==0 & w0==2*pi) slp <- 1
1664 if (tw==3 & sw==0 & w0==2*pi) slp <- 1
1665 if (tw==0 & sw==0.5 & w0==2*pi) slp <- 1
1666 if (tw==1.5 & sw==0.5 & w0==2*pi) slp <- 8.3
1667 if (tw==3 & sw==0.5 & w0==2*pi) slp <- 1
1668 if (tw==0 & sw==1 & w0==2*pi) slp <- 1
1669 if (tw==1.5 & sw==1 & w0==2*pi) slp <- 1
1670 if (tw==3 & sw==1 & w0==2*pi) slp <- 1
1672 if (tw==0 & sw==0 & w0==3*pi) slp <- 999 #not valid
1673 if (tw==1.5 & sw==0 & w0==3*pi) slp <- 1
1674 if (tw==3 & sw==0 & w0==3*pi) slp <- 1
1675 if (tw==0 & sw==0.5 & w0==3*pi) slp <- 1
1676 if (tw==1.5 & sw==0.5 & w0==3*pi) slp <- 1
1677 if (tw==3 & sw==0.5 & w0==3*pi) slp <- 1
1678 if (tw==0 & sw==1 & w0==3*pi) slp <- 1
1679 if (tw==1.5 & sw==1 & w0==3*pi) slp <- 1
1680 if (tw==3 & sw==1 & w0==3*pi) slp <- 1
1682 if (tw==0 & sw==0 & w0==4*pi) slp <- 999 #not valid
1683 if (tw==1.5 & sw==0 & w0==4*pi) slp <- 1
1684 if (tw==3 & sw==0 & w0==4*pi) slp <- 1
1685 if (tw==0 & sw==0.5 & w0==4*pi) slp <- 1
1686 if (tw==1.5 & sw==0.5 & w0==4*pi) slp <- 1
1687 if (tw==3 & sw==0.5 & w0==4*pi) slp <- 1
1688 if (tw==0 & sw==1 & w0==4*pi) slp <- 1
1689 if (tw==1.5 & sw==1 & w0==4*pi) slp <- 1
1690 if (tw==3 & sw==1 & w0==4*pi) slp <- 1
1694 cat(paste("# slope ",slp,"\n",sep=""))
1700 #============================
1701 #============================
1702 #============================
1704 ## 1. Read auxiliar data files ####
1706 identifiants <- read.table("identifs.txt")[ ,1]
1707 dates0 <- read.table("datesall.txt")[, 1]
1708 dates <- as.character(dates0[grep("2009", dates0)])
1711 n <- length(identifiants)
1712 p <- delta <- length(dates)
1714 synchros09 <- t(as.matrix(read.table("~/tmp/2009_synchros200RC.txt")))
1715 #synchros09 <- t(as.matrix(read.table("~/tmp/2009_synchros200-random.txt")))
1717 nas <- which(is.na(synchros09)[, 1]) # some 1/1/2009 are missing
1718 synchros09[nas, 1] <- rowMeans(synchros09[nas, 2:4]) #valeurs après 1er janvier
1720 #moyenne pondérée pour compléter deux demi-heures manquantes
1721 imput09 <- synchros09[, 4180:4181] %*% matrix(c(2/3, 1/3, 1/3, 2/3), 2)
1722 synchros09 <- cbind(synchros09[, 1:4180], imput09, synchros09[, 4181:17518])
1724 conso <- synchros09[-201, ]; # series must be on rows
1726 delta <- ncol(conso)
1730 ## 2. Compute WER distance matrix ####
1732 ## _.a CWT -- Filtering the lowest freqs (>6m) ####
1734 # # noctave4 = 2^13 = 8192 half hours ~ 180 days
1735 # noctave4 <- adjust.noctave(N = delta, dt = 1, s0 = 2,
1736 # tw = 0, noctave = 13)
1737 # # 4 here represent 2^5 = 32 half-hours ~ 1 day
1738 # scalevector4 <- 2^(4:(noctave4 * nvoice) / nvoice) * 2
1739 # lscvect4 <- length(scalevector4)
1740 # lscvect <- lscvect4 # i should clean my code: werFam demands a lscvect
1743 #17000 colonnes coeff 1, puis 17000 coeff 2... [non : dans chaque tranche du cube]
1745 #TODO: une fonction qui fait lignes 59 à 91
1748 # Xcwt4 <- toCWT(conso, noctave = noctave4, dt = 1,
1749 # scalevector = scalevector4,
1750 # lt = delta, smooth = FALSE,
1751 # nvoice = nvoice) # observations node with CWT
1754 # ############Xcwt2 <- matrix(0.0, nrow= n, ncol= 2 + delta * lscvect)
1755 # #Xcwt2 <- matrix(NA_complex_, nrow= n, ncol= 2 + length((c(Xcwt4[,,1]))))
1757 # #NOTE: delta et lscvect pourraient etre gardés à part (communs)
1759 # Xcwt2[i,] <- c(delta, lscvect, Xcwt4[,,i] / max(Mod(Xcwt4[,,i])) )
1761 # #rm(conso, Xcwt4); gc()
1763 # ## _.b WER^2 distances ########
1764 # Xwer_dist <- matrix(0.0, n, n)
1765 # for(i in 1:(n - 1)){
1766 # mat1 <- vect2mat(Xcwt2[i,])
1767 # for(j in (i + 1):n){
1768 # mat2 <- vect2mat(Xcwt2[j,])
1769 # num <- Mod(mat1 * Conj(mat2))
1770 # WX <- Mod(mat1 * Conj(mat1))
1771 # WY <- Mod(mat2 * Conj(mat2))
1772 # smsmnum <- smCWT(num, scalevector = scalevector4)
1773 # smsmWX <- smCWT(WX, scalevector = scalevector4)
1774 # smsmWY <- smCWT(WY, scalevector = scalevector4)
1775 # wer2 <- sum(colSums(smsmnum)^2) /
1776 # sum( sum(colSums(smsmWX) * colSums(smsmWY)) )
1777 # Xwer_dist[i, j] <- sqrt(delta * lscvect * (1 - wer2))
1778 # Xwer_dist[j, i] <- Xwer_dist[i, j]
1781 # diag(Xwer_dist) <- numeric(n)
1783 # save(Xwer_dist, file = "../res/2009_synchros200WER.Rdata")
1784 # save(Xwer_dist, file = "../res/2009_synchros200-randomWER.Rdata")
1786 load("../res/2009_synchros200WER.Rdata")
1787 #load("../res/2009_synchros200-randomWER.Rdata")
1789 ## 3. Cluster using WER distance matrix ####
1791 #hc <- hclust(as.dist(Xwer_dist), method = "ward.D")
1794 # #clust <- cutree(hc, 2)
1798 #pamfit <- pam(tdata[-201, ci$selectv], k = K)
1799 pamfit <- pam(as.dist(Xwer_dist), k = K, diss = TRUE)
1801 #table(pamfit$clustering)
1803 SC <- matrix(0, ncol = p, nrow = K)
1805 clustfactor <- pamfit$clustering
1807 # clustk <- which(clustfactor == k)
1808 # if(length(clustk) > 0) {
1809 # if(length(clustk) > 1) {
1810 # SCk <- colSums(synchros09[which(clustfactor == k), ])
1812 # SCk <- synchros09[which(clustfactor == k), ]
1814 # SC[k, ] <- SC[k, ] + SCk
1819 #write.table(clustfactor, file = paste0("~/tmp/clustfactorRC", K, ".txt"))
1820 #write.table(clustfactor, file = "~/tmp/clustfactor3.txt")
1821 #write.table(clustfactor, file = paste0("~/tmp/clustfactorWER", K, ".txt"))
1822 write.table(clustfactor, file = paste0("~/tmp/clustfactor-randomWER", K, ".txt"))
1827 # matplot(t(SC)[48*10 + 1:(48*30), ], type = 'l', ylab = '',col = 1:3, lty = 1)
1828 # matplot(t(SC)[48*100 + 1:(48*30), ], type = 'l', ylab = '', col = 1:3, lty = 1)