1. 程式人生 > >單通道ECG資料包解析

單通道ECG資料包解析

print('-------------單通道ECG 資料包 解析!--------------------')
with open('C:/Users/Liu/Desktop/abcd.txt','r') as file:
    list = file.readlines()

#去掉每行的末尾'\n'
# 判斷包是否合法
for i,val in enumerate(list):
    list[i] = list[i].rstrip('\n')

    if val.startswith("81") == True:
        pass
    elif val.startswith("80") == True:
        pass
    elif val.startswith("5A") == True:
        pass
    elif val.startswith("F1") == True:
        pass
    else:
        print(">>>錯誤的包: "+val)

print("before delete list len:"+str(len(list)))

# 刪除非ECG資料
list2 = []
for i,val in enumerate(list):
    if val.startswith("81") == True or val.startswith("80") == True:
        list2.append(val)

print("after delete list len:"+str(len(list2)))

old_value = 0

# 判斷SEQ差值是否等於2
for i,val in enumerate(list2):
    sub_str= val[3:5]
    sub_int = int(sub_str,16)

    if i == 0:
        old_value = sub_int
        continue

    if sub_int >= old_value:
        diff_int = sub_int - old_value
    else:
        diff_int = 256 - old_value

    old_value = sub_int

    if diff_int ==2:
        pass
        # print (str(i)+":"+ str(diff_int))
    else:
        print (str(i)+">>>錯誤SEQ:"+ str(diff_int) + "   "+val)