Skip to content

Instantly share code, notes, and snippets.

@rmitsch
Last active November 14, 2017 17:12
Show Gist options
  • Save rmitsch/1d4cb30500330b4df3586cc91eb2127b to your computer and use it in GitHub Desktop.
Save rmitsch/1d4cb30500330b4df3586cc91eb2127b to your computer and use it in GitHub Desktop.
import sys
import numpy
import matplotlib.pyplot as plt
def apply_hough_transformation(data, num_dims, alpha_values):
"""
Maps data points to parameter functions.
:param data:
:param num_dims:
:param alpha_values:
:return: List of nd-arrays (data points <-> rows, paramter function values <-> columns).
"""
parameter_function_values = []
# Apply Hough transformations on each point.
for data_point in data:
delta = numpy.zeros([1, len(alpha_values[0])])
# Iterate over point's attributes.
for i in range(0, num_dims):
pi_sinus_alpha = numpy.ones([1, len(alpha_values[0])])
# Iterate over sinus values.
for j in range(0, i):
pi_sinus_alpha *= numpy.sin(alpha_values[j])
delta += data_point[i] * pi_sinus_alpha * numpy.cos(alpha_values[i])
parameter_function_values.append(delta)
return parameter_function_values
data = numpy.array([[1, 2], [2, 3], [3, 4], [4, 5], [6, 7], [7, 2], [1, 5]])
num_points = len(data)
num_dims = len(data[0])
alpha_values = [numpy.arange(start=0, stop=360 * numpy.pi / 180, step=0.1) for i in range(0, num_dims)]
fig, subplots = plt.subplots(1, 2, sharex=False, sharey=False, squeeze=False)
subplots[0, 0].scatter(data[:, 0:1], data[:, 1:2])
subplots[0, 0].grid(True)
subplots[0, 0].set_ylim(0, 10)
subplots[0, 0].set_xlim(0, 10)
subplots[0, 1].set_ylabel("$\delta$")
subplots[0, 1].set_xlabel("$\\alpha\:in\:^{\circ}$")
subplots[0, 1].grid(True)
# Calculate and plot Hough transform.
for index, param_function_value in enumerate(apply_hough_transformation(data, num_dims, alpha_values)):
subplots[0, 1].plot(alpha_values[0] * 180 / numpy.pi, param_function_value.T, label="${f_p}_" + str(index + 1) + "$")[0]
subplots[0, 1].legend(bbox_to_anchor=(1.01, 1), loc=2, borderaxespad=0.)
# fig.tight_layout();
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment