Skip to content

Instantly share code, notes, and snippets.

@Steboss
Created February 23, 2021 13:50
Show Gist options
  • Save Steboss/f654209d99cec62008768887f08e9cc7 to your computer and use it in GitHub Desktop.
Save Steboss/f654209d99cec62008768887f08e9cc7 to your computer and use it in GitHub Desktop.
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
# use intc otherwise np.int it's long which is not what we want and we'll have a type mismatch
scales = (2**np.arange(scale_low, scale_high, scale_dense)).astype(np.intc)
scale_length = len(scales)
cdef int[:] memory_scales = scales
# initialize flucutations
cdef float[:] fluct = np.zeros(scale_length, dtype=np.float32)
# initialize coefficients
cdef float[:] coeffs = np.zeros(2, dtype=np.float32)
print("Calling C")
#call C-DFA and pass the memoryview to be populated
dfa(&a[0],
n_elems,
&memory_scales[0],
scale_length,
&fluct[0],
&coeffs[0])
# reconvert back to python format
coeff_to_array = np.asarray(coeffs)
fluct_to_array = np.asarray(fluct)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment