我用示波器读取正弦波。有超过30 * 10 ^ 6点分为几千个段(5000)。我需要适合这些数据,细分市场。问题是程序在安装一些段后会卡住。我得到的错误是
RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 1000.
我已经检查了程序卡住的段的数据。数据是所有其他段的正弦波。使用try
和except
时,我没有收到此错误,但程序在没有异常处理的情况下卡在段之后停止执行一些段。
有人可以向我解释问题是什么以及如何绕过它?
import numpy as np
import scipy as sp
import readTrc #package to read data from oscilloscope
ch = 'mydata.trc'
tw, ampl, m = readTrc.readTrc(ch)
l=m['WAVE_ARRAY_COUNT']//m['SUBARRAY_COUNT'] #number of points per segment
tw_re = np.reshape(tw, (m['SUBARRAY_COUNT'], l)) #time
ampl_re = np.reshape(ampl, ((m['SUBARRAY_COUNT']), l)) #amplitude
#find frequency of sinewave
fastfft = np.fft.fft(ampl)
freq_array = np.fft.fftfreq(len(ampl),tw[0]-tw[1])
max_value_index = np.argmax(abs(fastfft[1:]) + 1)
frequency = abs(freq_array[max_value_index])
#fit
def fit(tw, a, f, p, o):
return a * np.sin(2*np.pi*f*tw + p) + o
guess = (0.1,frequency,np.pi,0.01)
for i in range(m['SUBARRAY_COUNT']):
params, pcov = sp.optimize.curve_fit(fit, tw_re[i], ampl_re[i], p0=guess)
a, f, p, o = params