Guest User

Untitled

a guest
Aug 15th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. # Math
  2. import math
  3. import numpy as np
  4.  
  5. # Tools
  6. def tabulate(x, y, f):
  7. """Return a table of f(x, y). Useful for the Gram-like operations."""
  8. return np.vectorize(f)(*np.meshgrid(x, y, sparse=True))
  9.  
  10. def cos_sum(a, b):
  11. """To work with tabulate."""
  12. return(math.cos(a+b))
  13.  
  14. def gramian_angular_field(serie):
  15. """
  16. Compute the Gramian Angular Field of a given time serie.
  17. Parameters:
  18. - serie: (np.array of shape (n, 1)) input time-serie
  19.  
  20. Output:
  21. - gaf: (np.array of shape (n, n)) Gramian Angular Field generated
  22. """
  23. # Min-Max scaling
  24. min_ = np.min(serie)
  25. max_ = np.max(serie)
  26. scaled_serie = (2*serie - max_ - min_)/(max_ - min_)
  27.  
  28. # Floating point inaccuracy!
  29. scaled_serie = np.where(scaled_serie >= 1., 1., scaled_serie)
  30. scaled_serie = np.where(scaled_serie <= -1., -1., scaled_serie)
  31.  
  32. # Polar encoding
  33. phi = np.arccos(scaled_serie)
  34. r = np.linspace(0, 1, len(scaled_serie))
  35.  
  36. # GAF Computation (every term of the matrix)
  37. gaf = tabulate(phi, phi, cos_sum)
  38. return(gaf)
Add Comment
Please, Sign In to add comment