2

I have some data in python which represents the motion of detected SURF keypoints across multiple frames of a video. I need to separate the points which are oscillating from the points experiencing other kinds of motion. I am thinking that I should be able to do this by fitting a sine function to the data and then thresholding the parameters to discard motion outside the parameters of what I'm looking for.

So I defined the generic sine function and call scipy.optimize.curve_fit

def sine(x,a,b,c,d):
    return a * np.sin(b*(x+c)) + d

opt, cov = opt.curve_fit(sine, np.linspace(0,119,119), x[:,5])

(x[:,5] is a data element I know to have fairly clean and obvious sine characteristics)

But when I plot sine(sine(np.linspace(0,119,119), *opt) against x[:,5] in matplotlib I end up with a curve that doesn't look anything like my data except perhaps the y-axis bias

Data Plot

Am I missing something, or is this simply outside of what can be done?

  • 2
    Sine is a nasty function to fit. You should provide a good initial guess via `p0` to the optimizer. – DYZ Jun 05 '18 at 07:43
  • take a look at this: [Trying to fit a sine function to phased light curve](https://stackoverflow.com/a/41208993/2521214) – Spektre Jun 05 '18 at 07:47
  • 2
    As DyZ said the initial guess parameter `p0` is essential. Find the first `min` and `max` values. Half of their difference gives you an estimate for `a`, their shift on the x-axis an idea of `b`, their mean is an estimate for `d`. – Mr. T Jun 05 '18 at 08:03

0 Answers0