Reputation: 5
I am working on a curve-fitting optimization problem using sinusoidal wave data. I have around 16 cycles of almost identical sine wave data, but I am encountering an issue with the y-axis offset. The offset is always slightly off, and I am not sure what might be causing this discrepancy.
Here is a brief description of my data and approach:
I would appreciate any insights or suggestions on what might be causing this issue and how to resolve it. Thank you!
from scipy.optimize import curve_fit
def sine_wave(t, A, phi_beta, phi_alpha, C):
return A * np.sin(2 * np.pi * primary_frequency * t + phi_beta*t +phi_alpha) + C
initial_guess = [np.ptp(CH2) / 2, 800, 200, np.mean(CH2)]
params, _ = curve_fit(sine_wave, time, CH2, p0=initial_guess, maxfev=10000)
A, phi_beta,phi_alpha, C = params
fitted_CH2_with_primary_freq = sine_wave(time, average_amplitude, phi_beta, phi_alpha, C)
The output is below:
Upvotes: 0
Views: 79
Reputation: 477
Without having the data at hand I will have to speculate a bit but from experience I would guess that you have one of the following problems
I believe this to be the most likely. This would mean that your data just isn't a sine wave and this is in fact the best fit. Do you have any theory to suggest that your data should follow a sine wave? (Not all periodic functions are sine waves. I have seen a lot of undergrads make mistakes like this.)
Scipy curve_fit
uses the scipy.optimize.minimize
function, which is a local minimizer. If your X² is non convex in parameter space you may be getting stuck in a local minimum. You can try to measure your systemic bias separately and set it as a constraint. (Or just replace C with the number you measure) Calculate the reduced Chi Squared for both and select the better seloution. Alternatively you can define your own loss functon and run a global search algorithm like scipy.optimize.dual_annealing()
. (I have implemented this for distribution fitting in my fitting_toolkit repository)
I do not think this is the issue. I have never had this problem with sin-wave, however I will add it for completeness. scipy.optimize.curvefit
uses least squares fitting which is notoriously unstable for non polynomial models. In that case you may want to use another optimization like Maximum Likelyhood Estimation
Upvotes: 0