绘制离散分段函数 - 信号
Plotting discrete piecewise function - signal
我需要绘制一个由分段函数定义的离散信号:
if n < 0 , x = (135/56) * (0.9)^n - (1/8)(0.1)^n - (2/7)(0.2)^n
if 0<=n< 4, x = (135/56) * (0.9)^n + (7/8)(0.1)^n + (5/7)(0.2)^n
if n>=4 , x = (135/56) * (0.9)^n + (7/8)(0.1)^n + (5/7)(0.2)^n + (0.1)^(-4) + (0.2)^(-4)
我在网络上进行了很多搜索,尤其是在这里,我想出了这段代码,经过多次更正,它实际上在 spyder 中运行。但结果绝对不是预期的结果。谁能帮帮我?
import numpy as np
import matplotlib.pyplot as plt
xPoints = []
nPoints = []
q = (135 / 56)
z= -(1/8)
r = -(2/7)
m = 7/8
p = 5 /7
j = np.power(0.1, -3.5)
a = np.power(0.2, -3.5)
for n in range(-5,11):
if n<0 :
x = q *np.power(0.9, n) + z* np.power(0.1, n) + r* np.power(0.2, n)
elif (n>=0 and n<4):
x =q *np.power(0.9, n) + m* np.power(0.1, n) + p* np.power(0.2, n)
else:
x =q *np.power(0.9, n) + m* np.power(0.1, n) + p* np.power(0.2, n)+ j + a
xPoints.append(x)
nPoints.append(n)
plt.plot(nPoints, xPoints)
plt.plot.show()
在 numpy 中,可以使用 where
. One of numpy's most magical features is broadcasting 创建逐步函数,其中可以一次对完整的值数组调用函数。
您的示例代码创建了预期的曲线,但仅在整数值处添加了一个点。要创建平滑曲线,np.linspace
creates a long array of values (the code below uses 1000 little steps between -5 and 5). (Note that numpy needs the &
operator 用于两个数组表达式的逻辑 and
。在这种特殊情况下,您可以使用 n < 4
而不是 (n >= 0) & (n < 4)
,因为 n < 0
的情况已在前面得到处理。)
import numpy as np
import matplotlib.pyplot as plt
q = (135 / 56)
z = -(1 / 8)
r = -(2 / 7)
m = 7 / 8
p = 5 / 7
j = np.power(0.1, -3.5)
a = np.power(0.2, -3.5)
n = np.linspace(-5, 5, 1000)
x = np.where(n < 0, q * np.power(0.9, n) + z * np.power(0.1, n) + r * np.power(0.2, n),
np.where((n >= 0) & (n < 4), q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n),
q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n) + j + a))
plt.plot(n, x)
plt.show()
如果你只想要整数位置,你可以使用np.arange
instead of np.linspace
and then create a scatter plot (plt.scatter(n, x)
) or maybe a stemplot:
import numpy as np
import matplotlib.pyplot as plt
q = (135 / 56)
z = -(1 / 8)
r = -(2 / 7)
m = 7 / 8
p = 5 / 7
j = np.power(0.1, -3.5)
a = np.power(0.2, -3.5)
n = np.arange(-5, 6)
x = np.where(n < 0, q * np.power(0.9, n) + z * np.power(0.1, n) + r * np.power(0.2, n),
np.where((n >= 0) & (n < 4), q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n),
q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n) + j + a))
plt.stem(n, x)
plt.show()
我需要绘制一个由分段函数定义的离散信号:
if n < 0 , x = (135/56) * (0.9)^n - (1/8)(0.1)^n - (2/7)(0.2)^n
if 0<=n< 4, x = (135/56) * (0.9)^n + (7/8)(0.1)^n + (5/7)(0.2)^n
if n>=4 , x = (135/56) * (0.9)^n + (7/8)(0.1)^n + (5/7)(0.2)^n + (0.1)^(-4) + (0.2)^(-4)
我在网络上进行了很多搜索,尤其是在这里,我想出了这段代码,经过多次更正,它实际上在 spyder 中运行。但结果绝对不是预期的结果。谁能帮帮我?
import numpy as np
import matplotlib.pyplot as plt
xPoints = []
nPoints = []
q = (135 / 56)
z= -(1/8)
r = -(2/7)
m = 7/8
p = 5 /7
j = np.power(0.1, -3.5)
a = np.power(0.2, -3.5)
for n in range(-5,11):
if n<0 :
x = q *np.power(0.9, n) + z* np.power(0.1, n) + r* np.power(0.2, n)
elif (n>=0 and n<4):
x =q *np.power(0.9, n) + m* np.power(0.1, n) + p* np.power(0.2, n)
else:
x =q *np.power(0.9, n) + m* np.power(0.1, n) + p* np.power(0.2, n)+ j + a
xPoints.append(x)
nPoints.append(n)
plt.plot(nPoints, xPoints)
plt.plot.show()
在 numpy 中,可以使用 where
. One of numpy's most magical features is broadcasting 创建逐步函数,其中可以一次对完整的值数组调用函数。
您的示例代码创建了预期的曲线,但仅在整数值处添加了一个点。要创建平滑曲线,np.linspace
creates a long array of values (the code below uses 1000 little steps between -5 and 5). (Note that numpy needs the &
operator 用于两个数组表达式的逻辑 and
。在这种特殊情况下,您可以使用 n < 4
而不是 (n >= 0) & (n < 4)
,因为 n < 0
的情况已在前面得到处理。)
import numpy as np
import matplotlib.pyplot as plt
q = (135 / 56)
z = -(1 / 8)
r = -(2 / 7)
m = 7 / 8
p = 5 / 7
j = np.power(0.1, -3.5)
a = np.power(0.2, -3.5)
n = np.linspace(-5, 5, 1000)
x = np.where(n < 0, q * np.power(0.9, n) + z * np.power(0.1, n) + r * np.power(0.2, n),
np.where((n >= 0) & (n < 4), q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n),
q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n) + j + a))
plt.plot(n, x)
plt.show()
如果你只想要整数位置,你可以使用np.arange
instead of np.linspace
and then create a scatter plot (plt.scatter(n, x)
) or maybe a stemplot:
import numpy as np
import matplotlib.pyplot as plt
q = (135 / 56)
z = -(1 / 8)
r = -(2 / 7)
m = 7 / 8
p = 5 / 7
j = np.power(0.1, -3.5)
a = np.power(0.2, -3.5)
n = np.arange(-5, 6)
x = np.where(n < 0, q * np.power(0.9, n) + z * np.power(0.1, n) + r * np.power(0.2, n),
np.where((n >= 0) & (n < 4), q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n),
q * np.power(0.9, n) + m * np.power(0.1, n) + p * np.power(0.2, n) + j + a))
plt.stem(n, x)
plt.show()