numpy拟合的傅立叶级数:fft与编码

numpy拟合的傅立叶级数:fft与编码,第1张

概述假设我有一些要拟合傅立叶级数的数据y.在此post上,Mermoz使用该系列的复杂格式发布了一个解决方案,并“用riemann和计算系数”.在另一个post上,通过FFT获得序列,并写下一个示例.我尝试实现两种方法(下面的图像和代码-注意每次运行代码时,由于使用numpy.random.normal会生成不同的数据),但我想知道为什么我得到了不同的结果-黎曼

假设我有一些要拟合傅立叶级数的数据y.在此post上,Mermoz使用该系列的复杂格式发布了一个解决方案,并“用riemann和计算系数”.在另一个post上,通过FFT获得序列,并写下一个示例.

我尝试实现两种方法(下面的图像和代码-注意每次运行代码时,由于使用numpy.random.normal会生成不同的数据),但我想知道为什么我得到了不同的结果-黎曼方法似乎“错误地移位”,而FFT方法似乎“被压缩”.我也不确定我对系列“ tau”期间的定义.感谢您的关注.

我在windows 7上将Spyder与Python 3.7.1结合使用

Example

import matplotlib.pyplot as pltimport numpy as np# Assume x (independent variable) and y are the data.# Arbitrary numerical values for question purposes:start = 0stop = 4mean = 1sigma = 2N = 200terms = 30 # number of terms for the FourIEr serIEsx = np.linspace(start,stop,N,endpoint=True) y = np.random.normal(mean,sigma,len(x))# FourIEr serIEstau = (max(x)-min(x)) # assume that signal length = 1 period (tau)# From ref 1def cn(n):    c = y*np.exp(-1j*2*n*np.pi*x/tau)    return c.sum()/c.sizedef f(x,Nh):    f = np.array([2*cn(i)*np.exp(1j*2*i*np.pi*x/tau) for i in range(1,Nh+1)])    return f.sum()y_FourIEr_1 = np.array([f(t,terms).real for t in x])# From ref 2Y = np.fft.fft(y)np.put(Y,range(terms+1,len(y)),0.0) # zero-ing coefficIEnts above "terms"y_FourIEr_2 = np.fft.ifft(Y)# Visualizationf,ax = plt.subplots()ax.plot(x,y,color='lightblue',label = 'artificial data')ax.plot(x,y_FourIEr_1,label = ("'riemann' serIEs fit (%d terms)" % terms))ax.plot(x,y_FourIEr_2,label = ("'FFT' serIEs fit (%d terms)" % terms))ax.grID(True,color='dimgray',linestyle='--',linewidth=0.5)ax.set_axisbelow(True)ax.set_ylabel('y')ax.set_xlabel('x')ax.legend()
最佳答案执行两个小的修改足以使总和几乎类似于np.fft的输出. The FFTW library indeed computes these sums.

1)信号的平均值c [0]应考虑:

f = np.array([2*cn(i)*np.exp(1j*2*i*np.pi*x/tau) for i in range(0,Nh+1)]) # here : 0,not 1

2)必须缩放输出.

y_FourIEr_1=y_FourIEr_1*0.5


输出似乎被“压缩”,因为高频分量已被滤波.实际上,输入的高频振荡已经清除,输出看起来像是移动平均线.

在这里,tau实际上定义为停止开始:它对应于帧的长度.这是信号的预期周期.

如果帧与信号周期不对应,则可以通过将信号与自身卷积并找到第一个最大值来猜测其周期.看到
Find period of a signal out of the FFT不过,它不太可能与numpy.random.normal生成的数据集一起正常工作:这是Additive White Gaussian Noise.由于它具有恒定的功率谱密度,因此很难将其描述为周期性的! 总结

以上是内存溢出为你收集整理的numpy拟合的傅立叶级数:fft与编码 全部内容,希望文章能够帮你解决numpy拟合的傅立叶级数:fft与编码 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1199526.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存