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


  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值