What is the most efficient way to produce an array of 100 numbers that form the shape of the triangle wave below, with a max/min amplitude of 0.5?
产生100个数字阵列的最有效方法是什么,形成下面的三角波形状,最大/最小振幅为0.5?
Triangle waveform in mind:
记住三角波形:
6 个解决方案
#1
8
Use a generator:
使用发电机:
def triangle(length, amplitude):
section = length // 4
for direction in (1, -1):
for i in range(section):
yield i * (amplitude / section) * direction
for i in range(section):
yield (amplitude - (i * (amplitude / section))) * direction
This'll work fine for a length divisible by 4, you may miss up to 3 values for other lengths.
这对于可被4整除的长度可以正常工作,对于其他长度,您可能会错过最多3个值。
>>> list(triangle(100, 0.5))
[0.0, 0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.48, 0.46, 0.44, 0.42, 0.4, 0.38, 0.36, 0.33999999999999997, 0.32, 0.3, 0.28, 0.26, 0.24, 0.21999999999999997, 0.2, 0.18, 0.15999999999999998, 0.14, 0.12, 0.09999999999999998, 0.08000000000000002, 0.06, 0.03999999999999998, 0.020000000000000018, -0.0, -0.02, -0.04, -0.06, -0.08, -0.1, -0.12, -0.14, -0.16, -0.18, -0.2, -0.22, -0.24, -0.26, -0.28, -0.3, -0.32, -0.34, -0.36, -0.38, -0.4, -0.42, -0.44, -0.46, -0.48, -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.36, -0.33999999999999997, -0.32, -0.3, -0.28, -0.26, -0.24, -0.21999999999999997, -0.2, -0.18, -0.15999999999999998, -0.14, -0.12, -0.09999999999999998, -0.08000000000000002, -0.06, -0.03999999999999998, -0.020000000000000018]
#2
5
To use numpy:
要使用numpy:
def triangle2(length, amplitude):
section = length // 4
x = np.linspace(0, amplitude, section+1)
mx = -x
return np.r_[x, x[-2::-1], mx[1:], mx[-2:0:-1]]
#3
4
The simplest way to generate a triangle wave is by using signal.sawtooth. Notice that signal.sawtooth(phi, width) accepts two arguments. The first argument is the phase, the next argument specifies the symmetry. width = 1 gives a right-sided sawtooth, width = 0 gives a left-sided sawtooth and width = 0.5 gives a symmetric triangle. Enjoy!
生成三角波的最简单方法是使用signal.sawtooth。请注意,signal.sawtooth(phi,width)接受两个参数。第一个参数是阶段,下一个参数指定对称。 width = 1给出一个右边的锯齿,width = 0给出一个左边的锯齿,width = 0.5给出一个对称的三角形。请享用!
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 500)
triangle = signal.sawtooth(2 * np.pi * 5 * t, 0.5)
plt.plot(t, triangle)
#4
2
Triangle is absolute value of sawtooth.
三角是锯齿的绝对值。
from scipy import signal
time=np.arange(0,1,0.001)
freq=3
tri=np.abs(signal.sawtooth(2 * np.pi * freq * time))
#5
1
You can use an iterator generator along with the numpy fromiter method.
您可以使用迭代器生成器和numpy fromiter方法。
import numpy
def trigen(n, amp):
y = 0
x = 0
s = amp / (n/4)
while x < n:
yield y
y += s
if abs(y) > amp:
s *= -1
x += 1
a = numpy.fromiter(trigen(100, 0.5), "d")
Now you have an array with the square wave.
现在你有一个带方波的数组。
#6
0
Here is a home-made python function for triangular signals
这是一个用于三角形信号的自制python功能
import matplotlib.pyplot as plt
import numpy as np
phase=-10
length=30 # should be positive
amplitude=10
x=np.arange(0,100,0.1)
def triang(x,phase,length,amplitude):
alpha=(amplitude)/(length/2)
return -amplitude/2+amplitude*((x-phase)%length==length/2) \
+alpha*((x-phase)%(length/2))*((x-phase)%length<=length/2) \
+(amplitude-alpha*((x-phase)%(length/2)))*((x-phase)%length>length/2)
tr=triang(x,phase,length,amplitude)
plt.plot(tr)
#1
8
Use a generator:
使用发电机:
def triangle(length, amplitude):
section = length // 4
for direction in (1, -1):
for i in range(section):
yield i * (amplitude / section) * direction
for i in range(section):
yield (amplitude - (i * (amplitude / section))) * direction
This'll work fine for a length divisible by 4, you may miss up to 3 values for other lengths.
这对于可被4整除的长度可以正常工作,对于其他长度,您可能会错过最多3个值。
>>> list(triangle(100, 0.5))
[0.0, 0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.48, 0.46, 0.44, 0.42, 0.4, 0.38, 0.36, 0.33999999999999997, 0.32, 0.3, 0.28, 0.26, 0.24, 0.21999999999999997, 0.2, 0.18, 0.15999999999999998, 0.14, 0.12, 0.09999999999999998, 0.08000000000000002, 0.06, 0.03999999999999998, 0.020000000000000018, -0.0, -0.02, -0.04, -0.06, -0.08, -0.1, -0.12, -0.14, -0.16, -0.18, -0.2, -0.22, -0.24, -0.26, -0.28, -0.3, -0.32, -0.34, -0.36, -0.38, -0.4, -0.42, -0.44, -0.46, -0.48, -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.36, -0.33999999999999997, -0.32, -0.3, -0.28, -0.26, -0.24, -0.21999999999999997, -0.2, -0.18, -0.15999999999999998, -0.14, -0.12, -0.09999999999999998, -0.08000000000000002, -0.06, -0.03999999999999998, -0.020000000000000018]
#2
5
To use numpy:
要使用numpy:
def triangle2(length, amplitude):
section = length // 4
x = np.linspace(0, amplitude, section+1)
mx = -x
return np.r_[x, x[-2::-1], mx[1:], mx[-2:0:-1]]
#3
4
The simplest way to generate a triangle wave is by using signal.sawtooth. Notice that signal.sawtooth(phi, width) accepts two arguments. The first argument is the phase, the next argument specifies the symmetry. width = 1 gives a right-sided sawtooth, width = 0 gives a left-sided sawtooth and width = 0.5 gives a symmetric triangle. Enjoy!
生成三角波的最简单方法是使用signal.sawtooth。请注意,signal.sawtooth(phi,width)接受两个参数。第一个参数是阶段,下一个参数指定对称。 width = 1给出一个右边的锯齿,width = 0给出一个左边的锯齿,width = 0.5给出一个对称的三角形。请享用!
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 500)
triangle = signal.sawtooth(2 * np.pi * 5 * t, 0.5)
plt.plot(t, triangle)
#4
2
Triangle is absolute value of sawtooth.
三角是锯齿的绝对值。
from scipy import signal
time=np.arange(0,1,0.001)
freq=3
tri=np.abs(signal.sawtooth(2 * np.pi * freq * time))
#5
1
You can use an iterator generator along with the numpy fromiter method.
您可以使用迭代器生成器和numpy fromiter方法。
import numpy
def trigen(n, amp):
y = 0
x = 0
s = amp / (n/4)
while x < n:
yield y
y += s
if abs(y) > amp:
s *= -1
x += 1
a = numpy.fromiter(trigen(100, 0.5), "d")
Now you have an array with the square wave.
现在你有一个带方波的数组。
#6
0
Here is a home-made python function for triangular signals
这是一个用于三角形信号的自制python功能
import matplotlib.pyplot as plt
import numpy as np
phase=-10
length=30 # should be positive
amplitude=10
x=np.arange(0,100,0.1)
def triang(x,phase,length,amplitude):
alpha=(amplitude)/(length/2)
return -amplitude/2+amplitude*((x-phase)%length==length/2) \
+alpha*((x-phase)%(length/2))*((x-phase)%length<=length/2) \
+(amplitude-alpha*((x-phase)%(length/2)))*((x-phase)%length>length/2)
tr=triang(x,phase,length,amplitude)
plt.plot(tr)