Python生成正弦波信号

import numpy as np
import matplotlib.pyplot as plt
def sin_wave(A, f, fs, phi, t):
    '''
    :params A:    振幅
    :params f:    信号频率
    :params fs:   采样频率
    :params phi:  相位
    :params t:    时间长度
    '''
    # 若时间序列长度为 t=1s, 
    # 采样频率 fs=1000 Hz, 则采样时间间隔 Ts=1/fs=0.001s
    # 对于时间序列采样点个数为 n=t/Ts=1/0.001=1000, 即有1000个点,每个点间隔为 Ts
    Ts = 1/fs
    n = t / Ts
    n = np.arange(n)
    y = A*np.sin(2*np.pi*f*n*Ts + phi*(np.pi/180))
    return y
# f=50 hz
fs = 5000
hz_50 = sin_wave(A=1, f=50, fs=fs, phi=0, t=0.08)
hz_50_30 = sin_wave(A=1, f=50, fs=fs, phi=30, t=0.08)
hz_50_60 = sin_wave(A=1, f=50, fs=fs, phi=60, t=0.08)
hz_50_90 = sin_wave(A=1, f=50, fs=fs, phi=90, t=0.08)
x = np.arange(0, 0.08, 1/fs)
plt.xlabel('t/s')
plt.ylabel('y')
plt.grid()
plt.plot(x, hz_50, 'k')
plt.plot(x, hz_50_30, 'r-.')
plt.plot(x, hz_50_60, 'g--')
plt.plot(x, hz_50_90, 'b-.')
plt.legend(['phase 0', 'phase 30', 'phase 60', 'phase 90'], loc=1)

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值