您的位置:首页 > 编程语言 > Python开发

Python小实验:查看平台信息/处理谐波信号(面向对象)

2015-07-20 16:34 716 查看
1.Python查看硬件信息

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运行效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: