简体   繁体   中英

Sine Function Fitting in Java - interpreting output from commons.apache

I am interested in creating a Sine Function in Java that best adapts to some given points. These points are given in the following array:

double[] array_to_fit = {0,2,4,6,8,8,5,3,3,0};

I've found the HarmonicCurveFitter function for Java and decided to use it in Android. In order to use them with this function, I believe I need to 'pre-process' the data by subtracting the average to each point, which gives me the following:

double[] array_to_fit = {-3.9,-1.9,0.1,2.1,4.1,4.1,1.1,-0.9,-0.9,-3.9};

And fit the data into the HarmonicCurveFitter object:

        //add points to WeightedObservedPoints object
        for (int index = 0; index < array_to_fit.length; index++) {
            obs.add(index, array_to_fit[index]-average);
        }
        double[] bestFit = fitter.fit(obs.toList());
        amp = bestFit[0];
        freq = bestFit[1];
        phase = bestFit[2];

The returned output from the Android function is the following:

  • Amp: 3.7572
  • Angular Freq (omega): 0.6273
  • Phase: -2.7699

I've done the same exercise in Python using SciPy , with the following outputs:

  • Amp: 3.7572
  • Angular Freq (omega): 0.6273
  • Phase: -1.1991

As you see the Phase values are differing. When I plot both functions in a graph:

Visual output graph results

As you can see the sine function created in Python really seems to be ok, while the sine function from Android seems to be slightly shifted in the horizontal axis...

Does anyone know the reason for this? Am I wrongly interpreting the phase output from the HarmonicCurveFitter Function?

Solved! The mistake was that I was expecting a sine but the function in this lib is expressed in cosine. The function is as stated

f(t) = A * cos(omega*t + phi)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM