Github link to txt file`So I have a project , of which the data are of atomic force microscopy. I have two values d = distance and f = force plotted on a normal x-y graph. So there will curves of slightly differnt shapes and values but will look somehow the same.
I am using numpy and using sliding window method unfortuantely it does not work ans the proffesor told me in one curve the data points are increasing and on the other the data points are dereasing. My sample code is :
def find_linear_part(d, f, window_size=120, min_slope=0.01):
slope = 0
intercept = 0
fit = 0
min_residual = float('inf')
for i in range(len(d) - window_size + 1):
d_window = d[i:i + window_size]
f_window = f[i:i + window_size]
slope, intercept = np.polyfit(d_window, f_window, 1)
y_fit = slope * d_window + intercept
residual = np.sum((f_window - y_fit) ** 2)
if residual < min_residual:
min_residual = residual
fit = (d_window, f_window)
new_slope = slope
new_intercept = intercept
return fit, new_slope, new_intercept
I used here a threshold for the slope as some of the slopes were mentioned on the graph .`
and those are images of the expected graphs for instance.
find_linear_part = lambda: 0
;-)