**6. Implementation**

The proposed algorithm was implemented in low-power wearable hardware and tested. The hardware consists of the main board, power supply PCB, with external coil for wireless inductive charging, and a LiPo battery of capacity 110 mAh. A block diagram of the hardware is presented in Figure 8. As the processing unit, a PSoC6 microcontroller from Cypress was used. This is an ultra-low-power microcontroller with dual processor architecture: Arm Cortex M4 and M0+ cores. A BH1790GLC optical sensor from Rohm detects the PPG signal and also drives the four green 527 nm LEDs, equally placed in a circle of diameter of 6 mm, with the optical sensor placed in the center. The detector detects the light emitted by the LEDS and reflected from the patient's skin. An LSM6DSL accelerometer from STMicroelectronics is mounted on the same board as the optical sensor. The optical sensor and the accelerometer are connected to the microcontroller via an I2C bus, used for configuration and data transfer.

**Figure 8.** Block diagram of the device implementing the algorithm for measuring the heart rate.

The software for heart rate measurements runs on the Cortex M4 processing core of the PSoC6 microcontroller. The algorithm presented in this paper was written in C as two tasks running on the FreeRTOS operating system: The first task constantly reads data from the sensors, realizes filtering, and buffers data. The second task starts periodically and executes the peak detection algorithm, using data from the buffer. The calculated heart rate is sent wirelessly, using the Bluetooth Low Energy protocol. For this purpose, a built-in BLE transceiver in PSoC6 device was used. According to the BLE nomenclature, in the proposed solution, the BLE transceiver block was configured to perform a peripheral (a device constrained in resources such as energy and computing power) and server role (a device working as a data source and sending that data to the remote master device). As a data format, standard BLE Heart Rate Profile was used. The values of the measured heart rate are transmitted periodically; the user can receive the transmitted values by connecting any Bluetooth receiver compatible with Heart Rate Profile.

The PPG signal is sampled with 14-bit resolution and with frequency *fs* = 32 Hz, using a buffer of *N* = 128 ... 1024 samples, which enables data from 4 to 32 s to be analyzed, depending on the selected value of *N*, compromising accuracy versus computational cost and memory usage.

The prototype device was built and installed inside a custom-made 3D-printed case with a rubber strap, as shown in Figure 9. The size of the printed case (without the rubber strap) is 11.7 × 26 × 46 mm. The cost of the components, including the battery, was about \$60, at retail prices, for 15 pieces. The proposed implementation was capable of continuous measuring of the heart rate for more than 24 h.

**Figure 9.** The prototype implementation of a wrist sensor for heart rate measurements with the implemented TDHR algorithm: (**a**) the picture of the internal modules and parts; (**b**) the picture of the final device with strap. The main board has the dimensions of 20 × 30 mm.

#### **7. Discussion and Conclusions**

Nowadays, there are many smart watches on the market that are capable of measuring the heart rate. The details regarding the optical part of the measurement, such as the number of sensors or the wavelengths of the LEDs used during measurement, are often revealed. However, the details regarding the algorithms used are not available. Most of the publications focus on measuring the accuracy of the popular devices. In [24], the authors measured the performance of Apple's iWatch Apple Watch Sport 42 mm (first generation), during cardiopulmonary exercise test (CPET). They observed MAE from 6.34 to 7.55. It is di fficult to exactly compare this to our results, as we use di fferent and longer test conditions. In fact, the details of the PPG algorithms can only be found in the scientific publications, where the authors try to increase the accuracy of the measurement by using novel ideas and powerful techniques.

In this paper, a time-domain algorithm for the real-time detection of the heart rate was presented. The algorithm is aimed at wearable, resource-constrained devices, where battery capacity, processor speed and memory size are constrained. The algorithm processes raw data in a time-domain and requires only a few parameters. The approach is simple, the proposed algorithm has a reasonable accuracy, and it can be implemented in a typical (not DSP) microcontroller.

The algorithm consists of a two-stage input-signal-conditioning block with a limiter, the peak detection block, and period calculation block. The two-stage input conditioning block is built out of two digital bandpass filters, where the first filter has been modified to process data nonlinearly, to provide fast recovery after large signal transients. The use of band-pass filters is very simple to implement; it appeared su fficient and very e ffective for conditioning the signal; therefore, other methods such as wavelet-based baseline removal would not need to be considered.

The peak detection block enables operation at significantly lower processing and implementation costs, compared to the original AMPD algorithm. The period calculation block uses the median to calculate the heart rate based on the time di fferences between the peaks, with the use of an accelerometer to exclude the time periods a ffected by body movement.

In this study, most of the parameters were set experimentally, as we targeted on a simple and economical hardware implementation. It would be interesting to provide a method of automatic and dynamic adjustment of the parameters to further reduce the computational cost, basing on the input signal quality and the movement readouts. This will be a topic of our further research.

The proposed algorithm was evaluated in several variants, for di fferent sliding window lengths, *N*, providing the possibility to compromise the accuracy versus lower operational costs. The authors mainly used *N* = 1024, which seems to be a good compromise between the calculation cost and the accuracy. The proposed algorithm was compared to the other algorithms from the literature. The achieved accuracy is comparable to the other algorithms at smaller computational costs. The proposed solution was also implemented in a low-power wrist-wearable device.

**Author Contributions:** Conceptualization, B.P. and M.W.; methodology, M.W.; software, M.W.; validation, M.W.; formal analysis, M.W.; investigation, B.P. and M.W.; resources, M.W.; data curation, M.W.; writing—original draft preparation, M.W. and B.P.; writing—review and editing, M.W. and B.P.; visualization, M.W.; supervision, M.W.; project administration, M.W.; funding acquisition, M.W. All authors have read and agreed to the published version of the manuscript.

**Funding:** This research was partially supported by the DS Programs of Faculty of Electronics, Telecommunications and Informatics, as well as National Centre for Research and Development, Poland, project "E-Pionier—using the potential of universities to improve the innovation of ICT solutions in the public sector", No. 17/02/2018/UD.

**Acknowledgments:** The authors would like to thank Małgorzata Szczerska for her initiative in organizing research and raising funds for research.

**Conflicts of Interest:** The authors declare no conflict of interest.
