如何创建两个具有完全相同功能但不同x值的正方形正弦波

时间:2021-07-02 14:58:41

I need to construct two square sine waves that describe the SAME function, BUT, they should have differing starting points on the x axis. One should start at 0 and go to 2pi, and the other from 0.05 and go to 2pi+0.05. They should both have a step size of 1. For a normal sine wave, this is easy:

我需要构造两个描述SAME函数的正方形正弦波,但是它们应该在x轴上有不同的起点。一个应该从0开始到2pi,另一个从0.05开始到2pi + 0.05。它们的步长都应为1.对于正常的正弦波,这很容易:

n = int(2*numpy.pi//0.1)

x_train = numpy.linspace(0, 2*numpy.pi, n)
x_test = numpy.linspace(0.05, 2*numpy.pi+0.05, n)

ysin_train = numpy.sin(2*x_train)
ysin_test = numpy.sin(2*x_test)

plt.plot(x_train, ysin_train)
plt.plot(x_test, ysin_test)
plt.show()

The plot will show you that the two curves are exactly the same, except for their starting points. For the square wave, it's not as obvious what to do. I've tried this:

该图将向您显示两条曲线完全相同,除了它们的起点。对于方波,它不是那么明显该做什么。我试过这个:

from scipy import signal

ysquare_train = signal.square(2*x_train)
ysquare_test = signal.square(2*x_test)

plt.plot(x_train, ysquare_train)
plt.plot(x_test, ysquare_test)

But the two curves clearly do not describe the same function, ysquare_test is 0.05 units ahead of ysquare_train. How can achieve what I want?

但是这两条曲线显然没有描述相同的函数,ysquare_test比ysquare_train提前0.05个单位。怎么能实现我想要的?

1 个解决方案

#1


1  

You suffer from discrete spacing issues. Try increasing the number of points:

您遇到离散间距问题。尝试增加点数:

n = int(2*numpy.pi//0.01)

(BTW: the int is redundant here since you using the // operator)

(顺便说一下:因为你使用//运算符,int在这里是多余的)

#1


1  

You suffer from discrete spacing issues. Try increasing the number of points:

您遇到离散间距问题。尝试增加点数:

n = int(2*numpy.pi//0.01)

(BTW: the int is redundant here since you using the // operator)

(顺便说一下:因为你使用//运算符,int在这里是多余的)