01-numpy玩轉(zhuǎn)信號處理(生成信號)
摘要:眾所周知,scipy是python信號處理的重要第三方庫,但scipy也有其缺點,兼容性似乎不友好。筆者在使用pyinstaller打包scipy函數(shù)時,無法成功。所以萌生了一個念頭:在信號處理中,使用numpy替代scipy。

00 正弦信號 np.sin(t)
幅值默認(rèn)為1:
import numpy as np
import matplotlib.pyplot as plt
fs=256 #采樣率
f=5 # 頻率,一秒內(nèi)有五對峰值峰谷
t=np.arange(0,2,1/fs)
xt=np.sin(2*np.pi*f*t)
plt.plot(t,xt)
plt.grid()

01 余弦信號 np.cos(t)
幅值默認(rèn)為1:
import numpy as np
import matplotlib.pyplot as plt
fs=256 #采樣率
f=5 # 頻率,一秒內(nèi)有五對峰值峰谷
t=np.arange(0,2,1/fs)
xt=np.cos(2*np.pi*f*t)
plt.plot(t,xt)
plt.grid()

02 三角信號 sawtooth(t,width=1)
幅值默認(rèn)為1:
import numpy as np
import matplotlib.pyplot as plt
def sawtooth(t, width=1):
t, w = np.asarray(t), np.asarray(width)
w = np.asarray(w + (t - t))
t = np.asarray(t + (w - w))
if t.dtype.char in ['fFdD']:
ytype = t.dtype.char
else:
ytype = 'd'
y = np.zeros(t.shape, ytype)
mask1 = (w > 1) | (w < 0)
np.place(y, mask1, np.nan)
tmod = np.mod(t, 2 * np.pi)
mask2 = (1 - mask1) & (tmod < w * 2 * np.pi)
tsub = np.extract(mask2, tmod)
wsub = np.extract(mask2, w)
np.place(y, mask2, tsub / (np.pi * wsub) - 1)
mask3 = (1 - mask1) & (1 - mask2)
tsub = np.extract(mask3, tmod)
wsub = np.extract(mask3, w)
np.place(y, mask3, (np.pi * (wsub + 1) - tsub) / (np.pi * (1 - wsub)))
return y
fs=256 #采樣率
f=5 # 頻率,一秒內(nèi)有五對峰值峰谷
t=np.arange(0,2,1/fs)
xt=sawtooth(2*np.pi*f*t,1)
plt.plot(t,xt)
plt.grid()

xt=sawtooth(2*np.pi*f*t,0)

xt=sawtooth(2*np.pi*f*t,0.5)

03 方波信號 square(t,duty=0.5)
幅值默認(rèn)為1:
import numpy as np
import matplotlib.pyplot as plt
def square(t, duty=0.5):
t, w = np.asarray(t), np.asarray(duty)
w = np.asarray(w + (t - t))
t = np.asarray(t + (w - w))
if t.dtype.char in ['fFdD']:
ytype = t.dtype.char
else:
ytype = 'd'
y = np.zeros(t.shape, ytype)
mask1 = (w > 1) | (w < 0)
np.place(y, mask1, np.nan)
tmod = np.mod(t, 2 * np.pi)
mask2 = (1 - mask1) & (tmod < w * 2 * np.pi)
np.place(y, mask2, 1)
mask3 = (1 - mask1) & (1 - mask2)
np.place(y, mask3, -1)
return y
fs=256 #采樣率
f=5 # 頻率,一秒內(nèi)有五對峰值峰谷
t=np.arange(0,2,1/fs)
xt=square(2*np.pi*f*t,0.5)
plt.plot(t,xt)
plt.grid()

xt=square(2*np.pi*f*t,0.25)

xt=square(2*np.pi*f*t,0.75)

xt=square(2*np.pi*f*t,1)

04 掃頻信號 chirp(t,f0,t1,f1,method='linear')
幅值默認(rèn)為1:

fs=256 #采樣率
t=np.arange(0,2,1/fs)
f0=6 #起點頻率
f1=1 #終點頻率
xt=chirp(t,f0,3,f1,method='linear')
plt.plot(t,xt)
plt.grid()

xt=chirp(t,f0,3,f1,method='quadratic')

xt=chirp(t,f0,3,f1,method='logarithmic')

05 沖擊信號 unit_impluse(shape,idx='mid')
幅值默認(rèn)為1:
import numpy as np
import matplotlib.pyplot as plt
def unit_impulse(shape, idx=None, dtype=float):
out = np.zeros(shape, dtype)
shape = np.atleast_1d(shape)
if idx is None:
idx = (0,) * len(shape)
elif idx == 'mid':
idx = tuple(shape // 2)
elif not hasattr(idx, "__iter__"):
idx = (idx,) * len(shape)
out[idx] = 1
return out
fs=256 #采樣率
t=np.arange(0,2,1/fs)
xt=unit_impulse(len(t),'mid')
plt.plot(t,xt)
plt.grid()

06 高斯調(diào)制余弦信號 gausspulse(t,fc=1000,retenv=False)

fs=256 #采樣率
t=np.arange(-1,2,1/fs)
xr,xe = gausspulse(t,2,retenv=True)
plt.plot(t,xr,color='r')
plt.plot(t,xe,color='b')
plt.grid()

工程師必備
- 項目客服
- 培訓(xùn)客服
- 平臺客服
TOP




















