python生成连续正弦波
代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pylab import mpl
class Sin():
def __init__(self):
self.Fs = 1024.0 # 采样频率
self.N = 1024 # 采样点数
self.f0 = 4.0 # 信号频率
self.pha = 0 # 初始相位
self.t = np.arange(self.N) / self.Fs # 时间向量 1*1024的矩阵
def get_data(self):
self.pha += 10
return np.sin(8 * np.pi * self.t + self.pha * np.pi / 180.0)
def show_plot(self):
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文
mpl.rcParams['axes.unicode_minus'] = False # 显示负号
plt.subplot()
plt.plot(self.t, self.get_data(), 'blue')
plt.title('正弦波', fontsize=9, color='blue')
plt.show()
if __name__ == '__main__':
Sin().show_plot()