最近在学习python各种库之间的运用。
比如matplotlib的练习,用Python来证明方波的谐波分量。
代码如下:
def square_sigma_sin(x, n):
“”“将n个三角波函数合成为方波的近似函数”""
sum1 = 0
for a in range(1, 2*n, 2):
sum1 += 1 / a * math.sin(a * x)
return sum1
N = 200 # N个采样点
input_value = [4 * math.pi * x / N for x in range(1, N + 1)] # 输入列表,两个周期,将4pi分为N份
output_value = [square_sigma_sin(x, 9) for x in input_value] # 输出列表,将输入带入到近似函数

plt.title(“Square Function”, fontsize=24)
plt.axis([0, 4 * math.pi, -2, 2])
plt.xlabel(“input”, fontsize=16)
plt.ylabel(“output”, fontsize=16)
plt.tick_params(axis=“both”, labelsize=16)
plt.scatter(input_value, output_value, c=output_value, cmap=plt.cm.Set2, s=20)
plt.savefig(‘squares_plot.png’, bbox_inches=‘tight’)
plt.show()
Python证明方波的谐波分量

相关文章:

  • 2021-11-02
  • 2021-06-12
  • 2022-01-02
  • 2021-11-19
  • 2021-08-20
  • 2021-10-26
  • 2022-01-22
  • 2021-05-17
猜你喜欢
  • 2021-08-08
  • 2022-01-24
  • 2022-12-23
  • 2021-07-27
  • 2021-08-26
  • 2021-10-07
  • 2022-12-23
相关资源
相似解决方案