Python implements a simple function generator

Source: Internet
Author: User
Tags generator

Recently, the school has been a fan of my automation department of the bitter children's shoes left to do lesson set, almost bored to explode--with VB to implement the function generator, (language not limited)

Guys don't know where to get the MATLAB version, so dozens of people on the basis of the revision modified to muddle through, but I really do not want to use MATLAB

Write (if caught, dozens of people 0 points, that scene. ), with VB? Learn a language again. Besides, I'm not used to the way she encodes it, so

It reminds me of the python I've been playing,

#------------------------The code---First (because it's just a lesson set, it's not optimized in coding and code logic, it just implements the functionality)---------

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= ' error ', message= ' property value must be a floating-point number ')

Else

Freq = Fe1

margin = me1

deviation = De1

if __name__ = = ' __main__ ':

root = Tkinter. Tk ()

Root.title (' waveform generator ')

Root.maxsize (320,140)

Root.minsize (320,140)

LB = tkinter. Label (root,text= ' waveform selection ')

Lb.pack ()

FBT = Tkinter. Button (root,text= ' sine wave ', command=ssin)

Fbt.place (X=20,Y=20)

SBT = Tkinter. Button (root,text= ' triangle wave ', COMMAND=SSJ)

Sbt.place (X=100,Y=20)

JBT = Tkinter. Button (root,text= ' sawtooth wave ', COMMAND=JJC)

Jbt.place (X=180,Y=20)

JBT = Tkinter. Button (root,text= ' square wave ', COMMAND=FFB)

Jbt.place (X=260,Y=20)

IB = Tkinter. Label (root,text= ' Property control ')

Ib.place (X=130,Y=50)

FL = Tkinter. Label (root,text= ' frequency ')

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= ' amplitude value ')

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= ' offset ')

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= ' phase ')

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= ' emulation ', Command=simu)

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

ani = animation. Funcanimation (Fig, update, Data_gen, interval=10)

Plt.show ()

Root.mainloop ()

#-------------------------------results are as follows--------------------------------------------------------------

Python implements a simple function generator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.