Skip to content

Instantly share code, notes, and snippets.

View Steboss's full-sized avatar

Steboss Steboss

View GitHub Profile
@Steboss
Steboss / return_Hurst_exponent.c
Created February 25, 2021 10:12
Compute Hurst exponent from RMS-log2 transform
// 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
@Steboss
Steboss / 3_compute_the_rms.c
Created February 25, 2021 10:07
Compute the final RMS
// 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);
}
@Steboss
Steboss / 3_compute_the_rms.c
Created February 25, 2021 09:59
Compute the rms element-wise for each chunk
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
@Steboss
Steboss / define_fitting_line_values.c
Created February 25, 2021 09:56
Define the fitting lines values
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));
}
@Steboss
Steboss / 2_cycle_through_scales.c
Created February 25, 2021 09:50
First cycle through scales
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;
@Steboss
Steboss / 1_define_arrays.c
Created February 25, 2021 09:32
First step of the dfa function
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
#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
@Steboss
Steboss / memview_init.pyx
Created February 23, 2021 13:50
Initialise memory views
# 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
@Steboss
Steboss / declare_all_c.pyx
Created February 23, 2021 13:33
Declare all the C codes used
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,
@Steboss
Steboss / plot_rms.py
Created February 23, 2021 13:31
Plot of log2 RMS with Hurst coefficient
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()