python實現簡單函數發生器


最近學校又抽風把我自動化系的苦逼童鞋留下做課設,簡直無聊到爆的-->用VB實現函數發生器,(語言不限制)

大伙不知從哪搞來的MATLAB版本,於是幾十個人就在這基礎上修修改改蒙混過關了,可我實在不想用MATLAB

寫(要是被抓了,幾十個人0分,那場面->。->),用VB? 又要學一門語言。。況且我不太習慣她的編碼方式,於是

乎想起了我一直在玩的python,

# ------------------------先上代碼---(由於只是應付課設,在編碼規范和代碼邏輯上沒有做優化,只是實現功能)---------

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

import tkinter

from tkinter import messagebox

 

freq = 1

margin = 1

deviation = 0

wave = 0

 

ss = []

 

fig = plt.figure()

axes1 = fig.add_subplot(111)

line, = axes1.plot(np.linspace(-10,10,3000))

 

def fangbo(data):

  lis = []

  for i in list(data):

    if i//5%2 == 1:

      lis.append(1)

    else:

      lis.append(-1)

  return np.array(lis)

 

def juchi(data):

  lis = []

  for i in list(data):

    lis.append(i%5/2.5)

  return np.array(lis)-1

 

def sanjiao(data):

  lis = []

  for i in list(data):

    if i//5%2 == 1:

      lis.append(i%5/2.5)

    else:

      lis.append((5-i%5)/2.5)

  return np.array(lis)-1

 

def update(data):

  line.set_ydata(data)

  return line,

def data_gen():

  i = 50

  res = [0*i for i in range(3000)]

  while True:

    global freq, margin, deviation, wave, ss

    if wave == 0:

      lis = list(margin*np.sin(freq*3.1415926/5*np.linspace(1,100,3000))+deviation)

    elif wave == 1:

      lis = list(margin*juchi(freq*np.linspace(1,100,3000))+deviation)

    elif wave == 2:

      lis = list(margin*sanjiao(freq*np.linspace(1,100,3000))+deviation)

    elif wave == 3:

      lis = list(margin*fangbo(freq*np.linspace(1,100,3000))+deviation)

    ss = ss+lis[i-50:i]

    yield ss+res[len(ss):]

    i = i+50

    if len(ss) >= 3000:

      ss = ss[50:]

    if i > 3050:

      i = 50

 

def ssin():

  global wave

  wave = 0

 

def jjc():

  global wave

  wave = 1

 

def ssj():

  global wave

  wave = 2

 

def ffb():

  global wave

  wave = 3

 

def simu():

  global freq, margin, deviation

  try:

    fe1 = float(fe.get('0.0','end'))

    me1 = float(me.get('0.0','end'))

    de1 = float(de.get('0.0','end'))

    xe1 = float(xe.get('0.0','end'))

  except:

    messagebox.showinfo(title='錯誤',message='屬性值必須為浮點數')

  else:

    freq = fe1

    margin = me1

    deviation = de1

 

if __name__ == '__main__':

  root = tkinter.Tk()

  root.title('波形發生器')

  root.maxsize(320,140)

  root.minsize(320,140)

  lb = tkinter.Label(root,text='波形選擇')

  lb.pack()

  fbt = tkinter.Button(root,text='正弦波',command=ssin)

  fbt.place(x=20,y=20)

  sbt = tkinter.Button(root,text='三角波',command=ssj)

  sbt.place(x=100,y=20)

  jbt = tkinter.Button(root,text='鋸齒波',command=jjc)

  jbt.place(x=180,y=20)

  jbt = tkinter.Button(root,text='方波',command=ffb)

  jbt.place(x=260,y=20)

  ib = tkinter.Label(root,text='屬性控制')

  ib.place(x=130,y=50)

  fl = tkinter.Label(root,text='頻率')

  fl.place(x=10,y=70)

  fe = tkinter.Text(root,width=3,height=1)

  fe.insert('end','1')

  fe.place(x=50,y=70)

  ml = tkinter.Label(root,text='幅值')

  ml.place(x=85,y=70)

  me = tkinter.Text(root,width=3,height=1)

  me.insert('end','1')

  me.place(x=125,y=70)

  dl = tkinter.Label(root,text='偏移')

  dl.place(x=160,y=70)

  de = tkinter.Text(root,width=3,height=1)

  de.insert('end','0')

  de.place(x=200,y=70)

  xl = tkinter.Label(root,text='相位')

  xl.place(x=235,y=70)

  xe = tkinter.Text(root,width=3,height=1)

  xe.insert('end','0')

  xe.place(x=275,y=70)

  mb = tkinter.Button(root,text='仿真',command=simu)

  mb.place(x=130,y=95)

  ani = animation.FuncAnimation(fig, update, data_gen, interval=10)

  plt.show()

  root.mainloop()

 

# -------------------------------結果如下--------------------------------------------------------------

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM