#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ ENGG1811 Lecture Week 4 Counting heart beats """ #%% Import file import matplotlib.pyplot as plt #%% Load data and convert to float # load the file which contains sampling instants # create a list of sampling time (data type: string) with open('time_data_partial.txt') as f: time_str_list = f.read().splitlines() # load the file which contains measured voltage # create a list of voltage values (data type: string) with open('voltage_data_partial.txt') as f: voltage_str_list = f.read().splitlines() # convert the elements in the list to float data type time_list = [float(t) for t in time_str_list] voltage_list = [float(v) for v in voltage_str_list] #%% Count the number tall peaks # Define a threshold threshold = 3 # Counting incoporating threshold is_peak_list = [] # Enter the code here # k is an index for the voltage_list for k in range(1,len(voltage_list)-1): voltage_before = voltage_list[k-1] voltage_current = voltage_list[k] voltage_after = voltage_list[k+1] if voltage_current > voltage_before and \ voltage_current > voltage_after: # test if it is a peak or not # if it is a peak if voltage_current > threshold: # is it a tall peak, need threshold # tall peak is_peak_list.append(1) else: # Not a peak is_peak_list.append(0) print('The number of tall peaks is:',sum(is_peak_list)) fig3 = plt.figure() # create a new figure plt.plot(time_list,voltage_list) plt.xlabel('time [s]') # label for x-axis plt.ylabel('voltage [V]') # label for y-axis plt.title('Pulse oximeter data') # title of the graph plt.grid() # display the grid plt.show() # to display the graph