闲来无事,想起之前看来关于傅里叶分析的文章,进行一个简单的仿真;
具体的文章的地址如下:
https://blog.csdn.net/u013921430/article/details/79683853
python实现的代码如下:
'''
author : sunyan
'''
from matplotlib import pyplot as plt
import numpy as np
import math
plt.figure(figsize = (10, 10))
three_d_flg = True #3D开启标志位
all_pit_flg = True #是否要绘制所有图形
f = 2 #正弦函数的频率
te_100 = 0 #由于range方法不能生成小数,所以先放大,实际时间再缩小
re_sin = 0
acc_end = 1000 #放大倍数,如果想要提高采样精度,放大此值
fu_n = 23 #叠加正弦函数的次数(fu_n - 2)
re_add = [0] * acc_end #所有正弦函数结果的叠加
time = [] #x轴----->>>>时间
result = [] #每一个正弦函数的结果
z_dex = [] #z轴
if three_d_flg:
plt.gca(projection='3d')
plt.xlim(0, 1.2) # 设置x轴范围
plt.ylim(-1.5, 1.5) # 设置y轴范围
plt.grid(True)
for ide in range(1, fu_n, 2):
time = []
result = []
z_dex = []
for index in range(0, acc_end, 1):
te_100 = index / acc_end
time.append(te_100)
re_sin = (1 / (ide * math.pi)) * math.sin(2 * math.pi * ide * f * te_100)
re_add[index] = re_add[index] + re_sin
result.append(re_sin)
z_dex.append(ide / 10)
if all_pit_flg:
plt.plot(time, result, z_dex)
plt.plot(time, re_add)
plt.show()
放一张绘制的图片: