python 实现正弦波、三角波、方波、锯齿波

本文介绍了一种使用Python的Numpy库生成不同波形的方法,包括正弦波、三角波、方波和锯齿波。通过定义函数并设置周期、精度等参数,可以灵活地创建各种波形,为工程测试提供了便利。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码,本人由于工程测试需要,使用numpy的库写了下面的代码产生函数,

大家点赞,转发哦  转载请注明出处

import numpy as np
import matplotlib.pyplot as plt

def sin_wave(start,end,zhouqi,midu):
    '''

    :param start: the fist value of the wave
    :param end:  the end value of the wave
    :param zhouqi:  the zhouqi range of the wave
    :param midu:  every zhouqi, there are how many points in this zhouqi
    :return: the x array and the y array
    '''

    # 根据numpy的sin函数,生成对应的y的坐标。
    # y = np.sin(x)

    xout=[]
    yout=[]
    for i in range(start,end,zhouqi):
        x = np.arange(i, i + zhouqi,  midu)
        # y = np.where(x<start+0.5, x-start, 0)
        y = np.sin(x)

        xout = np.append(xout, x)
        yout = np.append(yout, y)
    return xout,yout





def triangle_wave(start,end,zhouqi,midu):
    '''

    :param start: the fist value of the wave
    :param end:  the end value of the wave
    :param zhouqi:  the zhouqi range of the wave
    :param midu:  every zhouqi, there are how many points in this zhouqi
    :return: the x array and the y array
    '''

    xout=[]
    yout=[]
    for i in range(start,end,zhouqi):
        x = np.arange(i, i + zhouqi,  midu)
        # y = np.where(x<start+0.5, x-start, 0)
        y = np.where(x >= i + zhouqi/2, i + zhouqi - x, x - i)

        xout = np.append(xout, x)
        yout = np.append(yout, y)
    return xout,yout



def square_wave(start,end,zhouqi,midu):
    '''

    :param start: the fist value of the wave
    :param end:  the end value of the wave
    :param zhouqi:  the zhouqi range of the wave
    :param midu:  every zhouqi, there are how many points in this zhouqi
    :return: the x array and the y array
    '''
    xout = []
    yout = []
    for i in range(start,end,zhouqi):
        x = np.arange(i, i + zhouqi,  midu)
        # y = np.where(x<start+0.5, x-start, 0)
        y = np.where(x >= i+ zhouqi/2, 1,0)

        xout = np.append(xout, x)
        yout = np.append(yout, y)
    return xout, yout



def swatooth_wave(start,end,zhouqi,midu):
    '''

    :param start: the fist value of the wave
    :param end:  the end value of the wave
    :param zhouqi:  the zhouqi range of the wave
    :param midu:  every zhouqi, there are how many points in this zhouqi
    :return: the x array and the y array
    '''
    xout = []
    yout = []
    for i in range(start,end,zhouqi):
        x = np.arange(i, i + zhouqi, midu)
        # y = np.where(x<start+0.5, x-start, 0)
        y = np.where(x >= i, i + zhouqi - x, x - i)

        xout = np.append(xout, x)
        yout = np.append(yout, y)
    return xout, yout


if __name__=='__main__':



    x,y=sin_wave(1,10,2,0.0001)
    # for  i in range()
    plt.plot(x,y)
    plt.show()
    x,y=triangle_wave(1,10,2,0.0001)
    # for  i in range()
    plt.plot(x,y)
    plt.show()
    x,y=square_wave(1,10,2,0.0001)
    # for  i in range()
    plt.plot(x,y)
    plt.show()
    x,y=swatooth_wave(1,10,2,0.0001)
    # for  i in range()
    plt.plot(x,y)
    plt.show()

 

 

将上面的代码简化一层

 

 

import numpy as np
import matplotlib.pyplot as plt

def sin_wave(start,zhouqi,midu,xdecimals,ydecimals):
    '''

    :param start: the fist value of the wave
    :param end:  the end value of the wave
    :param zhouqi:  the zhouqi range of the wave
    :param midu:  every zhouqi, there are how many points in this zhouqi
    :param xdecimals:  x decimals
    :param ydecimals: y decimals
    :return: the x array and the y array
    '''

    # 根据numpy的sin函数,生成对应的y的坐标。
    # y = np.sin(x)

    xout=[]
    yout=[]
    # for i in range(start,end,zhouqi):
    x = np.around(np.arange(start,start+zhouqi,midu),decimals=xdecimals)
    # y = np.where(x<start+0.5, x-start, 0)
    y = np.around(np.sin(x),decimals=ydecimals)

        # xout = np.append(xout, x)
        # yout = np.append(yout, y)
    return x,y





def triangle_wave(start,zhouqi,midu,xdecimals,ydecimals):
    '''

    :param start: the fist value of the wave
    :param end:  the end value of the wave
    :param zhouqi:  the zhouqi range of the wave
    :param midu:  every zhouqi, there are how many points in this zhouqi
    :return: the x array and the y array
    '''

    xout=[]
    yout=[]
    x = np.around(np.arange(start, start + zhouqi,  midu),decimals=xdecimals)
    # y = np.where(x<start+0.5, x-start, 0)
    y =np.around(np.where(x >= start + zhouqi/2, start + zhouqi - x, x - start),decimals=ydecimals)

    return x,y



def square_wave(start,zhouqi,midu,xdecimals,ydecimals):
    '''

    :param start: the fist value of the wave
    :param end:  the end value of the wave
    :param zhouqi:  the zhouqi range of the wave
    :param midu:  every zhouqi, there are how many points in this zhouqi
    :return: the x array and the y array
    '''
    xout = []
    yout = []
    x =np.around(np.arange(start, start + zhouqi,  midu),decimals=xdecimals)
    # y = np.where(x<start+0.5, x-start, 0)
    y =np.around(np.where(x >= start+ zhouqi/2, 1,0),decimals=ydecimals)

    return x, y



def swatooth_wave(start,zhouqi,midu,xdecimals,ydecimals):
    '''

    :param start: the fist value of the wave
    :param end:  the end value of the wave
    :param zhouqi:  the zhouqi range of the wave
    :param midu:  every zhouqi, there are how many points in this zhouqi
    :return: the x array and the y array
    '''
    xout = []
    yout = []
    x =np.around(np.arange(start, start + zhouqi, midu),decimals=xdecimals)
    # y = np.where(x<start+0.5, x-start, 0)
    y =np.around(np.where(x >= start, start + zhouqi - x, x - start),decimals=ydecimals)

    return x,y


if __name__=='__main__':



    x,y=sin_wave(1,10,2,0.1,xdecimals=2,ydecimals=5)
    # for  i in range()
    plt.plot(x,y)
    plt.show()
    # x,y=triangle_wave(1,10,2,0.0001)
    # # for  i in range()
    # plt.plot(x,y)
    # plt.show()
    # x,y=square_wave(1,10,2,0.0001)
    # # for  i in range()
    # plt.plot(x,y)
    # plt.show()
    # x,y=swatooth_wave(1,10,2,0.0001)
    # # for  i in range()
    # plt.plot(x,y)
    # plt.show()
    #

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没有水杯和雨伞的工科男

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值