## 3 Linear Kalman Filter and Derivatives  
The linear Kalman filter (LKF) [31] is a recursive algorithm that performs real-time estimation of the state variables of a system. It is based on minimizing the estimation error between the state real value \(x_{k}\) and the estimated value \(\hat{x}_{k}\) using the least-squares estimation method. The algorithm operates recursively in two stages: estimation and correction, as shown in Figure 3. The mathematical model description of a sinusoidal and non-sinusoidal signal is presented in Appendix A. Among many other applications, the LKF is used to estimate the amplitude and phase of the fundamental frequency component of the voltage in the electrical power network [28]. The LKF assumes a stochastic model for the estimation of the state variables. This model should be defined before starting the recursive estimation process. The equations that define the system model to be estimated are defined as follows:  
\[x_{k+1}=\Phi_{k}x_{k}+w_{k} \tag{5}\]  
\[z_{k}=H_{k}x_{k}+v_{k} \tag{6}\]  
where  
\(x_{k}\) corresponds to the value of the state vector at the current instant (\(k\)). \(\Phi_{k}\) corresponds to the value of the state transition matrix at the current instant (\(k\)). \(x_{k+1}\) corresponds to the value of the state vector at the next sampling instant (\(k+1\)). \(w_{k}\) corresponds to the value of the system's intrinsic error at the current instant (\(k\)). \(z_{k}\) corresponds to the value of the system's output at the current instant (\(k\)). \(H_{k}\) corresponds to the value of the system's output matrix at the current instant (\(k\)). \(v_{k}\) corresponds to the value of the error introduced and measured by the measurement system at the current instant (\(k\)). \(w_{k}\) and \(v_{k}\) are uncorrelated white noises, where \(w_{k}\) is the intrinsic noise of the system, and \(v_{k}\) is the noise introduced by the measurement system. Their known covariances are:  
\[E\Big{\{}w_{k}w_{l}^{T}\Big{\}}=\left\{ \begin{array}{l}Q_{k};i=k\\ 0,i\
eq k\end{array} \right. \tag{7}\]  
\[E\Big{\{}v_{k}v_{l}^{T}\Big{\}}=\left\{ \begin{array}{l}R_{k};i=k\\ 0,i\
eq k\end{array} \right. \tag{8}\]\[E\Big{\{}w_{i}{v_{j}}^{T}\Big{\}}=0,\forall k,i \tag{9}\]  
where,  
\(E\{.\}\) represents the mathematical expectation. \(Q_{k}\) is the covariance of the intrinsic system noise. \(R_{k}\) is the covariance of the noise measured in the output. It is assumed that matrices \(\Phi_{k}\) and \(H_{k}\) are known. The state error in the estimation stage is defined as:  
\[e_{k}^{-}=x_{k}-\hat{x}_{k}^{-} \tag{10}\]  
where \(x_{k}\) is the actual state value at time \(k\), and \(\hat{x}_{k}^{-}\) is the estimation of the stage at time instant \(k\), which is obtained by model prediction from previous information at instant \(k-1\). The superscript \((-)\) indicates that the calculation for this time instant \((k)\) was performed using the values obtained or estimated at the previous instant \((k-1)\), and the caret symbol \((\cdot)\) indicates that the displayed value is an estimation rather than a real value. Equations (11) to (15) constitute the recursive LKF model. Equation (11) shows the correction phase, where the value of the state is estimated based on the prediction state estimation \(\hat{x}_{k}^{-}\) and the measured \((z_{k})\), which leads to the error between the output measured value \((z_{k})\) and its estimated value \((H_{k}\hat{x}_{k}^{-})\). The Kalman filter gain \((K_{k})\) is the weighting factor of the error:  
\[\hat{x}_{k}=\hat{x}_{k}^{-}+K_{k}\big{(}z_{k}-H_{k}\hat{x}_{k}^{-}\big{)} \tag{11}\]  
The optimal error weighting factor \((K_{k})\), also known as the Kalman gain matrix, is calculated using Equation (12).