数字信号在表达时需要标明0位置,采用一个嵌套列表来表示。
[zero_in_signal,[0,1,2,3,4,5,4,2,1]]
为了显示我们的波形,还需要一个画图的函数
import numpy as np
from matplotlib import pyplot as plt
def show(res,down,up):
lis = res[1]
n = np.arange(down,up+1)
plt.stem(n,lis)
x0 = res[0]
plt.plot(x0,0)
plt.show()
单位脉冲序列
def impseq(n0,down,up):
# if ((n0 < down) or (n0 > up) or (down > up)):
# print("err: should be down<=n0<=up")
# return
lis = np.zeros(up-down+1)
lis[n0-down] = 1
a = [0-down,lis]
return a
tes1 = impseq(4,-2,6)
show(tes1)
使用numpy生成一个都是0的序列,在把0位置的数赋1即可
单位跃阶序列
def stepseq(n0,down,up):
# if ((n0 < down) or (n0 > up) or (down > up)):
# print("err: should be down<=n0<=up")
# return
lis = np.zeros(up-down+1)
lis[(n0-down):] = 1
a = [0-down,lis]
return a
tes2 = stepseq(2,-2,5)
矩形序列
def RN(N,down,up):
lis = np.zeros(up-down+1)
lis[0-down:N-down] = 1
a = [0-down,lis]
return a
tes3 = RN(5,-3,8)
实指数序列
def expN(a,down,up):
lis = np.arange(down,up+1)
np.power(a,lis)
a = [0-down,lis]
return a
tes4 = expN(1/4,-2,2)
复指数序列
def complexN(alph,omig,down,up):
t=np.arange(down,up+1,1)
plt.ylim(0,4)
f=2*np.exp((complex(alph,omig))*t)
plt.subplot(1,2,1)
plt.title('real')
n = np.arange(down,up+1)
plt.stem(n,np.real(f))
plt.subplot(1,2,2)
plt.title('img')
n = np.arange(down,up+1)
plt.stem(n,np.imag(f))
plt.show()
return f
complexN(0.2,-1.2,-4,4)
由于复指数有俩图,因此画图直接在内部实现了
三角序列
def sin_cosN(sin_w0,sin_sigma,sin_A,cos_w0,cos_sigma,cos_A,down,up):
lis = np.arange(down,up+1)
liss = sin_A*np.sin(sin_w0*lis*np.pi+sin_sigma)+cos_A*np.cos(cos_w0*lis*np.pi+cos_sigma)
a = [0-down,liss]
return a
tes5 = sin_cosN(0.2,-3/4,2,0.6,0.5,1.2,0,9)
序列求和
def culculate_sum(x1,x2):
x1_lef,x1_rig,x2_lef,x2_rig = 0,0,0,0
lef_max = 0
#找下最大值
if x1[0]>=0:
x1_lef = x1[0]
x1_rig = len(x1[1])-x1[0]-1
else:
x1_lef = x1[0]+len(x1[1])
x1_rig = -x1[0]-1
if x2[0]>=0:
x2_lef = x2[0]
x2_rig = len(x2[1])-x2[0]-1
else:
x2_lef = x2[0]+len(x2[1])
x2_rig = -x2[0]-1
#补齐
if x1_lef>x2_lef:
for i in range(x1_lef):
x2[1].insert(0,0)
lef_max=x1_lef
elif x1_lef<x2_lef:
for i in range(x2_lef):
x1[1].insert(0,0)
lef_max = x2_lef
if x1_rig>x2_rig:
for i in range(x1_rig):
x2[1].append(0)
elif x1_rig<x2_rig:
for i in range(x2_rig):
x1[1].append(0)
x1[0] = lef_max
x2[0] = lef_max
print(x1)
print(x2)
#相加
res = [lef_max,[]]
for i in range(len(x1[1])):
res[1].append(x1[1][i]+x2[1][i])
print(res)
return res