## 4 Materials and Methods
### Exercise Estimation  
After the data collection process, the data are received by the simulation agent. This agent collects and processes the information, IMU sensors, and smartwatch, as shown in Figure 3.  
The information generated by the IMU sensors is collected as quaternions [40]. Quaternions are extensions of real numbers, similar to complex numbers, but they are an extension generated by adding the imaginary units \(i,j\), and \(k\) to real numbers such that  
\[i^{2}=j^{2}=k^{2}=ijk=-1 \tag{1}\]  
The set of all quaternions can be expressed as follows:  
\[\mathbb{H}=\{q_{0}+q_{1}i+q_{2}j+q_{3}k\} \tag{2}\]  
where \(q=(q_{0},\overrightarrow{q})\) is the real part, and \(\overrightarrow{q}=(q_{1}i+q_{2}j+q_{3}k)^{T}\) is the imaginary part of the quaternion. In addition, for [PERSON]'s product between two quaternions, the following properties are fulfilled:  
\[p\circ q\
eq q\circ p\qquad p\circ q\circ r=(p\circ q)\circ r=p\circ(q\circ r )\qquad\text{where}\qquad p,q,r\in\mathbb{H} \tag{3}\]  
The use of quaternions to collect the information produced by the IMU sensors allows us to process this information in the Unity3D [41] environment using 3D points in space that have rotations. After the transformation of the information, a processing of the information is carried out to determine whether the patient is doing the exercise properly as well as his or her condition. To determine whether the patient has performed the exercise correctly, a skilled user (who may be the doctor or someone with knowledge of the sport or motor system) must enter the exercise into the system. To do this, the process to be carried out is similar to Figure 3. The expert user must indicate to the system the introduction of a new exercise, performing the relevant movements relating to the exercise. The system collects the information generated, transforms it, and stores it for later use. Figure 3: Block diagram of the data processing algorithm. The processing of the data allows for the generation of information that is used for the following things:  
* **Visualization of movements**: The data received from the Enflux suit are transformed into quaternions for storage. The position of each of the nodes, which are equivalent to the IMU sensors, can be represented within the Unity3D environment, which allows us to carry out a representation of the patient's movements within the system. * **Information on errors made**: Once the quaternions have been obtained, they are used to detect movements that have been made incorrectly. This is discussed below. * **Data for patient monitoring**: The data generated by the Garmin watch and the Enflux Suit, once transformed, are processed to obtain statistics and information that can be displayed to know the status of the user and the progress of their rehabilitation. As for the error detection, the angular distance between quaternions is used. As such, the angle between two rotations \(a\) and \(b\) is treated as the angle formed if a third rotation \(c\) is defined as moving \(a\) and \(b\) such that, if two lines are drawn from \(c\) to \(a\) and from \(c\) to \(b\), the angle between \(ca\) and \(cb\) is the angle to be used in this case as an interpretation of the angle between \(a\) and \(b\). The equation for obtaining this angle is as follows:  
\[angle=2\cdot\cos^{-1}(x) \tag{4}\]  
where \(x\) is the real part of \((b\circ a^{-1})\). Bearing this in mind, it is possible to discern between correctly executed movements by knowing the angles formed between the various IMU sensors at every instant of time so that, knowing the movements and values of the exercise performed by the expert as well as the patient, it is possible to process this information to know the errors made. The use of angles makes it possible to generalize this processing independently of the size and body of the person, since the angle formed during the execution of the movement is independent of the values of these points in space.