#include #include #include #ifndef M_PI #define M_PI 3.14159265358979323846 #endif // n: number of synchrones, m: length of a synchrone float computeWerDist(float* s1, float* s2, int n, int m) { //TODO: automatic tune of all these parameters ? (for other users) int nvoice = 4; //noctave 2^13 = 8192 half hours ~ 180 days ; ~log2(ncol(synchrones)) int noctave = 13 // 4 here represent 2^5 = 32 half-hours ~ 1 day //NOTE: default scalevector == 2^(0:(noctave * nvoice) / nvoice) * s0 (?) //R: scalevector <- 2^(4:(noctave * nvoice) / nvoice + 1) int* scalevector = (int*)malloc( (noctave*nvoice-4 + 1) * sizeof(int)) for (int i=4; i<=noctave*nvoice; i++) scalevector[i-4] = pow(2., (float)i/nvoice + 1.); //condition: ( log2(s0*w0/(2*pi)) - 1 ) * nvoice + 1.5 >= 1 int s0 = 2; double w0 = 2*M_PI; bool scaled = false; int s0log = as.integer( (log2( s0*w0/(2*pi) ) - 1) * nvoice + 1.5 ) int totnoct = noctave + as.integer(s0log/nvoice) + 1 ///TODO: continue computeCWT = function(i) { if (verbose) cat(paste("+++ Compute Rwave::cwt() on serie ",i,"\n", sep="")) ts <- scale(ts(synchrones[i,]), center=TRUE, scale=scaled) totts.cwt = Rwave::cwt(ts,totnoct,nvoice,w0,plot=0) ts.cwt = totts.cwt[,s0log:(s0log+noctave*nvoice)] #Normalization sqs <- sqrt(2^(0:(noctave*nvoice)/nvoice)*s0) sqres <- sweep(ts.cwt,2,sqs,'*') sqres / max(Mod(sqres)) } if (parll) { cl = parallel::makeCluster(ncores_clust) parallel::clusterExport(cl, varlist=c("synchrones","totnoct","nvoice","w0","s0log","noctave","s0","verbose"), envir=environment()) } # (normalized) observations node with CWT Xcwt4 <- if (parll) parallel::parLapply(cl, seq_len(n), computeCWT) else lapply(seq_len(n), computeCWT) if (parll) parallel::stopCluster(cl) Xwer_dist <- bigmemory::big.matrix(nrow=n, ncol=n, type="double") fcoefs = rep(1/3, 3) #moving average on 3 values (TODO: very slow! correct?!) if (verbose) cat("*** Compute WER distances from CWT\n") #TODO: computeDistances(i,j), et répartir les n(n-1)/2 couples d'indices #là c'est trop déséquilibré computeDistancesLineI = function(i) { if (verbose) cat(paste(" Line ",i,"\n", sep="")) for (j in (i+1):n) { #TODO: 'circular=TRUE' is wrong, should just take values on the sides; to rewrite in C num <- filter(Mod(Xcwt4[[i]] * Conj(Xcwt4[[j]])), fcoefs, circular=TRUE) WX <- filter(Mod(Xcwt4[[i]] * Conj(Xcwt4[[i]])), fcoefs, circular=TRUE) WY <- filter(Mod(Xcwt4[[j]] * Conj(Xcwt4[[j]])), fcoefs, circular=TRUE) wer2 <- sum(colSums(num)^2) / sum( sum(colSums(WX) * colSums(WY)) ) if (parll) synchronicity::lock(m) Xwer_dist[i,j] <- sqrt(delta * ncol(Xcwt4[[1]]) * (1 - wer2)) Xwer_dist[j,i] <- Xwer_dist[i,j] if (parll) synchronicity::unlock(m) } Xwer_dist[i,i] = 0. } parll = (requireNamespace("synchronicity",quietly=TRUE) && parll && Sys.info()['sysname'] != "Windows") if (parll) m <- synchronicity::boost.mutex() ignored <- if (parll) { parallel::mclapply(seq_len(n-1), computeDistancesLineI, mc.cores=ncores_clust, mc.allow.recursive=FALSE) } else lapply(seq_len(n-1), computeDistancesLineI) Xwer_dist[n,n] = 0. mat_dists = matrix(nrow=n, ncol=n) #TODO: avoid this loop? for (i in 1:n) mat_dists[i,] = Xwer_dist[i,] mat_dists