python实现简单的傅里叶变换
傅里叶变换参考:傅里叶变换之掐死教程-前言-谷动谷力 (sunsili.com) 傅里叶变换之掐死教程-傅里叶级数(Fourier Series)-谷动谷力 (sunsili.com) 傅里叶变换之掐死教程-指数形式的傅里叶变换-谷动谷力 (sunsili.com) 傅里叶变换之掐死教程-嘛叫频域 概念讲解-谷动谷力 (sunsili.com) 傅里叶变换之掐死教程-宇宙耍帅第一公式:欧拉公式-谷动谷力 (sunsili.com) 傅里叶变换之掐死教程-相位普-谷动谷力 (sunsili.com)
python实现实例实例1.
实例2.
实例3.
具体代码以第一个函数为例 - #!/usr/bin/env python
- # -*- coding:utf-8 -*-
- from pylab import *
- import matplotlib.pyplot as plt
- from sympy import * #用于求导积分等科学计算
- from scipy.integrate import tplquad,dblquad,quad
- #原函数f(x)={ exp(x), -pi≤x<0
- # 1, 0≤x<pi}
- ax1 = plt.subplot(2, 2, 1)
- ax2 = plt.subplot(2, 2, 2)
- ax3=plt.subplot(2, 2, 3)
- ax4=plt.subplot(2, 2, 4)
- #原函数
- x_value=[]
- y_value=[]
- for i in np.arange(-pi, pi, 0.01):
- if(i<0):
- x_value.append(i)
- y_value.append(i+pi)
- else:
- x_value.append(i)
- y_value.append(pi-i)
- plt.sca(ax1)
- plt.plot(x_value,y_value,'orange',linewidth=0.6)
- plt.title('original function')
- a_value = []
- b_value = []
- from pylab import *
- x = mgrid[-10:10:0.02]
- def fourier_transform():
- plt.sca(ax2)
- m1, err1 = quad(lambda m: np.exp(m), -np.pi, 0)
- m2,err2=quad(lambda m: 1, 0, np.pi)
- a0 = 1 / pi * m1 + 1 / pi * m2
- s=a0/2
- for i in range(1,100,1):
- mm1,errm1=quad(lambda m: np.exp(m)* np.cos(i * m), -np.pi, 0)
- mm2,errm2=quad(lambda m: np.cos(i * m), 0, np.pi)
- a=1 / pi * mm1 + 1 / pi * mm2
- a_value.append(a)
- nn1, errn1 = quad(lambda m: np.exp(m) * np.sin(i * m), -np.pi, 0)
- nn2, errn2 = quad(lambda m: np.sin(i * m), 0, np.pi)
- b = 1 / pi * nn1 + 1 / pi * nn2
- b_value.append(b)
- s0 = ( 1 / pi *a*cos(i*x) + 1 / pi *b* sin(i*x) )
- s=s+s0
- plot(x,s,'orange',linewidth=0.6)
- title('fourier_transform')
- fourier_transform()
- plt.sca(ax3)
- plot(np.arange(len(x_value)),x_value,'orange',linewidth=0.6)
- title('X')
- plt.sca(ax4)
- plot(np.arange(len(y_value)),y_value,'orange',linewidth=0.6)
- title('Y')
- plt.show()
复制代码
|