Inertial Measurement Unit-Based Real-Time Adaptive Algorithm for Human Walking Pattern and Gait Event Detection

: In this work, a lightweight adaptive hybrid gait detection method with two inertial measurement units (IMUs) on the foot and thigh was developed and preliminarily evaluated. An adaptive detection algorithm is used to eliminate the pre-training phase and to modify parameters according to the changes within a walking trial using an adaptive two-level architecture. The present algorithm has a two-layer structure: a real-time detection algorithm for detecting the current gait pattern and events at 100 Hz., and a short-time online training layer for updating the parameters of gait models for each gait pattern. Three typical walking patterns, including level-ground walking (LGW), stair ascent (SA), and stair descent (SD), and four events/sub-phases of each pattern, can be detected on a portable Raspberry-Pi platform with two IMUs on the thigh and foot in real-time. A preliminary algorithm test was implemented with healthy subjects in common indoor corridors and stairs. The results showed that the on-board model training and event decoding processes took 20 ms and 1 ms, respectively. Motion detection accuracy was 97.8% for LGW, 95.6% for SA, and 97.1% for SD. F 1 -scores for event detection were over 0.86, and the maximum time delay was steadily below 51 ± 32.4 ms. Some of the events in gait models of SA and SD seemed to be correlated with knee extension and ﬂexion. Given the simple and convenient hardware requirements, this method is suitable for knee assistive device applications.


Introduction
Intelligent walking assistive devices (IWADs) are increasingly being used to restore or assist the gait of patients with impaired lower extremity joint function or the elderly.These devices use a variety of sensors to detect the locomotor intentions of these populations, such as level-ground walking (LGW), stair ascent (SA), and stair descent (SD), to determine the best time instants to impose or remove assistive torque on the impaired biological joints [1,2], which improves human-machine interaction, increases wearing comfort, and reduces safety risks.Therefore, walking pattern and gait-event detection technologies that can perform real-time and long-term detection of gait states are crucial for clinical analysis and the proper functioning of the IWADs.
Optical tracking methods with the characteristics of non-contact and high precision have been widely used in gait segmentation and analysis [3,4].However, since methods of this kind generally depend on well-calibrated hardware with a limited field of view, it is difficult to apply in daily outdoor IWAD applications.By contrast, wearable and onboard sensing solutions seem more suitable for IWAD applications.Among them, surface electromyography-based (sEMG) strategies are considered to have the characteristic of good accuracy [5,6].However, sEMG sensors are not suitable for long-time use due to their complexity and poor interference immunity [7].Force-based sensors, such as forcesensitive resistors (FSRs), are another type of popular technology for gait event detection [8].Nevertheless, due to the lack of helpful kinematics information, it is hard to estimate or predict the gait events once in the swing phase [9].
In recent years, lightweight and integrable inertial measurement units (IMUs) have been proposed to overcome the above limitations and are widely used for real-time kinematic sensing and pattern recognition in IWAD control [10][11][12].Some high-speed and low-cost rule-based detection algorithms with inertial signals from IMUs have been developed for IWAD control and clinical analysis [13,14].With the developments of hardware and algorithm improvements in the past two decades, machine learning algorithms have also been introduced to the fields of IWAD control [15][16][17][18] and abnormal gait segmentation in clinical analysis [19][20][21][22].However, most of these studies achieve online gait detection by using an offline-trained model.Lit [1] implied that offline-trained models are sensitive to changes in gait parameters and even tend to lose validity when the locomotion conditions change dramatically.To increase the dependability of gait event detection, a few unsupervised adaptive algorithms for only LGW patterns have been presented [1,23].However, machine learning-based research on a real-time on-board adaptive gait event detection algorithm for multiple common walking patterns, including SA and SD, has not been reported.
In this study, our aim is twofold: (1) to establish methods for labeling three typical walking patterns (LGW, SA, and SD) and gait events for different walking patterns, and (2) to eliminate the cost of manual data acquisition and model training with a single portable microcontroller unit (MCU) to accomplish the real-time adaptive identification of walking patterns and gait events.Therefore, an IMU-mounted portable wearable system with a lightweight hybrid algorithm was developed to detect three patterns and four events of each pattern by using inertial sensing data from two IMUs on the thigh and foot in real-time.Using a short-term model training strategy, the training-level algorithm can automatically modify the model parameters according to the current gait pattern and enhance the adaptivity of detection models.Results obtained in this feasibility study demonstrated that the hybrid algorithm-based approach can detect gait patterns and events continuously with three models for each pattern in a low computational burden on a simple and convenient hardware frame.

IMU-Based Hardware Development and Data Pretreatment
The IMU-based brace-type device (gait detection device) is developed (shown in Figure 1), which is considered a replica of an IWAD targeting the knee joint.It contains two single-chip microcomputer-controlled IMU modules (STM32F104, STMicroelectronics, Geneva, Switzerland; MPU9250, InvenSense, San Jose, CA, USA), four FSRs (FSR UX 406, Interlink Electronic, Camarillo, CA, USA), one left leg knee brace (Xeleton, Ottobock, Duderstadt, Germany), a calibrated magnetic position sensor (AS5055A, ams-OSRAM, Premstätten, Austria), and one portable Raspberry-Pi (Raspberry-Pi 4B, Raspberry Pi Foundation, Cambridge, UK).Two IMU modules are fixed in the front of the thigh part of the brace and on the instep of the shoe, respectively.All the x-axes of IMU modules point to the outside of limbs from the medial, parallelly with the rotation axis of the knee brace.Then set all y-axes of them to point to the proximal joint of limbs from the distal joint.The magnetic position sensor is fixed to the knee joint rotation axis and connected to the thigh IMU module.Each printed circuit board (PCB) with sensor chips is held in a 3D-printed customized installation bottom case through screw fastening, respectively.Then all the installation bottom cases are glued to the frame of the knee brace and shoe with planned orientations by hot melt adhesive, respectively.In addition, the magnet ring for position sensing is glued to the thigh part of the knee brace, so the position sensor chip on the PCB mounted on the thigh part of the brace can monitor the angle of the knee joint.
To ensure the stability of signal transmission, the MCU and IMU modules are connected by shielded twisted-pair cables.All the cables are fixed to the brace and shoe at several fixing points along the path of the brace, the wearer's ankle, and the rear side of the shoe with tapes/strapping and hot melt adhesive.Four FSRs are attached to the bottom of the shoe just under the heel and metatarsal pads through double-faced adhesive tapes with a silicone pad for protection (shown in Figure 1a), and they are all wire-connected to the foot IMU module.With enough redundant cables and additional gluing reinforcements on each cable connector, the effect of cable traction due to repetitive motion that may impair the repeatability of sensors is minimized as much as possible.Four input signals are used in this method: angular velocity and angle of the x-axis of the thigh IMU, acceleration of the z-axis of the foot IMU, and angular velocity of the xaxis of the foot IMU.Since only data from the x-axis of the thigh IMU is used, the different coordinates of the thigh and foot IMUs do not affect the detection.The rotation angle of the magnetic position sensor and pressure markers of FSRs are also collected and only used as reference standards.The two modules update sensing data to a buffer queue in the MCU through the controller area network (CAN) bus at 200 Hz.The buffer contains the last five data points of each signal and is filtered every 10 ms to synchronize and smooth the data by an average filtering algorithm.Then these processed data that update at 100 Hz are delivered to the walking pattern detection algorithm, the real-time gait To ensure the stability of signal transmission, the MCU and IMU modules are connected by shielded twisted-pair cables.All the cables are fixed to the brace and shoe at several fixing points along the path of the brace, the wearer's ankle, and the rear side of the shoe with tapes/strapping and hot melt adhesive.Four FSRs are attached to the bottom of the shoe just under the heel and metatarsal pads through double-faced adhesive tapes with a silicone pad for protection (shown in Figure 1a), and they are all wire-connected to the foot IMU module.With enough redundant cables and additional gluing reinforcements on each cable connector, the effect of cable traction due to repetitive motion that may impair the repeatability of sensors is minimized as much as possible.
Four input signals are used in this method: angular velocity and angle of the x-axis of the thigh IMU, acceleration of the z-axis of the foot IMU, and angular velocity of the x-axis of the foot IMU.Since only data from the x-axis of the thigh IMU is used, the different coordinates of the thigh and foot IMUs do not affect the detection.The rotation angle of the magnetic position sensor and pressure markers of FSRs are also collected and only used as reference standards.The two modules update sensing data to a buffer queue in the MCU through the controller area network (CAN) bus at 200 Hz.The buffer contains the last five data points of each signal and is filtered every 10 ms to synchronize and smooth the data by an average filtering algorithm.Then these processed data that update at 100 Hz are delivered to the walking pattern detection algorithm, the real-time gait event/sub-phase decoding algorithm, and a training buffer queue for further event feature labeling and model training.

Gait Models Construction for Three Typical Walking Patterns
Gaits are considered consistent and quasi-periodic cyclic processes that describe the pattern of biped motion [24].In this work, gaits are regarded as cycle sequences containing four events and four sub-phases for each pattern (shown in Figure 2).Four events are defined, namely: (1) initial contact (IC), (2) foot flat (FF), (3) heel-off (HO), and (4) final contact (FC).In LGW, heel-off is the start event of knee flexion, and initial contact is the end event of knee extension.In SA, initial contact is the start event of knee extension, and final contact is the start event of knee flexion.In SD, initial contact is the start event of knee flexion, and final contact is the start event of knee extension.

Gait Models Construction for Three Typical Walking Patterns
Gaits are considered consistent and quasi-periodic cyclic processes that describe the pattern of biped motion [24].In this work, gaits are regarded as cycle sequences containing four events and four sub-phases for each pattern (shown in Figure 2).Four events are defined, namely: (1) initial contact (IC), (2) foot flat (FF), (3) heel-off (HO), and (4) final contact (FC).In LGW, heel-off is the start event of knee flexion, and initial contact is the end event of knee extension.In SA, initial contact is the start event of knee extension, and final contact is the start event of knee flexion.In SD, initial contact is the start event of knee flexion, and final contact is the start event of knee extension.Then, four sub-phases can be defined by each adjacent pair of the above-mentioned events, namely: (1) load response (LR), (2) mid-stance (MSt), (3) terminal stance (TSt), and (4) swing (Sw).For model consistency, all sub-phases continue until the next adjacent event occurs.

Adaptive Gait Detection Method
To enhance the adaptability of the detection algorithm to deal with parameter variations of gait, especially variations of walking speed and limb quasi-periodic behavior, a two-level algorithm was developed: (1) a periodic background training-level algorithm that updates the corresponding model parameters based on the recent historical data (training set) and segmentation results obtained in the last training cycle (10 s); (2) a realtime foreground detection-level algorithm that labels the current pattern with the current Then, four sub-phases can be defined by each adjacent pair of the above-mentioned events, namely: (1) load response (LR), (2) mid-stance (MSt), (3) terminal stance (TSt), and (4) swing (Sw).For model consistency, all sub-phases continue until the next adjacent event occurs.

Adaptive Gait Detection Method
To enhance the adaptability of the detection algorithm to deal with parameter variations of gait, especially variations of walking speed and limb quasi-periodic behavior, a two-level algorithm was developed: (1) a periodic background training-level algorithm that updates the corresponding model parameters based on the recent historical data (training set) and segmentation results obtained in the last training cycle (10 s); (2) a real-time foreground detection-level algorithm that labels the current pattern with the current IMU signals by a set of rules and then decodes the latest acquired data (testing set) into events or sub-phases with the latest gait models of the current pattern at 100 Hz.By using the two-level algorithms, the testing set is independent from the training set and avoids data leakage.By using multi-threading technology, the real-time performance of detection is guaranteed.The flowchart of the two-level algorithm is presented in Figure 3. IMU signals by a set of rules and then decodes the latest acquired data (testing set) into events or sub-phases with the latest gait models of the current pattern at 100 Hz.By using the two-level algorithms, the testing set is independent from the training set and avoids data leakage.By using multi-threading technology, the real-time performance of detection is guaranteed.The flowchart of the two-level algorithm is presented in Figure 3.

Training-Level Algorithm: Segmentation Rules and Model Training
The training-level algorithm runs every 10 s and updates current gait model parameters with the largest amount of continuous data of the corresponding pattern.The training set is used for further processing only if the data satisfies the set conditions (≥700 continuous data collected within this 10 sec, checkpoint 1).Otherwise, the training set will also be deprecated to avoid data leakage.Data from the first and last steps are removed before training to reduce the impact of pattern detection delays.The input data consists of an observation sequence . Set ω as angular velocity of x-axis of the thigh

Training-Level Algorithm: Segmentation Rules and Model Training
The training-level algorithm runs every 10 s and updates current gait model parameters with the largest amount of continuous data of the corresponding pattern.The training set is used for further processing only if the data satisfies the set conditions (≥700 continuous data collected within this 10 sec, checkpoint 1).Otherwise, the training set will also be deprecated to avoid data leakage.Data from the first and last steps are removed before training to reduce the impact of pattern detection delays.The input data consists of an observation sequence X = [x 1 , . . . ,x n , . . . ,x 1000 ], where Set ω thigh−x as angular velocity of x-axis of the thigh IMU, ω foot−x as angular velocity of x-axis of the foot IMU, and a foot−z as acceleration of z-axis of foot IMU.
The algorithm segments these data into several events and sub-phases using a set of rules (shown in Figure 4).Peak detection, zero-crossing detection, and flat-zone detection are used for gait event feature search to reduce the effect of sensor drift on gait model parameters.Foot flat and heel-off are two common events in all three patterns.When the foot is approaching stationary between these two events, ω thigh−x maintains a low value near the zero point.According to the gait model, the algorithm searches for the other two events in temporal order based on these two events.In LGW, when initial contact occurs, the foot IMU shows a corresponding impact peak in a foot−z before foot flat position within 300 ms.Accordingly, the algorithm searches for the maximum point in this period and labels it as the initial contact.The algorithm searches for the first zero-crossing point after heel-off in ω foot−x and labels it as the final contact.In SA, the algorithm searches for the minimum point within 300 ms before foot flat in a_(foot-z) and labels it as initial contact, and the maximum point within 300 ms after heel-off in ω foot−x can be labeled as final contact.In SD, the minimum point within 300 ms before foot flat can be labeled as initial contact, and the maximum point within 300 ms after heel-off can be labeled as final contact.Data between adjacent events are labeled as different sub-phases according to the walking pattern models.

IMU, ω
as angular velocity of x-axis of the foot IMU, and a as acceleration of z-axis of foot IMU.
The algorithm segments these data into several events and sub-phases using a set of rules (shown in Figure 4).Peak detection, zero-crossing detection, and flat-zone detection are used for gait event feature search to reduce the effect of sensor drift on gait model parameters.Foot flat and heel-off are two common events in all three patterns.When the foot is approaching stationary between these two events, ω maintains a low value near the zero point.According to the gait model, the algorithm searches for the other two events in temporal order based on these two events.In LGW, when initial contact occurs, the foot IMU shows a corresponding impact peak in a before foot flat position within 300 ms.Accordingly, the algorithm searches for the maximum point in this period and labels it as the initial contact.The algorithm searches for the first zero-crossing point after heel-off in ω and labels it as the final contact.In SA, the algorithm searches for the minimum point within 300 ms before foot flat in a_(foot-z) and labels it as initial contact, and the maximum point within 300 ms after heel-off in ω can be labeled as final contact.In SD, the minimum point within 300 ms before foot flat can be labeled as initial contact, and the maximum point within 300 ms after heel-off can be labeled as final contact.Data between adjacent events are labeled as different sub-phases according to the walking pattern models.
Literature [25] implies that 5 complete steps are enough to train a short-term LGW gait detection model.A similar strategy is adopted; 5 steps of past data are used for LGW model training, while 4 steps of past data are used for SA/SD model training (checkpoint 2).When the training set contains over the set number of steps, the algorithm then trains and updates all parameters of the gait model.The above-mentioned gait models with four events and four sub-phases can be considered a stochastic process restrained by a Markov chain (shown in Figure 5) whose probability of the current state only depends on the previous state.For each state, since only a set of emissions, not the state sequence, can be observed, a Gaussian Mixed-Model-based Literature [25] implies that 5 complete steps are enough to train a short-term LGW gait detection model.A similar strategy is adopted; 5 steps of past data are used for LGW model training, while 4 steps of past data are used for SA/SD model training (checkpoint 2).When the training set contains over the set number of steps, the algorithm then trains and updates all parameters of the gait model.
The above-mentioned gait models with four events and four sub-phases can be considered a stochastic process restrained by a Markov chain (shown in Figure 5) whose probability of the current state only depends on the previous state.For each state, since only a set of emissions, not the state sequence, can be observed, a Gaussian Mixed-Modelbased Hidden Markov Model (GMM-HMM) [1] is used to estimate the most likely hidden state sequence S = {S 1 , . . .S i , . . . ,S 8 }, i = {1, . . . ,8}, which corresponds to four events and four sub-phases in each gait detection model.Different pattern models have their own GMM-HMM.
Electronics 2023, 12, x FOR PEER REVIEW 7 Hidden Markov Model (GMM-HMM) [1] is used to estimate the most likely hidden sequence  = {S , … S , … , S } , i = {1, … ,8} , which corresponds to four events and sub-phases in each gait detection model.Different pattern models have their own G HMM.There are three parts of parameters in GMM-HMM as λ = {, ,  ()}.Paramet A is a transition matrix (8 × 8 square matrix) and defines a circular left-to-right cha lowing only transitions.Each row in the matrix represents one hidden state S , as th lowing form:

MSt
where a are probability of a hidden transition and equal to the ratio between the mated number of transitions from the hidden state S to S , satisfying 0 ≤ a ≤ 1 ∑ a = 1.As all events (row 2, 4, 6, 8) were boundaries of sub-phases (row 1, 3, 5, 7), probability of remaining in the same state was equal to zero, and their probability of sition was set to 1.There are three parts of parameters in GMM-HMM as λ = {π, A, b i (x)}.Parameter set A is a transition matrix (8 × 8 square matrix) and defines a circular left-to-right chain allowing only transitions.Each row in the matrix represents one hidden state S i , as the following form: , where a ij are probability of a hidden transition and equal to the ratio between the estimated number of transitions from the hidden state S i to S j , satisfying 0 ≤ a ij ≤ 1 and ∑ j a ij = 1.As all events (row 2, 4, 6, 8) were boundaries of sub-phases (row 1, 3, 5, 7), their probability of remaining in the same state was equal to zero, and their probability of transition was set to 1. GMMs are a set of parameters governing the distribution probability: b i (x) = p(x|µ i , Σ i ).Mean value µ i (GMM-µ) and covariance matrix Σ i are feature distributions of S i .If all events and sub-phases have already been labeled, the equation of GMMs can be simplified as follows: x n (1) where n i is the number of observation values labeled as S i .After all parameters are computed, the algorithm updates parameters a ij and b i (x) of the gait model of the pattern for further detection.

Detection-Level Algorithm: Walking Pattern Detection and Gait Decoding
The first part of the detection-level algorithm is a rule-based approach, which detects the current walking pattern and labels the current observation sequence x t according to different postures of the lower limbs (shown in Figure 6).GMMs are a set of parameters governing the distribution probability:  (x) = p(| ,  ).Mean value  (GMM-µ) and covariance matrix  are feature distributions of S .If all events and sub-phases have already been labeled, the equation of GMMs can be simplified as follows: where n is the number of observation values labeled as S .After all parameters are computed, the algorithm updates parameters a and  () of the gait model of the pattern for further detection.

Detection-Level Algorithm: Walking Pattern Detection and Gait Decoding
The first part of the detection-level algorithm is a rule-based approach, which detects the current walking pattern and labels the current observation sequence x according to different postures of the lower limbs (shown in Figure 6).Switching between LGW and SA can be detected by a dynamic threshold of thigh angle (pitch value of the thigh IMU) during the swing phase or at initial contact.This thigh angle threshold is the maximum angle that the thigh swings forward in LGW and is also calculated by peak detection and updated in the training-level algorithm.Switching between LGW and SD can be detected by a static angular velocity threshold with foot IMU during the period from initial contact to load response.Rules for pattern switching are presented in Figure 7 and Table 1.The default pattern of the detection algorithm is LGW.Switching between LGW and SA can be detected by a dynamic threshold of thigh angle (pitch value of the thigh IMU) during the swing phase or at initial contact.This thigh angle threshold is the maximum angle that the thigh swings forward in LGW and is also calculated by peak detection and updated in the training-level algorithm.Switching between LGW and SD can be detected by a static angular velocity threshold with foot IMU during the period from initial contact to load response.Rules for pattern switching are presented in Figure 7 and Table 1.The default pattern of the detection algorithm is LGW.Once the current walking pattern is detected, the detection algorithm then switches the gait model to the corresponding one for further gait event decoding.Once the current walking pattern is detected, the detection algorithm then switches the gait model to the corresponding one for further gait event decoding.1.
Table 1.Rule Base of Walking Pattern Switching.

Pattern Switch Rule Remarks
Level-ground Walking The second part of the decoding-level algorithm is an HMM-based decoding approach.It iterates values with the current observation sequence x by Viterbi algorithm and decodes the current state with the gait model of the current detected pattern at 100 Hz.The results of every iteration will be temporarily stored and used for the next loop.The equations are as follows: where  is a vector containing all values of the current hidden state S , φ is the location of the maximum value of  and can be seen as S .
The parameter set  is initial probabilities { , …  , … ,  } and should be calculated once the detection algorithm starts and the pattern changes.The initial equations are as follows:  1.

Pattern Switch Rule Remarks
Level-ground Walking The second part of the decoding-level algorithm is an HMM-based decoding approach.It iterates values with the current observation sequence x t by Viterbi algorithm and decodes the current state with the gait model of the current detected pattern at 100 Hz.The results of every iteration will be temporarily stored and used for the next loop.The equations are as follows: where V t is a vector containing all values of the current hidden state S j , ϕ i is the location of the maximum value of V t and can be seen as S j .
The parameter set π is initial probabilities {p 1 , . . .p i , . . . ,p 8 } and should be calculated once the detection algorithm starts and the pattern changes.The initial equations are as follows: (5) where d is the dimension of the inputs.Then, set p i to 1 as the initial hidden state, and the other parameters of π are set to 0.

Algorithm Validation Experiments
Ten subjects (males, 28.2 ± 4.6 years old, 176.4 ± 4.1 cm) without deformity, amputation, or other factors that may modify the gait behaviors were recruited to test the detection algorithm in a common experiment terrain (shown in Figure 8).The subjects were required to wear the device as presented in Figure 1b.Although only the inertial components of IMUs are used, to guarantee the accuracy and reliability of the data, each subject was asked to stand still for a few seconds before each trial to execute the initialization of the IMUs and magnetic sensors.Then, the subject was required to complete a ten-second free walk in each pattern to initial the corresponding gait model.To test the adaptive ability of the algorithm, all subjects then completed two rounds of ascent/descent of ten floors and a three-minute free level-ground walk in the corridors (0.8 m/s~1.3m/s).The experimental step of each subject's left leg were over 200 in each pattern, which ensured a reliable sample set.
where d is the dimension of the inputs.Then, set  to 1 as the initial hidden state, and the other parameters of  are set to 0.

Algorithm Validation Experiments
Ten subjects (males, 28.2 ± 4.6 years old, 176.4 ± 4.1 cm) without deformity, amputation, or other factors that may modify the gait behaviors were recruited to test the detection algorithm in a common experiment terrain (shown in Figure 8).The subjects were required to wear the device as presented in Figure 1b.Although only the inertial components of IMUs are used, to guarantee the accuracy and reliability of the data, each subject was asked to stand still for a few seconds before each trial to execute the initialization of the IMUs and magnetic sensors.Then, the subject was required to complete a ten-second free walk in each pattern to initial the corresponding gait model.To test the adaptive ability of the algorithm, all subjects then completed two rounds of ascent/descent of ten floors and a three-minute free level-ground walk in the corridors (0.8 m/s~1.3m/s).The experimental step numbers of each subject's left leg were over 200 in each pattern, which ensured a reliable sample set.To evaluate the performance of the walking pattern detection strategy, the patterns of subjects were manually marked as a reference for post-evaluation during the walking trails with stairs.The marking time for pattern switching was generally at the first IC of the leading leg in the new patterns, whether it was the sensing leg or the contralateral leg.All the detection results and reference marks were stored in a memory card at 100 Hz, as well as the parameters of the gait models.

Data Post-Processing and Detection Performance Evaluation
The foot contact status from plantar FSRs and the knee angle from the magnetic position sensor were presented as reference data for gaits for assessing the performance of detection (shown in Figure 9a).Two indexes, accuracy rate and time delay, were used to evaluate event detection performance in each pattern.A tolerable window of 200 ms at each gait event detection was used to determine whether the result was correct or not.Precision (P), recall (R), and F1-score were used to evaluate the universal accuracy rate of To evaluate the performance of the walking pattern detection strategy, the patterns of subjects were manually marked as a reference for post-evaluation during the walking trails with stairs.The marking time for pattern switching was generally at the first IC of the leading leg in the new patterns, whether it was the sensing leg or the contralateral leg.All the detection results and reference marks were stored in a memory card at 100 Hz, as well as the parameters of the gait models.

Data Post-Processing and Detection Performance Evaluation
The foot contact status from plantar FSRs and the knee angle from the magnetic position sensor were presented as reference data for gaits for assessing the performance of detection (shown in Figure 9a).Two indexes, accuracy rate and time delay, were used to evaluate event detection performance in each pattern.A tolerable window of 200 ms at each gait event detection was used to determine whether the result was correct or not.Precision (P), recall (R), and F 1 -score were used to evaluate the universal accuracy rate of detection algorithms.Among them, F 1 -score is the harmonic mean of precision and recall, which can synthetically evaluate the performance of a detection method.
where P, R, and F 1 ∈ (0, 1), TP are true positives, FP are false positives, and FN are false negatives.
Electronics 2023, 12, x FOR PEER REVIEW 11 of 17 detection algorithms.Among them, F1-score is the harmonic mean of precision and recall, which can synthetically evaluate the performance of a detection method.
where P, R, and F1 ∈ (0,1), TP are true positives, FP are false positives, and FN are false negatives.The mean differences between detection results and corresponding reference features represented the time delays of detection for each event.Meanwhile, a standard deviations was used to evaluate the stability of this detection delay.The lower values of mean and standard implied fewer time delays from the detection algorithm.
Manual markers were used to evaluate the performance of pattern detection and were expressed in steps instead of milliseconds (shown in Figure 9b).The mean differences between detection results and corresponding reference features represented the time delays of detection for each event.Meanwhile, a standard deviations was used to evaluate the stability of this detection delay.The lower values of mean and standard implied fewer time delays from the detection algorithm.
Manual markers were used to evaluate the performance of pattern detection and were expressed in steps instead of milliseconds (shown in Figure 9b).

Parameter Evolution in Different Walking Patterns
In the three-dimensional space of the observation sequence x, differences between swing phase and stance phase were obvious in SA (Figure 10b).Detection results at the end of the swing became slightly chaotic with those of load response in LGW (shown in Figure 10a), and detection results of load response swing were intersected in the space to a certain extent in SD (shown in Figure 10c), which might explain the misidentification of these two sub-phases (shown in Figure 10d).

Parameter Evolution in Different Walking Patterns
In the three-dimensional space of the observation sequence x, differences between swing phase and stance phase were obvious in SA (Figure 10b).Detection results at the end of the swing became slightly chaotic with those of load response in LGW (shown in Figure 10a), and detection results of load response and swing were intersected in the space to a certain extent in SD (shown in Figure 10c), which might explain the misidentification of these two sub-phases (shown in Figure 10d).
GMM-µ of initial contact is relatively concentrated (shown in Figure 11a).Several GMM-µ are highly clustered along a line in space.This clustering shows that different models have some compatibility when pattern misidentification occurs.Swing in LGW and SA has the highest stability value in the transition matrix, followed by mid-stance (shown in Figure 11b).The transition probabilities of SD fluctuated more, indicating that different sub-phases have different occupancy lengths within the 10 s training set.This reflects that subjects have greater pattern variability (postural differences in lower limbs or speed changes) in SD.GMM-µ of initial contact is relatively concentrated (shown in Figure 11a).Several GMM-µ are highly clustered along a line in space.This clustering shows that different models have some compatibility when pattern misidentification occurs.Swing in LGW and SA has the highest stability value in the transition matrix, followed by mid-stance (shown in Figure 11b).The transition probabilities of SD fluctuated more, indicating that different sub-phases have different occupancy lengths within the 10 s training set.This reflects that subjects have greater pattern variability (postural differences in lower limbs or speed changes) in SD.

Performance Indexes of the Detection Method
In all experiments, the detection algorithm cost only about 1 ms for each event/subphase detection loop and 20 ms for each model training loop.The total numbers of complete steps recorded were found to be equal to 2677, 2175, and 2187 in LGW, SA, and SD, respectively.The accuracies of walking pattern detection were 97.8% for LGW, 95.6% for SA, and 97.1% for SD.Events are not always directly decoded due to the small sample size boundaries of different sub-phases used as the corresponding events in this study.Most F1-scores for gait event detection were over 0.90 (shown in Table 2).The F1-score of initial contact in SD is 0.86, as the misidentification of load response and swing increased the total number of samples of initial contact.Another possible factor is that the reference to plantar FSRs produced distortions in some events, such as heel-off in SA and SD.The mean differences between events were less than 100 ms under the 200 ms tolerable window (shown in Table 3).The mean differences between events and their corresponding knee extension/flexion points are shown in Table 4.The final contact and knee flexion point in SD had the largest time delay, and the others were around 100 ms.A positive mean difference indicates that the event occurs later than its reference, which should

Performance Indexes of the Detection Method
In all experiments, the detection algorithm cost only about 1 ms for each event/subphase detection loop and 20 ms for each model training loop.The total numbers of complete steps recorded were found to be equal to 2677, 2175, and 2187 in LGW, SA, and SD, respectively.The accuracies of walking pattern detection were 97.8% for LGW, 95.6% for SA, and 97.1% for SD.Events are not always directly decoded due to the small sample size boundaries of different sub-phases used as the corresponding events in this study.Most F 1 -scores for gait event detection were over 0.90 (shown in Table 2).The F 1 -score of initial contact in SD is 0.86, as the misidentification of load response and swing increased the total number of samples of initial contact.Another possible factor is that the reference to plantar FSRs produced distortions in some events, such as heel-off in SA and SD.The mean differences between events were less than 100 ms under the 200 ms tolerable window (shown in Table 3).The mean differences between events and their corresponding knee extension/flexion points are shown in Table 4.The final contact and knee flexion point in SD had the largest time delay, and the others were around 100 ms.A positive mean difference indicates that the event occurs later than its reference, which should be seen as a time delay.A negative one indicates that the event occurs before its reference and can be used for event prediction.The mean difference of initial contact with the knee flexion point in SD has a certain delay of about 137 ms, which shows that knee flexion occurs closer to the foot flat.Then, initial contact is more related to the movement of the ankle joint.

Discussion
Event detection of different walking patterns plays an important role in the control of knee IWADs.It helps improve the performance and reliability of these devices in their ability to adjust control strategies to provide more comfortable and efficient assistance.Using only two IMUs on the thigh and foot is a cost-effective, wearable, and easy-to-integrate solution.In this study, the detection method can effectively detect continuous level-ground walking, stair ascent, and stair descent with four gait events of each pattern by using only four dimensions of data without further processing (e.g., gravity compensation, signal fusion, or band-pass filtering) at 100 Hz.In addition, the online analysis on desktop computers lacked practical values when considering daily use and was not indeed performable in wearable robots, and our on-board training method can break these limits.A series of advantages, including only obtaining sensing data from a well-fitting brace structure rather than bio-electricity signals from the wearer, depending on the features of sensing data rather than predefined thresholds, and continuously updating the parameters with online training rather than a fixed model, make the present method easy-to-use.
By using an adaptive algorithm to re-train the gait model every 10 s, the data collection and model training steps are integrated into the detection process, and the effects due to data drift are reduced.Therefore, the present method has fine repeatability and selfadaptability.With the two checkpoints mentioned in Section 2.3.1, the training set contains unpredicted motion behaviors, such as jumping and standing, that are not used for model training.Experiments with free walking on different terrains confirmed that the algorithm has good detection accuracy for different patterns (≥93.6%) and a high score of adaptability to different subjects' gait (F 1 -score ≥ 0.876) with stable detection (≤51 ± 32.4 ms).The results of the present study seem to imply that some foot-posture-based events might have a mapping relationship with the feature points of knee extension/flexion (around 100 ms), and the events related to knee extension have some prediction performance in SA and SD.This might be beneficial for the mechanical control of IWADs for walking, according to the Lit [26].
The results presented in this study are comparable to previous results reported for those of previously reported real-time event detection algorithms using inertial sensor data.Xu et al. [27] tested three different algorithms (SVM, QDA, and LDA) with a 10,000 × 80 training set for walking pattern detection.The accuracy of pattern detection was over 95%.The training time was 89 s for SVM, 25 s for QDA, and 10 s for LDA.The detection costs were 12.4 ms (SVM), 5.36 ms (QDA), and 0.067 ms (LDA), respectively.The total accuracy of pattern detection was over 95% on average.Two MCUs were required, one for pattern detection and the other for modeling training.While the pattern detection used in this study is a rule-based algorithm, we made the detection time short to about 1 ms (one-tenth of the 100 Hz detection frequency) and model training short to about 20 ms by using a 1000 × 3 training set and an optimized detection strategy with one single portable MCU.This short-term model training algorithm with low computing costs is feasible to use on portable platforms.
In terms of gait event detection, Andrea et al. [26,28] tested a simulation of online detection with a non-adaptive HMM-based algorithm.Accuracies of initial contact were 93% (LGW) and 91% (SA), those of foot flat were 91% (LGW) and 89% (SA), those of heel-off were 86% (LGW) and 85% (SA), and those of final contact were 90% (LGW) and 85% (SA).This shows that the adaptive method in this study is effective in improving detection accuracy.Hui et al. [29] used a rule-based algorithm and had a high F 1 -score of over 0.95 in all detections.Sahoo et al. [9] used rule-based algorithms with two IMUs on foot and shank to detect events during different patterns.The accuracy of initial contact detection was 100% (LGW), 99.15% (SA), and 99.49% (SD).The accuracy of final contact detection was 100% (LGW), 99.03% (SA), and 99.37% (SD).But each event detection was based on the previous event; otherwise, the subsequent events could not be detected.In addition, they mentioned that the rule-based algorithm is affected by the speed of movement of the lower limbs.Rule-based walking pattern detection in this study seemed to have similar problems with foot angular velocity, but the gait event detection method seemed to be less affected by speed factors.Time delays presented in this study are comparable to previous results reported for real-time gait event detection algorithms that use data from inertial sensors placed on the lower limbs and evaluated in healthy subjects, Table 5.There are still some limitations to this study.The training-level algorithm runs every 10 s automatically based on detection rules.Therefore, it is not guaranteed that data from LGW, SA, or SD will be trained into corresponding models with the four-step requirement of the method.Therefore, the gait models of SA and SD might not be updated properly once the walk time or the steps on the stairs are too few.The irregular distributions of detection results presented in Figure 10 indicate that subjects' locomotor behavior changed due to fatigue induced by prolonged exercise in SA and SD.These factors will also affect the detection accuracy rate.Only the pattern detection of the left sensing leg is tested.However, the condition might be different depending on whether the leg is the leading leg when the pattern changes.This will cause a maximum detection delay of up to 1.5 steps.This delay may cause a short-term negative impact on event detection.In addition, since the detection method was developed to detect three specific walking patterns, it does not have the ability to detect other locomotion.Relevant research to solve these limitations will be carried out in the future.

Conclusions
In this paper, a detection method with a short-term model updating strategy is presented for a real-time hybrid algorithm.Three walking patterns and four events of each pattern were detected by using four inertial sensing data from two IMUs on foot and thigh.The short-term model training strategy removes the large amount of data acquisition and model training steps in advance for machine learning.According to the results with ten healthy subjects, the detection system shows good adaptive ability to the subject's gaits and has a good performance for real-time walking patterns and events in detection.However, the system has a certain pattern detection delay for the single leg, and some periods of swing phase might be misidentified as load responses during stair descent.With the support of the optimized GMM-HMM algorithm, the system has a light calculation burden, which can achieve stable operation at a relatively high detection frequency on a portable MCU.This real-time on-board walking pattern and event detection method is feasible for powered knee assistive device applications after adding additional sensor dimensions or limiting conditions.

Figure 1 .
Figure 1.(a) Device wearing and installation of IMUs; (b) details of the gait detection device.Several points are set to fix the cables.

Figure 1 .
Figure 1.(a) Device wearing and installation of IMUs; (b) details of the gait detection device.Several points are set to fix the cables.

Figure 2 .
Figure 2. The finite-state machine of the gait model of different patterns: (a) level-ground walking, (b) stair ascent, and (c) stair descent.

Figure 2 .
Figure 2. The finite-state machine of the gait model of different patterns: (a) level-ground walking, (b) stair ascent, and (c) stair descent.

Figure 3 .
Figure 3. Flowchart of the proposed gait-detection algorithms.

Figure 3 .
Figure 3. Flowchart of the proposed gait-detection algorithms.

Figure 4 .
Figure 4. Features of events in different walking patterns.

Figure 4 .
Figure 4. Features of events in different walking patterns.

Figure 5 .
Figure 5. Markov model of the gait events of different walking patterns.

Figure 5 .
Figure 5. Markov model of the gait events of different walking patterns.

Figure 6 .
Figure 6.Features of walking pattern switching.The first column shows the lower limbs' posture at initial contact with the three walking patterns.Differences in the minimum value of the thigh are used to detect patterns between LGW and SA.The difference of the maximum value of ω in the gray areas (150 ms) is used to detect the pattern between LGW and SD.

Figure 6 .
Figure 6.Features of walking pattern switching.The first column shows the lower limbs' posture at initial contact with the three walking patterns.Differences in the minimum value of the thigh are used to detect patterns between LGW and SA.The difference of the maximum value of ω foot−x in the gray areas (150 ms) is used to detect the pattern between LGW and SD.

Figure 7 .
Figure 7.The relationship of three Markov chains of three typical walking patterns.Four paths represent the conditions of walking pattern switching in Table1.

①
If thigh angle > thigh angle threshold-15 deg switch to swing of SA ② Meet condition: a) ω < −150 deg/s and ω > −20 deg/s and position of ω < position of ω in the buffer, b) ω (within 300 ms after condition a) < 30 deg/s and absolute value of the last five data < 9 deg/s.switch to mid-stance of SD Stair Ascent ③ If (thigh angle < thigh angle threshold-15 deg) when initial contact occurs switch to initial contact of LGW Stair Descent ④ Meet condition: a) ω < −50 deg/s and ω > 50 deg/s and position of ω < position of ω , b) ω (within 100 ms after condition a) > 100 deg/s switch to load response of LGW ω : minimum value of ω _ in the ω buffer.ω : maximum value of ω in the buffer.

Figure 7 .
Figure 7.The relationship of three Markov chains of three typical walking patterns.Four paths represent the conditions of walking pattern switching in Table1.

1
If thigh angle > thigh angle threshold-15 deg switch to swing of SA 2 Meet condition: (a) ω foot−x−min < −150 deg/s and ω foot−x−max > −20 deg/s and position of ω foot−x−min < position of ω foot−x−max in the buffer, (b) ω foot−x (within 300 ms after condition (a) < 30 deg/s and absolute value of the last five data < 9 deg/s.switch to mid-stance of SD Stair Ascent 3 If (thigh angle < thigh angle threshold-15 deg) when initial contact occurs switch to initial contact of LGW Stair Descent 4 Meet condition: (a) ω foot−x−min < −50 deg/s and ω foot−x−max > 50 deg/s and position of ω foot−x−min < position of ω foot−x−max , (b) ω foot−x (within 100 ms after condition (a) > 100 deg/s switch to load response of LGW ω foot−x−min : minimum value of ω foot_x in the ω foot−x buffer.ω foot−x−max : maximum value of ω foot−x in the buffer.

Figure 8 .
Figure 8. Experiment with different walking patterns (22 stairs for each floor, 17 cm height × 26 cm length for each stair, 125 cm length × 130 cm width for each platform).

Figure 8 .
Figure 8. Experiment with different walking patterns (22 stairs for each floor, 17 cm height × 26 cm length for each stair, 125 cm length × 130 cm width for each platform).

Figure 9 .
Figure 9.Comparison of detection results from XXX and reference standards based on (a) gait detection results, foot contact states, and angle of knee.(b) pattern detection results and manual markers of walking patterns.

Figure 9 .
Figure 9.Comparison of detection results from XXX and reference standards based on (a) gait detection results, foot contact states, and angle of knee.(b) pattern detection results and manual markers of walking patterns.

Figure 10 .
Figure 10.Distribution of detection results of different patterns from one subject: (a) LGW, (b) SA, (c) SD, and (d) part of the detection results.Figure 10.Distribution of detection results of different patterns from one subject: (a) LGW, (b) SA, (c) SD, and (d) part of the detection results.

Figure 10 .
Figure 10.Distribution of detection results of different patterns from one subject: (a) LGW, (b) SA, (c) SD, and (d) part of the detection results.Figure 10.Distribution of detection results of different patterns from one subject: (a) LGW, (b) SA, (c) SD, and (d) part of the detection results.

Figure 11 .
Figure 11.Parameters of gait models.(a) distribution of GMM-µ of events in different patterns; (b) Sub-phase distribution of transition matrix with different patterns.

Figure 11 .
Figure 11.Parameters of gait models.(a) distribution of GMM-µ of events in different patterns; (b) Sub-phase distribution of transition matrix with different patterns.

Table 3 .
Time delays between detection results and FSR references (mean difference ± 1 standard deviation).Value in ms.

Table 4 .
Time delays between detection results and knee flexion/extension references (mean difference ± 1 standard deviation).Value in ms.

Table 5 .
Time delays obtained by real-time algorithms (mean difference ± 1 standard deviation).Value in ms.