-
-
Save quantra-go-algo/1d147a2c76ac51ef13856a3df13cae78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pythagoras function to calculate distance between two points | |
def pythg(pt1, pt2): | |
a_sq = (pt2[0] - pt1[0]) ** 2 | |
b_sq = (pt2[1] - pt1[1]) ** 2 | |
return sqrt(a_sq + b_sq) | |
# Function to calculate local minima and maxima points | |
def loc_min_max(points): | |
loc_minima = [] | |
loc_maxima = [] | |
prev_pts = [(0, points[0]), (1, points[1])] | |
for i in range(1, len(points) - 1): | |
append_to = '' | |
if points[i-1] > points[i] < points[i+1]: | |
append_to = 'min' | |
elif points[i-1] < points[i] > points[i+1]: | |
append_to = 'max' | |
if append_to: | |
if loc_minima or loc_maxima: | |
prev_distance = pythg(prev_pts[0], prev_pts[1]) * 0.5 | |
curr_distance = pythg(prev_pts[1], (i, points[i])) | |
if curr_distance >= prev_distance: | |
prev_pts[0] = prev_pts[1] | |
prev_pts[1] = (i, points[i]) | |
if append_to == 'min': | |
loc_minima.append((i, points[i])) | |
else: | |
loc_maxima.append((i, points[i])) | |
else: | |
prev_pts[0] = prev_pts[1] | |
prev_pts[1] = (i, points[i]) | |
if append_to == 'min': | |
loc_minima.append((i, points[i])) | |
else: | |
loc_maxima.append((i, points[i])) | |
return loc_minima, loc_maxima | |
# Store the local minima and maxima points | |
loc_minima, loc_maxima = loc_min_max(points) | |
minima_pts = np.array(loc_minima) | |
maxima_pts = np.array(loc_maxima) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment