在Python中找到~sin^2数据的初始频率猜测。

huangapple go评论64阅读模式
英文:

find initial guess for frequency of ~sin^2 data in python

问题

I am currently facing a problem of having to guess an already rather accurate frequency from data that I do not know the frequency of.
我目前面临的问题是必须从我不知道频率的数据中猜测出一个已经相当准确的频率。

I know that the data should relate to fit = A* sin^2(ω * t + φ) since I know through other means that we are calculating a Rabi-frequency.
我知道数据应该与 fit = A* sin^2(ω * t + φ) 相关,因为我通过其他方式知道我们正在计算 Rabi 频率。

I can take some guesses myself and try them out but I need to automate this as I will be generating way more of this kind of data in a bit, which would require a bit too much work when guessing manually.
我可以自己猜测一些值并尝试,但我需要自动化这个过程,因为我将在不久的将来生成更多这种类型的数据,如果手动猜测,工作量会太大。

Here is a plot of how the data with a fit over it looks.
这是数据及其拟合的绘图。

I tried some FFT stuff but even after subtracting the mean I am getting frequencies which are too high or too low resulting in the fit not working properly.
我尝试了一些FFT的方法,但即使在减去均值后,得到的频率也太高或太低,导致拟合效果不好。

As I also do not know how future data may look like (it all comes from a really complicated ODE solution that gets simulated over a lot of params in a very specific way) I absolutely need a rather robust solution.
而且,我也不知道未来的数据会是什么样子(它都来自一个非常复杂的ODE解决方案,以非常特定的方式在许多参数上进行模拟),所以我绝对需要一个相当健壮的解决方案。

My FFT implementation looks like this:
我的FFT实现如下:

def find_initial_freq(data, timestep):
    new_data = data-np.mean(data)
    fft_data = np.fft.fft(new_data)
    freqs = np.fft.fftfreq(len(data), d=timestep)
    peak = np.argmax(np.abs(fft_data))
    freq = freqs[peak]
    print(freq)

this results in a frequency of 0.11, the guess that works is about 0.35.
这导致一个频率为0.11的结果,而有效的猜测频率约为0.35。

What am I doing wrong?
我做错了什么?

英文:

I am currently facing a problem of having to guess an already rather accurate frequency from data that I do not know the frequency of.
I know that the data should relate to fit = A* sin^2(ω * t + φ) since I know through other means that we are calculating a Rabi-frequency. I can take some guesses myself and try them out but I need to automate this as I will be generating way more of this kind of data in a bit, which would require a bit too much work when guessing manually.

Here is a plot of how the data with a fit over it looks.
在Python中找到~sin^2数据的初始频率猜测。

I tried some FFT stuff but even after subtracting the mean I am getting frequencies which are too high or too low resulting in the fit not working properly. As I also do not know how future data may look like (it all comes from a really complicated ODE solution that gets simulated over a lot of params in a very specific way) I absolutely need a rather robust solution.
My FFT implementation looks like this:

def find_initial_freq(data, timestep):
    new_data = data-np.mean(data)
    fft_data = np.fft.fft(new_data)
    freqs = np.fft.fftfreq(len(data), d=timestep)
    peak = np.argmax(np.abs(fft_data))
    freq = freqs[peak]
    print(freq)

this results in a frequency of 0.11, the guess that works is about 0.35.
What am I doing wrong?

答案1

得分: 1

这个评论看起来,你正在计算由np.fft.fftfreq返回的频率f,并将其用作方程sin(ωt)中的径向频率ω

这两者之间的关系是ω=2πf

然后你还需要考虑到正弦函数的平方会使频率加倍,因此sin^2(ωt)的径向频率为2ω

将所有这些放在一起,如果你设定ω=0.35,你应该会看到一个频率f=2ω/2π=ω/π,这与你测量的0.11非常接近。

英文:

From this comment it seems that you are computing the frequency f as returned by np.fft.fftfreq, and using it as the radial frequency ω in the equation sin(ωt).

The two are related by ω=2πf.

Then you also need to take into account that squaring the sine doubles the frequency, so sin^2(ωt) has a radial frequency of 2ω.

Putting it all together, if you set ω=0.35, you should see a frequency f=2ω/2π=ω/π, which is pretty close to the 0.11 you measured.

huangapple
  • 本文由 发表于 2023年5月6日 18:41:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定