【问题标题】:Generating encoder signal wave form using python使用python生成编码器信号波形
【发布时间】:2016-05-17 12:57:30
【问题描述】:

我正在尝试使用 python 生成以下信号以测试某些功能,有人可以帮忙吗?
我查看了波形发生器库并使用了这段代码

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

【问题讨论】:

  • 是不是在方波之上的三角波,三角函数的频率是方波频率的10倍?
  • @Morten Jensen,不完全是三角形,应该是随机噪声

标签: python noise waveform


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 2014-11-08
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    相关资源
    最近更新 更多