- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想弄清楚我在这里不明白的是什么。
我正在关注 http://www.scipy.org/Cookbook/FittingData并试图拟合正弦波。真正的问题是卫星磁力计数据,它在旋转的航天器上产生了一个很好的正弦波。我创建了一个数据集,然后尝试对其进行拟合以恢复输入。
这是我的代码:
import numpy as np
from scipy import optimize
from scipy.optimize import curve_fit, leastsq
import matplotlib.pyplot as plt
class Parameter:
def __init__(self, value):
self.value = value
def set(self, value):
self.value = value
def __call__(self):
return self.value
def fit(function, parameters, y, x = None):
def f(params):
i = 0
for p in parameters:
p.set(params[i])
i += 1
return y - function(x)
if x is None: x = np.arange(y.shape[0])
p = [param() for param in parameters]
return optimize.leastsq(f, p, full_output=True, ftol=1e-6, xtol=1e-6)
# generate a perfect data set (my real data have tiny error)
def mysine(x, a1, a2, a3):
return a1 * np.sin(a2 * x + a3)
xReal = np.arange(500)/10.
a1 = 200.
a2 = 2*np.pi/10.5 # omega, 10.5 is the period
a3 = np.deg2rad(10.) # 10 degree phase offset
yReal = mysine(xReal, a1, a2, a3)
# plot the real data
plt.figure(figsize=(15,5))
plt.plot(xReal, yReal, 'r', label='Real Values')
# giving initial parameters
amplitude = Parameter(175.)
frequency = Parameter(2*np.pi/8.)
phase = Parameter(0.0)
# define your function:
def f(x): return amplitude() * np.sin(frequency() * x + phase())
# fit! (given that data is an array with the data to fit)
out = fit(f, [amplitude, frequency, phase], yReal, xReal)
period = 2*np.pi/frequency()
print amplitude(), period, np.rad2deg(phase())
xx = np.linspace(0, np.max(xReal), 50)
plt.plot( xx, f(xx) , label='fit')
plt.legend(shadow=True, fancybox=True)
这使得这个情节:
[44.2434221897 8.094832581 -61.6204033699]
的恢复拟合参数与我开始时的参数完全不同。
对我不理解或做错的事情有什么想法吗?
scipy.__version__
'0.10.1'
编辑:建议固定一个参数。在上面的示例中,将振幅固定为 np.histogram(yReal)[1][-1]
仍然会产生 Not Acceptable 输出。拟合:[175.0 8.31681375217 6.0]
我应该尝试不同的拟合方法吗?有什么建议?
最佳答案
这里是一些实现 Zhenya 想法的代码。它使用
yhat = fftpack.rfft(yReal)
idx = (yhat**2).argmax()
freqs = fftpack.rfftfreq(N, d = (xReal[1]-xReal[0])/(2*pi))
frequency = freqs[idx]
猜测数据的主要频率,以及
amplitude = yReal.max()
猜测振幅。
import numpy as np
import scipy.optimize as optimize
import scipy.fftpack as fftpack
import matplotlib.pyplot as plt
pi = np.pi
plt.figure(figsize = (15, 5))
# generate a perfect data set (my real data have tiny error)
def mysine(x, a1, a2, a3):
return a1 * np.sin(a2 * x + a3)
N = 5000
xmax = 10
xReal = np.linspace(0, xmax, N)
a1 = 200.
a2 = 2*pi/10.5 # omega, 10.5 is the period
a3 = np.deg2rad(10.) # 10 degree phase offset
print(a1, a2, a3)
yReal = mysine(xReal, a1, a2, a3) + 0.2*np.random.normal(size=len(xReal))
yhat = fftpack.rfft(yReal)
idx = (yhat**2).argmax()
freqs = fftpack.rfftfreq(N, d = (xReal[1]-xReal[0])/(2*pi))
frequency = freqs[idx]
amplitude = yReal.max()
guess = [amplitude, frequency, 0.]
print(guess)
(amplitude, frequency, phase), pcov = optimize.curve_fit(
mysine, xReal, yReal, guess)
period = 2*pi/frequency
print(amplitude, frequency, phase)
xx = xReal
yy = mysine(xx, amplitude, frequency, phase)
# plot the real data
plt.plot(xReal, yReal, 'r', label = 'Real Values')
plt.plot(xx, yy , label = 'fit')
plt.legend(shadow = True, fancybox = True)
plt.show()
产量
(200.0, 0.5983986006837702, 0.17453292519943295) # (a1, a2, a3)
[199.61981404516041, 0.61575216010359946, 0.0] # guess
(200.06145097308041, 0.59841420869261097, 0.17487141943703263) # fitted parameters
请注意,通过使用 fft,对频率的猜测已经非常接近最终拟合参数。
看来您不需要修复任何参数。通过使频率猜测更接近实际值,optimize.curve_fit
能够收敛到一个合理的答案。
关于python - SciPy leastsq 适合正弦波失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13405053/
我写的函数有问题。想法是使用 taylor 展开而不是 js 数学对象来计算 sin 和 cosin 值(在 radians 上运行) .这些是方程式: sin(x) = (x^1)/1! - (x^
我不知道这是编程还是数学问题,但我整理了一些FFT的简短示例。我加载了440hz的波并在顶部添加了一些正弦波,但是由于某种原因,频谱中存在一个我不理解的“波”。 据我了解,频谱应该具有相同的| Y(f
这个问题在这里已经有了答案: Java Math.cos(Math.toRadians()) returns weird values (4 个答案) 关闭 10 年前。 我正在编写一个程序,我必须
我想在 ios4 中实现一个正弦和余弦计算器: if([operation isEqual:@"sin"]){ operand = (operand*M_PI/180.0); oper
我使用 256 个元素为 VHDL 制作了一个正弦 LUT。 我使用 MIDI 输入,因此值范围为 8.17Hz(注 #0)到 12543.85z(注 #127)。 我有另一个 LUT 计算必须发送到
我想在ios4中实现一个正弦和余弦计算器: if([operation isEqual:@"sin"]){ operand = (operand*M_PI/180.0); operan
我是一名优秀的程序员,十分优秀!