Created
October 28, 2010 05:53
-
-
Save jonspeicher/650726 to your computer and use it in GitHub Desktop.
Read Cartesian points from a CSV and resample at a regular interval with linear interpolation.
This file contains hidden or 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
#!/usr/bin/env python | |
import bisect, sys | |
def point(entry): | |
return tuple([float(value) for value in entry.split(',')])[0:2] | |
def normalize(value, max_value): | |
return value * (1.0 / max_value) | |
def bounding_points(x, keys, points): | |
i = bisect.bisect(keys, x) | |
return (points[i - 1], points[i]) | |
def interpolate(x, ((x0, y0), (x1, y1))): | |
return y0 + ((x - x0) * ((y1 - y0) / (x1 - x0))) | |
def format(x, value): | |
return str(x) + ' ' + str(value) + '\n' | |
with open(sys.argv[2]) as f: | |
entries = f.readlines() | |
num_points = int(sys.argv[1]) | |
points = sorted([point(entry) for entry in entries[2:]], key = lambda point: point[0]) | |
keys = [point[0] for point in points] | |
normalized_range = [normalize(x, num_points) for x in range(0, num_points)] | |
interpolated_values = [interpolate(x, bounding_points(x, keys, points)) for x in normalized_range] | |
output = [format(x, interpolated_values[x]) for x in range(0, num_points)] | |
with open(sys.argv[3], 'w') as f: | |
f.writelines(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment