本文主要是介绍Python小实验:查看平台信息/处理谐波信号(面向对象),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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]*pointsself.points=pointsif formula:for p in range(points):x=p*pi*2/pointsself.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]*otherelse: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 targetdef integral(self):ans=0.0for pt in self.data:ans=ans+ptreturn ans*2*pi/self.pointsdef plot(self,title="??",pixHeight=None,maxY=None,others=[]):if not pixHeight:pixHeight=self.points*2/3pixWidth=self.pointsif not maxY:maxY=max(max(self.data),-min(self.data))offset=pixHeight/2scale=offset/maxYwin=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]*scaleif x:canvas.create_line(x-1,yprev,x,y)yprev=ydef fft(self):work=self*1for harm in range(1,10):formula="-sin(%d*x)"%harmarea=(wave(formula=formula)*work).integral()amplitude=area/-piif amplitude>.000001:print("Harmonic=",harm,"Amplitude=%.04f"%amplitude)takeAway=wave(formula="sin(%d*x)*%f"%(harm,amplitude))work=work-takeAwaydef test():p1=wave(formula="sin(x)/1")p2=wave(formula="sin(3*x)/3")p3=wave(formula="sin(5*x)/5")mys=p1+p2+p3mys.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小实验:查看平台信息/处理谐波信号(面向对象)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!