使用python生成编码器信号波形

时间:2016-05-17 12:57:31

标签: python noise waveform

我正在尝试使用python生成以下信号以测试某些功能,任何人都可以帮忙吗?  我查看了wavegenerator库并使用了这段代码

t=np.linspace(0,1,500, endpoint=False)
S1=signal.square(2*np.pi*10*t)
plt.ylim(-2,2)
plt.plot(t,S1)
plt.show()

但我不知道如何让它看起来与素描中的信号完全相同^我不知道如何指定/表征噪音的形式:

t=np.linspace(0,1,100, endpoint=False)
S1=signal.square(2*np.pi*10*t)
noise = np.random.normal(0,0.05,100)
plt.ylim(-2,2)
plt.plot(t,S1+noise)
plt.show()

Signals sketch

1 个答案:

答案 0 :(得分:1)

我找到了一个基于此网站的解决方案:https://www.embeddedrelated.com/showarticle/197.php 所以这是我使用的代码:

import matplotlib.pyplot as plt
import numpy
import scipy.integrate
from scipy import signal

t = numpy.arange(0,4,0.001)

# duty cycle on phase A and B
Da = 0.70
Db = 0.40

def extendrange(ra,rb):
    if ra is None:
        return rb
    elif rb is None:
        return ra
    else:
        return (min(ra[0],rb[0]),max(ra[1],rb[1]))

def createLimits(margin, *args):
    r = None
    for x in args:
        r = extendrange(r, (numpy.min(x),numpy.max(x)))
    rmargin = (r[1]-r[0])*margin/2.0
    return (r[0]-rmargin,r[1]+rmargin)

def showripple(centeralign=False):
    # voltage waveforms on phases A and B

    if centeralign:
        sawtooth = abs(2*(t % 1) - 1)
        Va = sawtooth < Da
        Vb = sawtooth < Db
    else:
        ramp = t % 1
        Va = ramp < Da
        Vb = ramp < Db

    # plot results
    margin = 0.1
    fig = plt.figure(figsize=(8, 6), dpi=80)
    ax = fig.add_subplot(2,1,1)
    VA=Va*0.8
    VB=Vb*0.8+1
    y = [VA, VB]
    ax.plot(t,y[0],t,y[1])
    ax.set_yticks([0.4,1.4])
    ax.set_yticklabels(['A','B'])
    ax.set_ylim(createLimits(margin,y[0],y[1]))
    ax.set_ylabel('Phase duty cycles')

    #generating noise
   noise = numpy.random.normal(0,0.009,len(VA))
    ax = fig.add_subplot(2,1,2)
    y = [VA + noise, VB]
    ax.plot(t,y[0],t,y[1])
    ax.set_yticks([0.4,1.4])
    ax.set_yticklabels(['A','B'])
    ax.set_ylim(createLimits(margin,y[0],y[1]))
    ax.set_ylabel(' Noisy Phase duty cycles')



showripple(centeralign=True)
plt.show()

Plot