直接上代码,本人由于工程测试需要,使用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() #

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/83698.html