/* Authors Martin Schlather, schlather@math.uni-mannheim.de calculation of the Hurst coefficient and the fractal dimension of the graph of a random field Copyright (C) 2002 - 2017 Martin Schlather, This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include "RF.h" SEXP boxcounting(SEXP Z, // data SEXP LX, // basic length of data SEXP Repet, // "independent" repetitions of data SEXP Factor, // rescalin of data values by factor SEXP Eps // box lengths ) { // in sum, the boxcounting result for the first lx data values is given // according to the box lengths given by eps; then the boxes are counted // for the next lx data values, etc. Hence, sum must have dimension int *eps = INTEGER(Eps), leps = length(Eps), repet = INTEGER(Repet)[0], lx = INTEGER(LX)[0]; long i, k, j, e, r, lastbox, s, // 0 to leps * repet -1 truelx = 2 + lx, total = repet * truelx; double zz, min, max, f, *sum, factor = REAL(Factor)[0], *z = REAL(Z); SEXP Sum; PROTECT(Sum = allocVector(REALSXP, leps * repet)); sum = REAL(Sum); // boxcounting expects that the first and the last values are repeated // in the first dimension, see D.H.R; accordingly the total length in // the first direction is increased by 2: for (s=r=0; r max) max = z[i]; assert(i <= r + lx + 1); if ((zz = 0.5 * (z[i-1] + z[i])) < min) min = zz; else if (zz > max) max = zz; f = factor / (double) e; sum[s] += FLOOR(max * f) - FLOOR(min * f) + 1.0; } } } UNPROTECT(1); return Sum; } SEXP periodogram(SEXP Dat, // data SEXP Len, // basic length of data SEXP Repet, // # of "independent" repetitions SEXP FFTM, // fftm[0] >=1 not >=1 (R indexing assumed) // interval of the vector given by fft of part SEXP Part, // not the basic length but only a part is // transformed SEXP Shift // part is then shifted by shift until end of // vector is reached; fft values are averaged // over all these parts ){ int *fftm = INTEGER(FFTM), part = INTEGER(Part)[0]; assert((fftm[0]>=1) && (fftm[1] <=part) && (fftm[1] >= fftm[0])); long seg_dat, k, j, total_seg, segment_r, repet = INTEGER(Repet)[0], start_k = 2 * (fftm[0] - 1), // of complex numbers end_k = 2 * (fftm[1] - 1), // of complex numbers segm_l, r, delta_l = fftm[1] - fftm[0] + 1, end_l = delta_l * repet, err = ERRORFAILED, len = INTEGER(Len)[0], lenMpart = len - part, shift = INTEGER(Shift)[0]; assert(lenMpart >= 0); assert(shift>=1); double taper_fact = SQRT( 2.0 / (3.0 * ((double) part + 1.0)) ), cos_factor = 2.0 * PI / ((double) part + 1.0), *compl_number = NULL, *taper = NULL, *lambda = NULL, n_inv = 1.0 / (double) ((int) (1.0 + lenMpart / (double) shift)), *dat = REAL(Dat), factor = LOG(2.0 * PI * len) ; FFT_storage FFT; SEXP Lambda; PROTECT(Lambda = allocVector(REALSXP, end_l)); lambda = REAL(Lambda); for (j=0; j 1); double var, a, b, residual, Yt, realm, sumt, meanY, t, VarMeth_old, VarMeth_mean, VarMeth_var, delta, realnbox, *lvar, *dat = REAL(Dat); SEXP Lvar; // ## rbind(l.varmeth.var, l.dfa.var) 2 * (repet * ldfa) PROTECT(Lvar = allocMatrix(REALSXP, 2, ldfa * repet)); lvar = REAL(Lvar); for (e = r = 0; r < total; r += lx, e+=ldfa) { last = r + lx; for (j=r+1; j 1) { VarMeth_mean = dat[ex - 1] / (realnbox); VarMeth_old = 0; VarMeth_var = 0; for (j=r+m-1; j max) max = dat[idx]; } count[cb] += max - min; } count[cb] = LOG(count[cb] / (double) epsilon); } // b } // r UNPROTECT(1); return Count; }