*2.2. CWT Based Gait Event Detection Algorithm*

A cycle of human gait can be divided into a sequence of repeated events and phases. More specifically, one gait cycle consists of stance and swing phases. The HS and TO events mark the beginning of stance and swing phases, respectively. In this case, it is obvious that the occurrence frequency of the two gait events (HS and TO) is twice of one gait cycle. This time-frequency relationship between the gait event and the gait cycle through the CWT can be used to detect gait events [13]. Moreover, it should be noted that the human gait is chaotic in nature in the real-world environment [35]. Thus, continuous wavelet transform methods have been proven to be robust and stable in several previous studies in effectively detecting gait events in practical applications that may be subject to various disturbances [13,20–22,26,28]. Here we propose a general CWT algorithm for the detection of two gait events (HS and TO), which is a combination of the methods in the two previous studies by Minh H. Pham et al. [22] and Siddhartha Khandelwal et al. [13].

The operational procedure of the general CWT based gait event detection algorithm is presented in Figure 2. Here the components of the tri-axially recorded acceleration signals from the anterior-posterior (AP) axis during level ground walking were used for the analysis of gait events [22,36]. In order to improve the quality of the AP signal recordings, the algorithm began with a pre-processing of the AP acceleration signals that were composed of three sequential procedures. Firstly, a linear de-trending algorithm was adopted to reduce the effect of noise and interference on the AP signal baseline. Secondly, a low-pass filtering at 10 Hz with a second-order Butterworth filter was used to remove the high-frequency interferences from the AP signals. Finally, the AP signals were smoothed by integration using the inbuilt function in MATLAB called *cumtrapz*, where the approximation of the cumulative integral of each sampling points along the time intervals was calculated.

**Figure 2.** Conceptualization of the proposed general CWT algorithm with three main phases: (1) Pre-processing of the acceleration signals; (2) Tracking the gait events and gait cycles through time-frequency analysis; (3) Distinguishing HS and TO gait events.

After the AP acceleration signal preprocessing, the time-frequency analysis was performed to track the inherent gait events as well as gait cycles. The CWT of a discrete time signal *xn* with equal time spacing δ*<sup>t</sup>* is defined as the inner product of *xn* with a scaled and translated mother wavelet ψ as expressed in Equation (1).

$$\mathcal{W}\_n(s) = \sum\_{n'=0}^{N-1} \mathbf{x}\_{n'} \boldsymbol{\psi} \* \left[ \frac{(n'-n)\delta\_t}{s} \right] \tag{1}$$

where *Wn*(*s*) denotes the wavelet transform, *s* is the wavelet scaling factor, *n* is the localized time index, and the (\*) indicates the complex conjugate. Detailed definitions of different mother wavelets (ψ) and the prescription of wavelet selection criteria are presented in Section 2.4. The range of scales for CWT analysis [1, *smax*] was chosen using the frequency scale relationship of the chosen wavelet [26], which is presented in Equation (2).

$$s\_{\text{max}} = \frac{f\_{\text{c}} \times \text{F}\_{\text{s}}}{f} \tag{2}$$

where *fc* is the central frequency of the wavelet, *Fs* is the data sampling frequency, and *f* is the gait frequency. Since the healthy subjects usually walk at an average speed of 1.5 m/s, a minimum gait frequency of 0.5 Hz was utilized in this study as the norm [26]. In the experiments, the hemiplegic subjects walked with an average speed of around 0.7–0.8 m/s. Thus, a minimum gait frequency of 0.25 Hz was assumed for the hemiplegic subjects.

Given a mother wavelet, the corresponding frequency scale can be calculated by Equation (2) for CWT analysis, and then the CWT coefficients can be obtained by Equation (1). As a typical example, Figure 3 graphically demonstrates the CWT plots of the acceleration signals during walking from one healthy subject (Figure 3a) and one hemiplegic subject (Figure 3b), where two mother wavelets ("db6" and "morl") were adopted for comparison. It was obvious from Figure 3 that the frequency of gait events underlying the corresponding scales is approximately two times of gait cycles. To distinguish the gait event and the gait cycle, the scale-dependent energy density spectrum *Es* was computed based on the obtained CWT coefficients by using Equation (3).

$$E\_s = \sum\_{n=0}^{N-1} \left| \mathcal{W}\_n(s) \right|^2, \quad s \in \left[ 1, \ s\_{\text{max}} \right] \tag{3}$$

where *N* is the total number of wavelet coefficients and *Wn*(*s*) <sup>2</sup> is the 2-D wavelet energy density function that measures the total energy distribution of the signal associated with the *s* scale. The peaks in *Es* represent the dominant energy scales that contribute most to the signal's energy in the spectral domain [13]. Typically, two distinct spectral energy peaks can be obtained from the above computed *Es* parameter and the corresponding peak scales have a ratio of two due to the frequency relationship between the gait event and the gait cycle. Since the candidate scales of gait events and gait cycles were found from the peaks of *Es*, the peaks were stored as the set of local maxima points {(*sm*, *Esm*), *m* ∈ [1, *M*]}; where *sm* is the scale corresponding to a peak value of *Esm* and *M* is the total number of peaks. Afterwards, the following three possible cases were checked to determine the scale of the associated gait cycle and gait event in the energy density spectrum.

**Figure 3.** Continuous wavelet transform (CWT) plots of the acceleration signals during walking from one healthy subject (**a**) and one hemiplegic subject (**b**), where two mother wavelets ("db6" and "morl") were adopted for comparison. The underlying scale relationship between the gait events (HS and TO) and gait cycles are illustrated.

Case I: If there was only a single peak (*M* = 1) in the energy density spectrum, then the dominant spectral scale was tracked as the cycle scale using *scycle* = *s*<sup>1</sup> and the event scale was tracked using the *sevent* = *round scycle*/2 relationship.

Case II: If there were two dominant peaks (*M* = 2) in the energy density spectrum, then the cycle scale was tracked using *scycle* = *s*<sup>2</sup> and the event scale was tracked using the *sevent* = *s*1(*s*<sup>1</sup> < *s*2) relationship.

Case III: If there were more than two peaks (*M* ≥ 3) in the energy density spectrum, then the ratio of any two consecutive (neighboring) peaks denoted as δ = *sm*+1/*sm* (*sm* < *sm*+1, *m* ∈ [1, *M*)) was checked with the relaxed spectral relationship that δ ∈ [1.6, 2.4] to attenuate interferences in the signal and manage the occurrence of low frequency [14]. Thus, the two latest peaks that satisfied the above condition were tracked as the dominant peaks using the *scycle* = *sm*+<sup>1</sup> and *sevent* = *sm* (*sm* < *sm*+1) relationships.

Based on the conditions specified in Cases I–III, the scales of gait event and gait cycle were determined with the associated CWT coefficients (*xevent* and *xcycle*) as shown in Equations (4) and (5).

$$\propto\_{\text{event}} \triangleq \mathcal{W}\_{\text{H}} \left( s\_{\text{event}} \right) \tag{4}$$

$$\mathbf{x}\_{\text{cycle}} \triangleq \mathcal{W}\_n \left( \mathbf{s}\_{\text{cycle}} \right) \tag{5}$$

After de-trending both the above temporal signals (*xevent* and *xcycle* ), the gait cycle bounds could be defined as the maximum points of *xcycle*, which marked the beginning of each gait cycle. Within every gait cycle, the HS event was defined as the first local minima of *xevent* and TO event was defined as the second local maxima of the further differentiated *xevent*. An illustration of the temporal representation of HS and TO gait event detection based on the proposed general CWT algorithm is illustrated in Figure 4.

**Figure 4.** Temporal representation of HS and TO gait event detection based on the proposed general CWT algorithm. The first local minima of *xevent* corresponded to the estimated HS event (orange circle). The second local maxima of the further differentiated *xevent* corresponded to the estimated TO event (purple circle). Each gait cycle bound was defined as the maximum points of *xcycle*. Vertical dashed lines indicated the time-errors between the estimated event and the reference event from the FSR method.
