电子商务公司网站怎么建,vps如何设置网站权限,专业做婚庆的网站有哪些,5建网站使用Python处理ADC激光测距数据并绘制为图片 说明一、定义全局变量变二、保存和清空原始数据三、拆分原始数据为键值对四、获取标题、FigText、更新统计信息文件五、生成图片六、处理原始数据文件七、主函数入口八、测试结果 说明 1. 主要是将ADC激光测距叠加后的1024Byte数据绘… 使用Python处理ADC激光测距数据并绘制为图片 说明一、定义全局变量变二、保存和清空原始数据三、拆分原始数据为键值对四、获取标题、FigText、更新统计信息文件五、生成图片六、处理原始数据文件七、主函数入口八、测试结果 说明 1. 主要是将ADC激光测距叠加后的1024Byte数据绘制为图片便于直观的分析与观察一、定义全局变量变 import os
import shutilimport matplotlib.pyplot as plt# 原始数据
OrgDataDir DataHandlerDir #原始数据目录
OrgDataName COM3-115200-2023-08-03-14-22-49-650m #原始文件名称
OrgDataExtension .log #原始文件拓展名
OrgDataFullDirNameExt OrgDataDir / OrgDataName OrgDataExtension #全路径
OrgDataFilesGroup list()# 处理后数据
CreatDataFatherDir ImageCreatDir #目录
CreatDataDir OrgDataName
CreatDataName OrgDataName
CreatDataExtension .txt #统计文件扩展名
CreatDataNameExtension CreatDataName CreatDataExtension
CreatDataImageFullDir CreatDataFatherDir / CreatDataDir
CreatDataStaFullDirNameExt CreatDataFatherDir / CreatDataDir / CreatDataName CreatDataExtension# FigText序号范围
FigTextStartIndex 1024
FigTextEndIndex 1030# 峰值及索引
OrgDataPeakValue 0 # 峰值
OrgDataPeakIndex 0 # 峰值索引
OrgDataPeakValue50 0 # 峰值50%
OrgDataPeakValue60 0 # 峰值60%
OrgDataPeakIndexGap 50 # 峰值索引间隔
OrgDataPeakIndexLeft 0 # 峰值索引左值
OrgDataPeakIndexRigth 0 # 峰值索引右值AtcDataHeaderStr [0000] # 有效数据头
OrgDataValSize 1024 # ADC数据项大小
OrgDataMemory {} # 原始数据缓存
UpdateImageCount 0 # 图片计数
AxisX list() # 横坐标
AxisY list() # 纵坐标 二、保存和清空原始数据 # 保存原始数据
def Store_OrgData_KeyVal(key, val):global OrgDataMemoryOrgDataMemory[key] val# 清空原始数据缓存
def Clear_OrgData_Memory():global OrgDataMemoryglobal AxisXglobal AxisYglobal OrgDataPeakValueglobal OrgDataPeakIndexglobal OrgDataPeakValue60global OrgDataPeakValue50global OrgDataPeakIndexLeftglobal OrgDataPeakIndexRigthglobal UpdateImageCountOrgDataMemory.clear()AxisX.clear()AxisY.clear()OrgDataPeakValue 0OrgDataPeakIndex 0OrgDataPeakValue60 0OrgDataPeakValue50 0OrgDataPeakIndexLeft 0OrgDataPeakIndexRigth 0 三、拆分原始数据为键值对 # 拆分键值对
def Split_OrgData_KeyVal():global OrgDataMemoryglobal OrgDataPeakValueglobal OrgDataPeakIndexglobal OrgDataPeakValue50global OrgDataPeakValue60global OrgDataPeakIndexGapglobal OrgDataPeakIndexLeftglobal OrgDataPeakIndexRigthglobal AxisXglobal AxisYi 0peakVal 0for xKey, yVal in OrgDataMemory.items():AxisX.append(int(xKey))AxisY.append(int(yVal))# 寻找峰值及索引peakVal int(yVal)if peakVal OrgDataPeakValue:OrgDataPeakValue peakValOrgDataPeakIndex int(xKey)i 1if i OrgDataValSize:OrgDataPeakValue60 OrgDataPeakValue * 0.6 # 峰值50%OrgDataPeakValue50 OrgDataPeakValue * 0.5 # 峰值60%# 峰值左间隔OrgDataPeakIndexLeft OrgDataPeakIndex - OrgDataPeakIndexGapif OrgDataPeakIndexLeft 0:OrgDataPeakIndexLeft 0# 峰值右间隔OrgDataPeakIndexRigth OrgDataPeakIndex OrgDataPeakIndexGapif OrgDataPeakIndexRigth OrgDataValSize - 1:OrgDataPeakIndexRigth OrgDataValSize - 1return 四、获取标题、FigText、更新统计信息文件 # 获取FigText
def GetFigText():global OrgDataMemoryglobal FigTextStartIndexglobal FigTextEndIndexresStr for i in range(FigTextStartIndex, FigTextEndIndex 1):txt OrgDataMemory[str(i)]resStr txt \n# print(GetFigText:, resStr)return resStr# 获取标题
def GetTitleText():global OrgDataPeakIndexglobal OrgDataPeakIndexLeftglobal OrgDataPeakIndexRigthglobal OrgDataPeakValueresStr xCenter:%s xLeft:%s xRight:%s Peak:%s % ( \str(OrgDataPeakIndex), \str(OrgDataPeakIndexLeft), \str(OrgDataPeakIndexRigth), \str(OrgDataPeakValue))# print(TitleText:, resStr)return resStr# 获取图片名称
def Get_Image_Name():global OrgDataMemoryglobal FigTextEndIndextxtStr str(OrgDataMemory[str(FigTextEndIndex)])index txtStr.find(Gears:)if index ! -1:name txtStr[index:].strip().replace(:, ).replace( , -)else:name Gears0-Superpos0-Dist0dmpicName str(OrgDataMemory[str(FigTextEndIndex 1)]) - namereturn picName# 更新统计信息
def Update_StaInfo(staFileFullName, txt):global OrgDataPeakValuewith open(staFileFullName, a, encodingutf-8) as file:# strTxt [{}] .format(int(UpdateImageCount)) txtstrTxt [%04u] % UpdateImageCount txtresTxt strTxt Peak: str(OrgDataPeakValue) \nfile.write(resTxt)print(resTxt, end) 五、生成图片 # 生成图片
def OrgData_CreateImage(staFileDir, staFileFullName):global AxisXglobal AxisYglobal OrgDataPeakIndexglobal OrgDataPeakIndexLeftglobal OrgDataPeakIndexRigthglobal OrgDataPeakValueglobal OrgDataPeakValue50global OrgDataPeakValue60global CreatDataImageFullDirglobal UpdateImageCountSplit_OrgData_KeyVal()plt.figure(figsize(12, 8))plt.title(GetTitleText())plt.xlabel(Sampling Point)plt.ylabel(Superposition Data)plt.plot(AxisX, AxisY, ro, linewidth2)plt.axvline(OrgDataPeakIndex)plt.axvline(OrgDataPeakIndexLeft, colorgreen, linestyle--)plt.axvline(OrgDataPeakIndexRigth, colorgreen, linestyle--)plt.axhline(OrgDataPeakValue, colorred)plt.axhline(int(OrgDataPeakValue50), colorblue)plt.axhline(int(OrgDataPeakValue60), colorblue)plt.text(600, int(OrgDataPeakValue), Peak{}.format(int(OrgDataPeakValue)), fontsize16, fontweightbold, colorblack, haleft, vacenter)plt.text(300, int(OrgDataPeakValue50), 50%Peak{}.format(int(OrgDataPeakValue50)), fontsize16, fontweightbold, colorblack, haleft, vacenter)plt.text(600, int(OrgDataPeakValue60), 60%Peak{}.format(int(OrgDataPeakValue60)), fontsize16, fontweightbold, colorblack, haleft, vacenter)plt.figtext(0.15, 0.7, GetFigText(), colorblue, fontsize12, haleft, vacenter)UpdateImageCount 1picName Get_Image_Name()serial [%04u] % UpdateImageCountpicDirName staFileDir / serial picName .pngplt.savefig(picDirName, dpi300, bbox_inchestight)Update_StaInfo(staFileFullName, picName)plt.close()Clear_OrgData_Memory() 六、处理原始数据文件 # 处理原始数据文件
def File_OrgData_Handler(orgDatFullName, staFileDir, staFileFullName):global AtcDataHeaderStrglobal FigTextEndIndexwith open(orgDatFullName, r, encodingutf-8) as fileHander: # 打开文件actDataHeaderFlg 0 # 有效数据头indexCount 0 # 索引计数for lineTxet in fileHander: # 读取一行数据if actDataHeaderFlg 0: # 数据头无效if AtcDataHeaderStr in lineTxet: # 包含数据头startIndex lineTxet.find(AtcDataHeaderStr) # 数据头位置if startIndex ! -1: # 位置有效endIndex lineTxet.find(], startIndex)if endIndex ! -1:key lineTxet[startIndex 1: endIndex].strip() # 截取数据val lineTxet[endIndex 1:].strip()if key.isdigit() True and val.isdigit() True: # 都为数字Clear_OrgData_Memory() # 清空缓存Store_OrgData_KeyVal(key, val) # 保存数据actDataHeaderFlg 1 # 数据头有效indexCount 0else:indexCount indexCount 1 # 计数加1indexHeader [%04u] % indexCountif indexCount (OrgDataValSize - 1): # ADC数据0~1023if indexHeader in lineTxet: # 包含ADC数据索引start lineTxet.find(indexHeader) # 索引有效end lineTxet.find(], start)if start ! -1 and end ! -1:key lineTxet[start 1: end].strip() # 截取数据去掉前后空格val lineTxet[end 2:].strip()if key.isdigit() True and val.isdigit() True: # 都为数字Store_OrgData_KeyVal(key, val) # 保存数据continueClear_OrgData_Memory()actDataHeaderFlg 0indexCount 0else: # 其他数据1024以后start lineTxet.find(])if start ! -1: # 索引有效val lineTxet[start 1:].strip()if len(val) ! 0 and val ! : # 有内容Store_OrgData_KeyVal(str(indexCount), val) # 保存数据if val.find(ADC Meas Complete Gears:) ! -1: # 结束标准timesTick lineTxet[1: start].strip().replace( , -).replace(:, -) # 截取数据Store_OrgData_KeyVal(str(indexCount 1), timesTick) # 保存数据FigTextEndIndex indexCountactDataHeaderFlg 0indexCount 0# name os.path.splitext(os.path.basename(dirName))[0]# dir os.path.join(CreatDataFatherDir, name)if os.path.exists(staFileDir):OrgData_CreateImage(staFileDir, staFileFullName)else:indexCount indexCount - 1else:Clear_OrgData_Memory()actDataHeaderFlg 0indexCount 0七、主函数入口 # 删除目录内容
def Delete_Directory_Content(dir):if os.path.exists(dir) True: # 目录存在for item in os.listdir(dir): # 目录中内容name os.path.join(dir, item) # 拼接完整路径if os.path.isfile(name):os.remove(name) # 删除目录elif os.path.isdir(name):shutil.rmtree(name) # 删除文件# 判断基本目录
def Judg_BasicDir_Info(datDir, creatDir):res 0if not os.path.exists(datDir): # 目录不存在print(datDir, Directory No Exists, , end)os.mkdir(datDir) # 创建目录print(Create Successful......)res 1if not os.path.exists(creatDir): # 目录不存在print(creatDir, Directory No Exists, , end)os.mkdir(creatDir) # 创建目录print(Create Successful......)res 2return res# 创建图片目录和统计文件
def Create_DataImage_DirFile(dir, name):if not os.path.exists(dir): # 图片目录不存在os.mkdir(dir)else:Delete_Directory_Content(dir)dirName os.path.join(dir, name)open(dirName, a).close() # 创建统计文件print(dirName:, dirName)# 获取所有原始数据文件
def Get_OrgData_FilesGroup(dir, extName):res 1fileGroup list()fileList os.listdir(dir) # 获取目录中内容for file in fileList:if file.endswith(extName) True: # 指定后缀名fullName os.path.join(dir, file) # 拼接名称fileGroup.append(fullName) # 保存名称res 0count 0for file in fileGroup:if os.path.exists(file):count 1print([%04u] %s % (count, file))return res, fileGroup#只处理 OrgDataFullDirNameExt原始文件数据
def Function_Option_Handler1():global OrgDataFullDirNameExtglobal CreatDataImageFullDirglobal CreatDataNameExtensionif os.path.isfile(OrgDataFullDirNameExt) True: # 原始数据文件存在Create_DataImage_DirFile(CreatDataImageFullDir, CreatDataNameExtension) # 创建图片目录和统计文件成功File_OrgData_Handler(OrgDataFullDirNameExt, CreatDataImageFullDir, CreatDataStaFullDirNameExt)else:print(OrgDataFullDirNameExt, OrgDat File No Exists......)#处理OrgDataDir目录下所有原始文件数据
def Function_Option_Handler2():global OrgDataFilesGroupglobal CreatDataExtensionglobal OrgDataDirglobal OrgDataExtensionres, OrgDataFilesGroup Get_OrgData_FilesGroup(OrgDataDir, OrgDataExtension)if res 0 and len(OrgDataFilesGroup) 0:for orgFile in OrgDataFilesGroup:name os.path.splitext(os.path.basename(orgFile))[0] # 名称dir os.path.join(CreatDataFatherDir, name) # 拼接目录Create_DataImage_DirFile(dir, name CreatDataExtension)File_OrgData_Handler(orgFile, dir, os.path.join(dir, name CreatDataExtension))# 主函数入口
def main():global CreatDataFatherDirglobal OrgDataDirif Judg_BasicDir_Info(OrgDataDir, CreatDataFatherDir) 0: # 基本目录存在否Function_Option_Handler1()# Function_Option_Handler2()else:print(Basic Directory Creat Successful......)print(OrgData Handler Complete......)if __name__ __main__:main()八、测试结果 原始数据