This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// What we have computed are the fluctuations | |
// now we can retrieve the Hurst exponent via a polyfit again | |
float* log2_scales ; | |
// create a log2 x-axis | |
log2_scales = (float*)malloc(scales_len*sizeof(float)); | |
for(int h=0; h<scales_len;h++){ | |
log2_scales[h]= Log2( (float)scales[h]); | |
} | |
// transform the RMS (fluctuations) with log2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from 2nd cycle, after each j has completed | |
// Free tmpDiff and tmpX | |
free (tmpDiff); | |
tmpDiff = NULL; // point to NULL | |
free (tmpX); | |
tmpX = NULL; | |
// compute the squared of the element-wise RMS | |
for (int h=0; h<shape1;h++){ | |
tmpShape1[h] = pow(tmpShape1[h],2); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(int t=0; t<curr_scale;t++){ | |
for(int u=(fitting_order); u>-1;u--){ | |
if(u==0){ | |
//this is the last step | |
fitting_result+= tmpCoeff[u]; //here no tmpX[t] because we have 0 for x order so x^0=1 | |
} | |
else{ | |
//tmpCoeff * x^fitting_order - u | |
//e.g. tmpCoeff[0]*x --> fitting-Order = 1 and u = 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(int u=(fitting_order); u>-1;u--){ | |
if(u==0){ | |
//this is the last step | |
fitting_result+= tmpCoeff[u]; //here no tmpX[t] because we have 0 for x order so x^0=1 | |
} | |
else{ | |
//tmpCoeff * x^fitting_order - u | |
//e.g. tmpCoeff[0]*x --> fitting-Order = 1 and u = 0 | |
fitting_result+=tmpCoeff[u]*(pow(scale_ax[t],u)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (int i=0; i<scales_len; i++){ | |
// take the current scale | |
int curr_scale = scales[i]; | |
// determine the dimensions of the current signal | |
int shape1 = floor_div(n_elems, curr_scale); | |
// initialize a counter, in order to cycle through x correctly | |
// this ensures to update the strided single vector element tmpX | |
int counter =0; | |
//initialize a temporary X vector, to host single strided Xcumsum matrix | |
float* tmpX; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void dfa(float* X, int n_elems, int* scales, int scales_len, float* fluct, float* coeffs){ | |
/* Main DFA function | |
Parameters | |
---------- | |
X: float* | |
input signal vector | |
n_elems: int | |
number of elements in vector X |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <time.h> | |
#include "polyfit.h" | |
// UGLY CONSTANTS | |
#define M_PI 3.14159265358979323846 | |
#define PI 3.14159265 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# convert the input signal to memoryview | |
n_elems = len(X) | |
# convert to float and vectorize | |
X_cython = np.zeros(n_elems, dtype=np.float32) | |
for i in range(n_elems): | |
X_cython[i] = X[i] | |
cdef float[:] a = X_cython | |
# create the scales and convert to memoryview |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cdef extern from "../c_code/dfa.c": | |
void dfa(float* X, | |
int n_elems, | |
int* scales, | |
int scales_len, | |
float* fluct, | |
float* coeffs) | |
cdef extern from "../c_code/polyfit.c": | |
int polyfit(float* dependentValues, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fluctfit = 2**np.polyval(coeff_to_array,np.log2(scales)) | |
plt.loglog(scales, fluct_to_array, 'bo') | |
plt.loglog(scales, fluctfit, 'r', label=r'$\alpha$ = %0.2f'%coeff_to_array[0]) | |
plt.title('DFA') | |
plt.xlabel(r'$\log_{10}$(time window)') | |
plt.ylabel(r'$\log_{10}$<F(t)>')# | |
plt.legend() | |
plt.show() |