Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Math
- import math
- import numpy as np
- # Tools
- def tabulate(x, y, f):
- """Return a table of f(x, y). Useful for the Gram-like operations."""
- return np.vectorize(f)(*np.meshgrid(x, y, sparse=True))
- def cos_sum(a, b):
- """To work with tabulate."""
- return(math.cos(a+b))
- def gramian_angular_field(serie):
- """
- Compute the Gramian Angular Field of a given time serie.
- Parameters:
- - serie: (np.array of shape (n, 1)) input time-serie
- Output:
- - gaf: (np.array of shape (n, n)) Gramian Angular Field generated
- """
- # Min-Max scaling
- min_ = np.min(serie)
- max_ = np.max(serie)
- scaled_serie = (2*serie - max_ - min_)/(max_ - min_)
- # Floating point inaccuracy!
- scaled_serie = np.where(scaled_serie >= 1., 1., scaled_serie)
- scaled_serie = np.where(scaled_serie <= -1., -1., scaled_serie)
- # Polar encoding
- phi = np.arccos(scaled_serie)
- r = np.linspace(0, 1, len(scaled_serie))
- # GAF Computation (every term of the matrix)
- gaf = tabulate(phi, phi, cos_sum)
- return(gaf)
Add Comment
Please, Sign In to add comment