# Fit a line pattern on curve with unknown number of points

I've got a sample curve which ends theoretically with decreasing exponential. The curve end falls into noise. The sample points are given in log scale. What I want to do, is to find and fit the linear part of the log curve to retrieve the exponential factor. The trick is that I don't know the starting point neither the ending point of the linear part of the log curve.

The strategy I'm using is to fit a line at each point with at least say 20 points and until the end of the curve is reached. Then from all these regressions, I keep the one with the best determination coefficient.

I did several tests and found that the RMS error increases with the number of points systematically as the curve becomes more and more noisy, so the extracted factor is always computed on the minimum number of samples (20 in this example case).

My question is, is there a more efficient method to compute this factor ? Does increasing the minimum number of samples improve the accuracy of the fit ?

Here a sample curve I want to fit

The computed slope regarding the position in the curve

And the corresponding RMS error

A useful approach could be to take the numerical derivative of the function. Then you will get some constant line at some $y=a$, where $a$ is non-zero. This would help to eliminate all the other parts and to determine the starting and eding points of the line.
About the fit Matlab has a polyfit command. If the curve of interest is linear than basically this command with parameter $2$ as input will give you the pair $(a,b)$ s.t. the best $ax+b$ fits into the numberical numbers that you provide.