I am trying to calculate the curvature of a 2D curve at each point using the formula here. The problem I am having is that while I am getting a constant value as it should be, this value is not the correct one. Here is my code:
from scipy.ndimage import gaussian_filter1d
import numpy as np
def curvature(x, y):
#first and second derivative
x1 = gaussian_filter1d(x, sigma=1, order=1, mode='wrap')
x2 = gaussian_filter1d(x, sigma=1, order=2, mode='wrap')
y1 = gaussian_filter1d(y, sigma=1, order=1, mode='wrap')
y2 = gaussian_filter1d(y, sigma=1, order=2, mode='wrap')
return np.abs(x1*y2 - y1*x2) / np.power(x1**2 + y1**2, 3/2)
# make circle data
alpha = np.linspace(-np.pi/2,np.pi/2, 1000)
R = 5
x = R*np.cos(alpha)
y = R*np.sin(alpha)
>>> 1 / curvature(x, y)
array([ 9.60e+02, 5.65e+01, 4.56e-01, 1.41e-02, 6.04e-01,
6.04e-01, 6.04e-01, 6.04e-01, 6.04e-01, 6.04e-01,
6.04e-01, 6.04e-01, 6.04e-01, 6.04e-01, 6.04e-01,
...
I was expecting to get something close to a 5. Can somebody help me spot the error or suggest a more robust way to do this? In practice my x,y points are not evenly spaced.
EDIT: I am using gaussian_filter1d
instead of np.gradient
for the derivative because it was shown here that this is a more robust method, especially for the second derivative.