英文:
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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论