Best fit to an oscillating function

In summary, the conversation discusses a plot of a function obtained numerically that has oscillations at a high frequency and an envelope with a lower frequency. The speaker is trying to find a function that closely matches this plot and is considering using a Fourier transform for this purpose. They also mention the possibility of downshifting the signal to make it more amenable to the transform. The conversation ends with a discussion about using Mathematica to find the edges of the plot and determine the desired low frequency signal.
  • #1
kelly0303
580
33
Hello! I have a plot of a function, obtained numerically, that looks like the red curve in the attached figure. It is hard to tell, but if you zoom in enough, inside the red shaded area you actually have oscillations at a very high frequency, ##\omega_0##. On top of that you have some sort of envelope, with a lower frequency, ##\omega_1##. I am trying to find a function that comes as close as possible to this. In green is what I obtained using:

$$A\sin(\omega_1 t/2)^2(1+\cos(\omega_0t/2))^2$$
where ##A## is just an overall amplitude (and I shifted everything for clarity). However I am not sure how to get the rest of the behaviour, basically fill in the gaps in my function relative to the red one. Can someone advice me on what functional form would I need to add to my expression to get that? Thank you!

func.png
 
Mathematics news on Phys.org
  • #2
Have you considered a Fourier transform?
 
  • Like
Likes Scottpm, berkeman, FactChecker and 1 other person
  • #3
DaveE said:
Have you considered a Fourier transform?
Thank you! What exactly do you mean? Fourier transform of what?
 
  • #4
kelly0303 said:
Thank you! What exactly do you mean? Fourier transform of what?
Fourier transform of your original signal. It will tell you how to write it as a sum of sines and cosines. If you absolutely to have it as an envelope times a constant oscillation, it will take a bit more work.

If you don't mind sharing the data (at least privately), I would like to take a go at it.
 
  • #5
DrClaude said:
Fourier transform of your original signal. It will tell you how to write it as a sum of sines and cosines. If you absolutely to have it as an envelope times a constant oscillation, it will take a bit more work.

If you don't mind sharing the data (at least privately), I would like to take a go at it.
Ah I see, thank you I will look into that. There is no actual data. The red-line is generated by numerically integrating on ODE, but I am attaching below the points used in that plot.
 

Attachments

  • data.txt
    2.4 MB · Views: 121
  • #6
If each low-frequency cycle contains thousands (or even hundreds) of high frequency cycles, you may have numerical problems with the Fourier transform. You will have to process way more samples than are really needed to answer your question.

Instead, shift the signal down so that your ##\omega_0 ## is lower, keeping your ##\omega_1## structure the same. (Then when you plot it, you will be able to see, let's say 20 cycles of ##\omega_0 /2## within one cycle of ##\omega_1##. After this down shift the signal will be more amenable to Fourier transform with a moderate number of samples.

You can down shift the "carrier" frequency by multiplying the original signal by ##e^{-\omega_2 t}## where ##\omega_2## is a few percent less than ##\omega_0##.

Edit:
As hutchpd has said, you need an 'i' in the exponential.

Downshifting will be a bit more complicated than I have indicated above... I will post later with more details, or maybe someone can fill in the details.
 
Last edited:
  • #7
You need an "i" in the exponential yes?
 
  • #8
hutchphd said:
You need an "i" in the exponential yes?
Yup.

Edit: Wait, I believe there was an i in the original... But anyway.
 
  • Like
Likes hutchphd
  • #9
It will probably work if you just take every 10th or 100th sample (leaving you with say 20 samples per "big" cycle) and pretend that's your actual signal.
 
  • #10
Just for fun, I asked Mathematica to find the edges in the OP's plot:

1678151257093.png


I am now trying to find out if Mathematica can convert the bitmap edges to vectors. Any ideas?

Also, is it correct that the desired low freq. signal is the average of the upper and lower boundaries of the outlines above?
 

FAQ: Best fit to an oscillating function

What is the best fit to an oscillating function?

The best fit to an oscillating function refers to the mathematical model that most accurately represents the data points of an oscillatory phenomenon, such as sine or cosine functions. This often involves using techniques like least squares fitting to determine the parameters (amplitude, frequency, phase, and offset) that minimize the difference between the observed data and the model predictions.

How do I choose the right model for fitting an oscillating function?

Choosing the right model involves understanding the nature of the data and the expected behavior of the oscillation. Common models include simple sine or cosine functions, damped oscillations, or more complex combinations. It is essential to consider the physical context, the presence of noise, and any potential harmonics that might affect the fitting process.

What methods are commonly used for fitting oscillating functions?

Common methods for fitting oscillating functions include least squares regression, nonlinear curve fitting, and Fourier analysis. Nonlinear optimization techniques, such as the Levenberg-Marquardt algorithm, are often employed to fit parameters when the model is nonlinear. Additionally, tools like Python's SciPy library or specialized software can facilitate this process.

How can I evaluate the quality of the fit for an oscillating function?

The quality of the fit can be evaluated using several criteria, including the coefficient of determination (R²), residual analysis, and root mean square error (RMSE). Visual inspection of the fitted curve against the data points is also important, as is checking for patterns in the residuals to ensure that the model captures the underlying oscillatory behavior adequately.

What challenges might I face when fitting an oscillating function?

Challenges in fitting oscillating functions can include noise in the data, which can obscure the underlying oscillatory signal, the presence of multiple frequencies, and the potential for local minima in the fitting process. Additionally, overfitting can occur if the model is too complex relative to the amount of data, leading to poor generalization to new data points.

Similar threads

Back
Top