python fft横轴_IFFT-FFT后缩放X轴

本文探讨了在使用Python进行FFT和IFFT运算后如何正确缩放X轴,以确保数据的准确恢复。通过示例代码展示了如何进行线性插值和频域转换,并指出X轴转换中的问题及其影响。
摘要由CSDN通过智能技术生成

importnumpyasnpimportmatplotlib.pyplotaspltimportscipyfromscipy.interpolateimportinterp1dC_LIGHT=299.792# for easier case, this is zero, so it can be ignored.def_disp(x,GD=0,GDD=0,TOD=0,FOD=0,QOD=0):returnx*GD+(GDD/2)*x**2+(TOD/6)*x**3+(FOD/24)*x**4+(QOD/120)*x**5# the generator to make sample datasetsdefgenerator(start,stop,center,delay,GD=0,GDD=0,TOD=0,FOD=0,QOD=0,resolution=0.1,pulse_duration=15,chirp=0):window=(np.sqrt(1+chirp**2)*8*np.log(2))/(pulse_duration**2)lamend=(2*np.pi*C_LIGHT)/startlamstart=(2*np.pi*C_LIGHT)/stoplam=np.arange(lamstart,lamend+resolution,resolution)omega=(2*np.pi*C_LIGHT)/lamrelom=omega-centeri_r=np.exp(-(relom)**2/(window))i_s=np.exp(-(relom)**2/(window))i=i_r+i_s+2*np.sqrt(i_r*i_s)*np.cos(_disp(relom,GD=GD,GDD=GDD,TOD=TOD,FOD=FOD,QOD=QOD)+delay*omega)#since the _disp polynomial is set to be zero, it's just cos(delay*omega)returnomega,idefinterpol(x,y):''' Simple linear interpolation '''xs=np.linspace(x[0],x[-1],len(x))intp=interp1d(x,y,kind='linear',fill_value='extrapolate')ys=intp(xs)returnxs,ysdefifft_method(initSpectrumX,initSpectrumY,interpolate=True):iflen(initSpectrumY)>0andlen(initSpectrumX)>0:Ydata=initSpectrumYXdata=initSpectrumXelse:raiseValueErrorN=len(Xdata)ifinterpolate:Xdata,Ydata=interpol(Xdata,Ydata)# the (2*np.pi) division is because we have angular frequency, not frequencyxf=np.fft.fftfreq(N,d=(Xdata[1]-Xdata[0])/(2*np.pi))*N*Xdata[-1]/(N-1)yf=np.fft.ifft(Ydata)else:pass# some irrelevant code therereturnxf,yfdeffft_method(initSpectrumX,initSpectrumY):iflen(initSpectrumY)>0andlen(initSpectrumX)>0:Ydata=initSpectrumYXdata=initSpectrumXelse:raiseValueErroryf=np.fft.fft(Ydata)xf=np.fft.fftfreq(len(Xdata),d=(Xdata[1]-Xdata[0])*2*np.pi)# the problem is there, where I transform the x values.xf=np.fft.ifftshift(xf)returnxf,yf# the generated datax,y=generator(1,3,2,delay=1500,resolution=0.1)# plt.plot(x,y)xx,yy=ifft_method(x,y)#if the x values are correctly scaled, the two symmetrical spikes should appear exactly at delay value# plt.plot(xx, np.abs(yy))#do the cutting there, which is also irrelevant now# the problem is there, in fft_method. The x values are not the same as before transforms.xxx,yyy=fft_method(xx,yy)plt.plot(xxx,np.abs(yyy))#and it should look like this:#xs = np.linspace(x[0], x[-1], len(x))#plt.plot(xs, np.abs(yyy))plt.grid()plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值