如何在Python中生成梯形波?Python中的梯形波
我查看了诸如SciPy和NumPy等模块,但徒劳无功。是否有像scipy.signal.gaussian这样的模块返回表示高斯函数波的数值数组?
我生成此使用的Astropy梯形内核, Trapezoid1DKernel(30,斜率= 1.0) 。我想在不使用Astropy的情况下在Python中实现它。
如何在Python中生成梯形波?Python中的梯形波
我查看了诸如SciPy和NumPy等模块,但徒劳无功。是否有像scipy.signal.gaussian这样的模块返回表示高斯函数波的数值数组?
我生成此使用的Astropy梯形内核, Trapezoid1DKernel(30,斜率= 1.0) 。我想在不使用Astropy的情况下在Python中实现它。
虽然宽度和斜率足以限定的三角形波信号,就需要为梯形信号的第三参数:幅值。
使用这三个参数,您可以轻松调整scipy.signal.sawtooth
函数,通过截断和偏移三角形函数来为您提供梯形形状。
from scipy import signal
import matplotlib.pyplot as plt
import numpy as np
def trapzoid_signal(t, width=2., slope=1., amp=1., offs=0):
a = slope*width*signal.sawtooth(2*np.pi*t/width, width=0.5)/4.
a[a>amp/2.] = amp/2.
a[a<-amp/2.] = -amp/2.
return a + amp/2. + offs
t = np.linspace(0, 6, 501)
plt.plot(t,trapzoid_signal(t, width=2, slope=2, amp=1.), label="width=2, slope=2, amp=1")
plt.plot(t,trapzoid_signal(t, width=4, slope=1, amp=0.6), label="width=4, slope=1, amp=0.6")
plt.legend(loc=(0.25,1.015))
plt.show()
注意,你可能也想定义一个阶段,depeding于使用情况。
为了定义单个脉冲,您可能需要修改该功能并提供一个范围超过[0,width]
的数组。
from scipy import signal
import matplotlib.pyplot as plt
import numpy as np
def trapzoid_signal(t, width=2., slope=1., amp=1., offs=0):
a = slope*width*signal.sawtooth(2*np.pi*t/width, width=0.5)/4.
a += slope*width/4.
a[a>amp] = amp
return a + offs
for w,s,a in zip([2,5], [2,1], [1,0.6]):
t = np.linspace(0, w, 501)
l = "width={}, slope={}, amp={}".format(w,s,a)
plt.plot(t,trapzoid_signal(t, width=w, slope=s, amp=a), label=l)
plt.legend(loc="upper right")
plt.show()
这看起来很方便。你可以告诉我如何产生一个特定宽度的单脉冲。我想实现我在问题中附加的图像。谢谢 –
我更新了答案。 – ImportanceOfBeingErnest
非常感谢。做了一些编辑并最终完成了。 –
从SciPy website它看起来像这不包括(他们目前有sawtooth
和square
,但不是梯形)。作为the C例程的通用版本,下面将做你想做的,
import numpy as np
import matplotlib.pyplot as plt
def trapezoidalWave(xin, width=1., slope=1.):
x = xin%(4*width)
if (x <= width):
# Ascending line
return x*slope;
elif (x <= 2.*width):
# Top horizontal line
return width*slope
elif (x <= 3.*width):
# Descending line
return 3.*width*slope - x*slope
elif (x <= 4*width):
# Bottom horizontal line
return 0.
x = np.linspace(0.,20,1000)
for i in x:
plt.plot(i, trapezoidalWave(i), 'k.')
plt.plot(i, trapezoidalWave(i, 1.5, 2.), 'r.')
plt.show()
它看起来像,
这可以更优雅与亥功能,使用户使用来完成NumPy阵列,
import numpy as np
import matplotlib.pyplot as plt
def H(x):
return 0.5 * (np.sign(x) + 1)
def trapWave(xin, width=1., slope=1.):
x = xin%(4*width)
y = ((H(x)-H(x-width))*x*slope +
(H(x-width)-H(x-2.*width))*width*slope +
(H(x-2.*width)-H(x-3.*width))*(3.*width*slope - x*slope))
return y
x = np.linspace(0.,20,1000)
plt.plot(x, trapWave(x))
plt.plot(x, trapWave(x, 1.5, 2.))
plt.show()
对于这个例子,Heaviside版本快了20倍!
下面的例子显示了如何做到这一点,以获得积分和显示范围。
方程基于答复:Equation for trapezoidal wave equation
import math
import numpy as np
import matplotlib.pyplot as plt
def get_wave_point(x, a, m, l, c):
# Equation from: https://stackoverflow.com/questions/11041498/equation-for-trapezoidal-wave-equation
# a/pi(arcsin(sin((pi/m)x+l))+arccos(cos((pi/m)x+l)))-a/2+c
# a is the amplitude
# m is the period
# l is the horizontal transition
# c is the vertical transition
point = a/math.pi*(math.asin(math.sin((math.pi/m)*x+l))+math.acos(math.cos((math.pi/m)*x+l)))-a/2+c
return point
print('Testing wave')
x = np.linspace(0., 10, 1000)
listofpoints = []
for i in x:
plt.plot(i, get_wave_point(i, 5, 2, 50, 20), 'k.')
listofpoints.append(get_wave_point(i, 5, 2, 50, 20))
print('List of points : {} '.format(listofpoints))
plt.show()
全归功于@ImportanceOfBeingErnest。我只是修改了他刚刚创建我的一天的代码。
from scipy import signal
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
def trapzoid_signal(t, width=2., slope=1., amp=1., offs=0):
a = slope*width*signal.sawtooth(2*np.pi*t/width, width=0.5)/4.
a += slope*width/4.
a[a>amp] = amp
return a + offs
for w,s,a in zip([32],[1],[0.0322]):
t = np.linspace(0, w, 34)
plt.plot(t,trapzoid_signal(t, width=w, slope=s, amp=a))
plt.show()
贵trapezodial波有哪些参数? –
只是宽度和坡度。 –