I have a set of data, y is angular orientation, and x is the timestamp for each point of y.
The entire data set has many segments for angular orientation. Inorder to do curve fitting, I have split the data into their respective segments, storing each segment as an numpy array.
I then find a polynomial fit using numpy.polyfit to find a curve fit for each segment of the data. However because my data is purely experimental, I have no knowledge of which polynomial degree to use for numpy.polyfit, thus I iterate through a range of polynomial degrees till I find the highest polynomial degree possible.
This is my code:
# Experimental data stored in lists: time_aralist and orienrad_aralist
# List stores the segments as arrays
fig = plt.figure()
# Finding curve fit
fittime_aralist, fitorienrad_aralist, fitorienrad_funclist = [], [], []
for j in range(len(time_aralist)):
z, res, _, _, _ = np.polyfit(time_aralist[j], orienrad_aralist[j], 200, full=True)
orienrad_func = np.poly1d(z)
fittime_ara = np.linspace(time_aralist[j][0], time_aralist[j][-1], 10000)
fitorienrad_ara = orienrad_func(fittime_ara)
# Appending to list
fittime_aralist.append(fittime_ara)
fitorienrad_aralist.append(fitorienrad_ara)
fitorienrad_funclist.append(orienrad_func)
# Plotting experimental data
for j in range(len(time_aralist)):
plt.plot(time_aralist[j], orienrad_aralist[j], 'ro')
for j in range(len(fittime_aralist)):
plt.plot(fittime_aralist[j], fitorienrad_aralist, 'k-')
This is what my plot looks like. Centered in the plot is one segment.
The black lines give the attempted curve fit, and the red dots are the experimental points.
As seen in the diagram, the black curve fitted lines do not really fit the data points very well. My region of interest is the middle region of the segment, however the region is not fitted well as well, despite using the highest polynomial degree possible.
Could anyone provide me with any supplementary techniques, or an alternative code that could fit the data better?