## 2 Materials and Methods
### Human-in-the-Loop Optimization  
The aim of the proposed method is to adaptively tune the trajectory \(q_{d}\) of leg joints to minimize the cost function \(J\) to achieve a balance between the tracking errors and the change in \(\tau_{e}\). The principle of this control method is illustrated in Figure 2.  
The interface was designed on a computer with Visual Studio 2010 to monitor the change of signals of sensors mounted on the leg prosthesis, sampled at 1 kH. Using this interface, information such as the position, velocity, and torque can be stored and analyzed. First, the CPG model parameterizes the trajectory of the joints for a fixed walking gait. The trajectory of the joints was reshaped by switching between the different parameter sets. With the continuous variation in the parameters, minor adjustments of the trajectory can be achieved with a single parameter change. Figure 1: Free-body diagram of leg prosthesis: (**a**) definition of mechanical parameters; (**b**) disabled body with leg prosthesis. Figure 2: Diagram of the control flow. The input trajectory \(q_{r}\) is generated from \(q_{d}\) and \(s_{emg}\), and the output trajectory is \(q\). With the parameter optimization process, \(q_{d}\) can be reshaped to minimize the cost function \(J\). The parameter estimation process ensured the controller’s tracking performance. The formulation of the CPG model can be described by (2) and (3). \[\left\{ \begin{array}{l}q_{di}=r_{0}+\Sigma_{j=1}^{N}r_{j}\sin(\omega_{j}t+s_{j}) \\ \dot{\omega_{j}}=g_{j}[\frac{\delta_{j}}{4}(\Omega_{j}-\omega_{j})-\omega_{j}]\\ \dot{s_{j}}=g_{j}[\frac{\delta_{j}}{4}(S_{j}-s_{j})-s_{j}]\\ \dot{r_{j}}=g_{j}[\frac{\delta_{j}}{4}(R_{j}-r_{j})-r_{j}]\\ \dot{r_{0}}=g_{0}[\frac{\delta_{0}}{4}(R_{0}-r_{0})-r_{0}]\\ \end{array} \right. \tag{2}\]  
where \(q_{di}\) denotes the desired trajectory of \(i\)-th joint of the leg prosthesis and can be generated from the superposition of the total oscillators. \(r_{0}\) denotes the total offset of the oscillator. \(N\) denotes the number of total oscillators. \(j\) denotes the number of the definite one. The variables \(r_{j}\), \(\omega_{j}\), and \(s_{j}\) are the amplitude, frequency, and phase of the \(j\)-th oscillator. \(g_{j}\) and \(g_{0}\) denote the constants and determine the speed of convergence. \(\Omega_{j}\), \(R_{j}\), and \(S_{j}\) are the frequency, amplitude, and phase of the \(j\)-th sine series function. \(R_{0}\) denotes the total offset of the sine series function. According to the Fourier series expansion, we have  
\[\left\{ \begin{array}{l}\Omega_{j}=j\omega_{1}\\ S_{j}=arctan\frac{a_{j}}{b_{j}}\\ R_{j}=\sqrt{a_{j}^{2}+b_{j}^{2}}\\ R_{0}=a_{0}\\ a_{j}=\frac{2}{T}\int_{0}^{T}q_{0}cosi\omega_{1}tdt\\ b_{j}=\frac{\delta_{j}}{2}\int_{0}^{T}q_{0}cosj\omega_{1}tdt\\ a_{0}=\frac{2}{T}\int_{0}^{T}q_{0}dt\end{array} \right. \tag{3}\]  
where \(\omega_{1}=\frac{2\pi}{T}\), \(T\) denotes the gait cycle of the leg prosthesis. \(q_{i0}\) denotes the raw trajectory of \(i\)-th joint. Then, \(\Omega_{j}\), \(R_{j}\), and \(S_{j}\) can be calculated. According to the singular perturbation theory, the limit cycle can be converged from the dynamics of the CPG model. \(\omega_{j}\), \(r_{j}\) and \(s_{j}\) will converge to \(\Omega_{j}\); \(R_{j}\), and \(S_{j}\), respectively. Next, the combined cost function was designed as follows:  
\[J=\frac{\lambda}{T}\Sigma_{i}^{n}(q_{di}-q_{i})^{2}+\frac{1-\lambda}{T}(a \Sigma_{i}^{n}\tau_{c,i}^{2}) \tag{4}\]  
where \(n\) denotes the number of total leg joints and \(i\) denotes the number of definite joint. \(\alpha\) denotes the constant scale gain for \(\tau_{c}\). The first term ensures a better tracking performance, and the second term assists the leg in achieving a lower interaction torque during various walking cases. \(\lambda\in(0,1)\) denotes the weights of the two terms. The cost function focuses on the tracking performance with \(\lambda=1\) and the strength of \(\tau_{c}\) with \(\lambda=0\).