sam1902

Varriation method for fractal dimension in Python Numpy

Mar 12th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. import numpy as np
  2.  
  3. PRECISION = 100
  4.  
  5. def osc(tau, f, x, precision=10000):
  6.     ens = np.linspace(x-tau, x+tau, precision)
  7.     # Computes (precision)-values of f between x-tau and x+tau
  8.     voisinage = f(ens)
  9.     # returns the maximum amplitude
  10.     return abs(np.amax(voisinage) - np.amin(voisinage))
  11. vect_osc = np.vectorize(osc)
  12.  
  13. def var(tau, f, a, b):
  14.     global PRECISION
  15.     x = np.linspace(a, b, PRECISION)
  16.     # Computes the oscillation of f for different values of x
  17.     y = vect_osc(tau, f, x)
  18.     # returns the average oscillation for each x around tau
  19.     return np.average(y)
  20. vect_var = np.vectorize(var)
  21.  
  22. def variation_dimension(f, a, b):
  23.     global PRECISION
  24.    
  25.     # from 0.00...001 to 0.99...99 since log(1) = 0 and log(0) is undefined
  26.     taus = np.linspace(1e-9, 1-1e-9, PRECISION)
  27.     # Computes the variation for different values of tau
  28.     variations = vect_var(taus, f, a, b)
  29.     # Fits linearly the  x=log(taus) and y=log(variations)
  30.     coeffs = np.polyfit(np.log(taus), np.log(variations), 1)
  31.     # Computes the estimated fractal dimension
  32.     dim = 2 - coeffs[0]
  33.     return dim, taus, variations
Advertisement
Add Comment
Please, Sign In to add comment