Python小实验:查看平台信息/处理谐波信号(面向对象)
2015-07-20 16:34
716 查看
1.Python查看硬件信息
Python属于上层语言,很少用于直接操作底层硬件,但是并不代表不可以可硬件搭配实现嵌入式功能。比如Python语言在FPGA上实现定点平方根运算,取代传统的Verilog和VHDL语言进行硬件设计。以下用python语言查看当前系统和配置信息。
2.Python处理谐波信号和信号变换
Python可用于处理数字信号方面的应用,可生成任意所需的波形函数。采用面向对象的方法编写一个wave类。
在wave.py中定义wave类:
测试用例1运行效果:
测试2:
测试用例2运行效果:
测试3:
测试用例3运行效果:
Python属于上层语言,很少用于直接操作底层硬件,但是并不代表不可以可硬件搭配实现嵌入式功能。比如Python语言在FPGA上实现定点平方根运算,取代传统的Verilog和VHDL语言进行硬件设计。以下用python语言查看当前系统和配置信息。
import platform def checkPlatformInfo(): uname=platform.uname() print("uname=",uname) arch=platform.architecture() print("arch=",arch) machine=platform.machine() print("machine=",machine) node=platform.node() print("node=",node) platformInfo=platform.platform() print("platformInfo=",platformInfo) processor=platform.processor() print("processor=",processor) system=platform.system() print("system=",system) version=platform.version() print("version=",version) if __name__=="__main__": checkPlatformInfo();运行效果:
2.Python处理谐波信号和信号变换
Python可用于处理数字信号方面的应用,可生成任意所需的波形函数。采用面向对象的方法编写一个wave类。
在wave.py中定义wave类:
import sys from tkinter import * from math import * #wave类 class wave: #初始化类 def __init__(self,points=400,formula=None): self.data=[0.0]*points self.points=points if formula: for p in range(points): x=p*pi*2/points self.data[p]=eval(formula) #与其它波相加 def __add__(self,other): target=wave(points=self.points) for i in range(self.points): target.data[i]=self.data[i]+other.data[i] return target #与其它波相乘 def __mul__(self,other): target=wave(points=self.points) if type(other) == type(5) or type(other)==type(5.0): for i in range(self.points): target.data[i]=self.data[i]*other else: for i in range(self.points): target.data[i]=self.data[i]*other.data[i] return target #与其它波相减 def __sub__(self,other): target=wave(points=self.points) for i in range(self.points): target.data[i]=self.data[i]-other.data[i] return target def integral(self): ans=0.0 for pt in self.data: ans=ans+pt return ans*2*pi/self.points def plot(self,title="??",pixHeight=None,maxY=None,others=[]): if not pixHeight: pixHeight=self.points*2/3 pixWidth=self.points if not maxY: maxY=max(max(self.data),-min(self.data)) offset=pixHeight/2 scale=offset/maxY win=Tk() win.title(title) canvas=Canvas(win,width=pixWidth,height=pixHeight) #画0值线 canvas.create_line(0,offset,pixWidth,offset) canvas.pack() self.plotOne(canvas,pixWidth,scale,offset) for i in range(len(others)): others[i].plotOne(canvas,pixWidth,scale,offset) if sys.platform=="win32": win.mainloop() def plotOne(self,canvas,pixWidth,scale,offset): for x in range(pixWidth): y=offset-self.data[x]*scale if x: canvas.create_line(x-1,yprev,x,y) yprev=y def fft(self): work=self*1 for harm in range(1,10): formula="-sin(%d*x)"%harm area=(wave(formula=formula)*work).integral() amplitude=area/-pi if amplitude>.000001: print("Harmonic=",harm,"Amplitude=%.04f"%amplitude) takeAway=wave(formula="sin(%d*x)*%f"%(harm,amplitude)) work=work-takeAway def test(): p1=wave(formula="sin(x)/1") p2=wave(formula="sin(3*x)/3") p3=wave(formula="sin(5*x)/5") mys=p1+p2+p3 mys.fft() if __name__=="__main__": test();测试1:
import wave a=wave.wave(formula="sin(x)") b=wave.wave(formula=".5*sin(2*x)") a.plot(maxY=1.2,pixHeight=200,title="Sin(x) and .5*sin(2*x)",others=[b])
测试用例1运行效果:
测试2:
import wave a=wave.wave(formula="sin(x)") b=wave.wave(formula=".5*sin(2*x)") c=a+b c.plot(maxY=1.5,pixHeight=200,title="Sin(x)+.5*sin(2*x)")
测试用例2运行效果:
测试3:
import wave a=wave.wave(formula="sin(x)") b=wave.wave(formula=".5*sin(2*x)") c=a*b c.plot(maxY=1.5,pixHeight=200,title="Sin(x)*.5*sin(2*x)")
测试用例3运行效果:
相关文章推荐
- python中的os.path.dirname(__file__)的使用
- Python Static Method
- 在Python的Django框架中编写编译函数
- python 读取配置文件
- python访问MySQL数据库
- python的私有化
- python之发送邮件
- python特殊函数之lambda和map
- python进阶四:类的继承
- Python的Django框架中自定义模版标签的示例
- 关于Python在函数中接收元组和列表的理解
- 【笨办法学Python】习题11:打印出改变了的输入
- python报错:IndentationError: unexpected indent
- Python日志之Python控制流
- Python日志之Python控制流(顺序,分支,循环)
- Python中默认参数self的理解
- python operator.itemgetter函数 sorted函数
- Python学习笔记——Unicode
- 你可能没听过的11个Python库
- Python 一些值得注意的地方