Python 简单的信号处理

混合正弦信号处理

import numpy as np
import matplotlib.pyplot as plt
from numpy.fft import rfft,rfftfreq

Pi2 = 2 * np.pi
framerate = 10000

def gene_fake_signal():
    #采样率
    global framerate
    #采样时长
    duration = 0.01
    #样本数
    n = round(duration*framerate)
    #采样时间点
    ts = np.arange(n)/framerate
    #频率
    freq1 = 1900
    freq2 = 2300
    #振幅
    A1 = 1
    A2 = 2
    #相位偏移
    offset1 = 10
    offset2 = 20

    ys1 = A1 * np.cos(Pi2*freq1*ts  + offset1)
    ys2 = A2 * np.cos(Pi2*freq2*ts + offset2)
    ys = ys1 + ys2
    return ts,ys

ts,ys = gene_fake_signal()

fig = plt.figure(figsize=(6,3))
ax1 = plt.subplot(121)
ax1.plot(ts,ys,"r-")
ax1.set_title("original signal")

ax2 = plt.subplot(122)

#rfft 与 rfftfreq
hs = np.abs(rfft(ys))
fs = rfftfreq(len(ys),1/framerate)

Data = np.array([[hs[i],fs[i]] for i in range(len(hs))])
temp = Data[:,0]
index = np.lexsort((temp,))
Data = Data[index]

ax2.plot(fs,hs)
plt.savefig("0.jpg")
plt.pause(0.01)
Python 简单的信号处理_第1张图片

       [1.79449838e-13, 2.40000000e+03],
       [2.61232155e-13, 1.00000000e+03],
       [5.00000000e+01, 1.90000000e+03],
       [1.00000000e+02, 2.30000000e+03]])

随机噪声对正弦信号干扰

示例:单通道+噪声干扰

import numpy as np
import matplotlib.pyplot as plt
from numpy.fft import rfft,rfftfreq

Pi2 = 2 * np.pi
framerate = 10000

def gene_fake_signal():
    #采样率
    global framerate
    #采样时长
    duration = 0.01
    #样本数
    n = round(duration*framerate)
    #采样时间点
    ts = np.arange(n)/framerate
    #频率
    freq1 = 1900
    #振幅
    A1 = 1
    #相位偏移
    offset1 = 10

    rs = np.random.uniform(0,1,n)
    ys1 = A1 * np.cos(Pi2*freq1*ts  + offset1)
    ys = ys1 + rs 
    return ts,ys

ts,ys = gene_fake_signal()

fig = plt.figure(figsize=(6,3))
ax1 = plt.subplot(121)
ax1.plot(ts,ys,"r-")
ax1.set_title("original signal")

ax2 = plt.subplot(122)
hs = np.abs(rfft(ys))
fs = rfftfreq(len(ys),1/framerate)
Data = np.array([[hs[i],fs[i]] for i in range(len(hs))])
temp = Data[:,0]
index = np.lexsort((temp,))
Data = Data[index]

ax2.plot(fs,hs)
plt.savefig("0.jpg")
plt.pause(0.01)
Python 简单的信号处理_第2张图片

示例:多通道+噪声干扰

import numpy as np
import matplotlib.pyplot as plt
from numpy.fft import rfft,rfftfreq

Pi2 = 2 * np.pi
framerate = 10000

def gene_fake_signal():
    #采样率
    global framerate
    #采样时长
    duration = 0.01
    #样本数
    n = round(duration*framerate)
    #采样时间点
    ts = np.arange(n)/framerate
    #频率
    freq1 = 1900
    freq2 = 2000
    freq3 = 2500
    #振幅
    A1 = 1
    A2 = 2
    A3 = 3
    #相位偏移
    offset1 = 10
    offset2 = 20
    offset3 = 30
    
    rs = np.random.uniform(0,0.1,n)
    ys1 = A1 * np.cos(Pi2*freq1*ts+offset1)
    ys2 = A2 * np.cos(Pi2*freq2*ts+offset2)
    ys3 = A3 * np.cos(Pi2*freq3*ts+offset3)
    
    ys = ys1 + ys2 + ys3 + rs 
    return ts,ys

ts,ys = gene_fake_signal()

fig = plt.figure(figsize=(6,3))
ax1 = plt.subplot(121)
ax1.plot(ts,ys,"r-")
ax1.set_title("original signal")

ax2 = plt.subplot(122)

hs = np.abs(rfft(ys))
fs = rfftfreq(len(ys),1/framerate)

Data = np.array([[hs[i],fs[i]] for i in range(len(hs))])
temp = Data[:,0]
index = np.lexsort((temp,))
Data = Data[index]

ax2.plot(fs,hs)
plt.savefig("0.jpg")
plt.pause(0.01)
Python 简单的信号处理_第3张图片
       [5.65482521e-01, 3.60000000e+03],
       [5.75495030e-01, 3.90000000e+03],
       [5.48305771e+00, 0.00000000e+00],
       [4.97797723e+01, 1.90000000e+03],
       [1.00382508e+02, 2.00000000e+03],
       [1.50113033e+02, 2.50000000e+03]])

滤波器

你可能感兴趣的:(Python,numpy,信号与系统)