Implementing an Extended Kalman Filter for SoC Estimation of a Li-Ion Battery with Hysteresis: A Step-by-Step Guide

: The growing share of renewable energies in power production and the rise of the market share of battery electric vehicles increase the demand for battery technologies. In both ﬁelds, a predictable operation requires knowledge of the internal battery state, especially its state of charge (SoC). Since a direct measurement of the SoC is not possible, Kalman ﬁlter-based estimation methods are widely used. In this work, a step-by-step guide for the implementation and tuning of an extended Kalman ﬁlter (EKF) is presented. The structured approach of this paper reduces efforts compared with empirical ﬁlter tuning and can be adapted to various battery models, systems, and cell types. This work can act as a tutorial describing all steps to get a working SoC estimator based on an extended Kalman ﬁlter.


Introduction
Lithium-ion batteries (LIB) have shown great potential as an energy storage technology for electrified transportation, portable electronics, and grid stabilization. In contrast to a fuel tank, the remaining energy in a battery cannot be measured directly. Therefore, the internal state of a battery has to be derived from accessible measurement values using mathematical models. These include physical models, data-driven models using machine learning, and equivalent circuit models (ECM). Physical models use differential equations to describe the electrochemical processes inside the cell. These are complex and require detailed information about the cells' composition for accurate parameterization, but provide a detailed description of the processes inside a cell during a wide range of operating conditions [1]. In data-driven models, machine learning algorithms are used to derive the model directly from measured data without prior knowledge of the cell. However, machine learning algorithms require large data sets to properly train the model [2]. Equivalent circuit models (ECM) describe the batteries' behaviour using voltage sources and passive electrical components, typically resistors and capacitors. The model parameters are determined by fitting the model prediction to measured responses. Compared with physical models, they do not provide detailed insight in internal processes, but are less complex and therefore easier to parameterize. ECMs provide a good compromise between accuracy and complexity, and are therefore used in many applications [1,[3][4][5][6][7][8][9][10][11][12][13][14][15]. Models are always a simplification of reality. Model parameters are imprecise, and sensor measurements are noisy. As a result, the prediction of the model diverges from the true system response. To reduce the prediction error, several mathematical filtering techniques have been introduced [16]. A common approach used in LIB applications is the extended Kalman Filter (EKF) [3,9]. It is widely used to estimate the state of charge [17,18], state of health [1], and cell capacity [6,19]. It can also be used for online parameter estimation [20]. A major challenge is the adjustment of the EKF to the specific application. An improperly tuned filter can lead to unstable and unpredictable behavior [21]. In contrast to other publications [7,[10][11][12] which tune the filter empirically, this paper systematically determines the required settings. In this process, a novel extension for handling current sensor noise is proposed. As an additional improvement, this work extends the common battery model by a hysteresis element, which is often omitted in the literature [7,[10][11][12][13][14][15]21]. Battery systems often consist of many cells. The SoC estimation on a system level is always based on estimating the SoC of single cells [22]. Therefore, this paper focuses on single-cell SoC estimation only.

Structure of the Paper
Section 2 describes the theory of the battery model and the extended Kalman filter (EKF). In Section 3, EKF extensions for determining the process noise matrix, handling current sensor noise, and filter initialization are introduced. In Section 4, methods for cell characterization are introduced. Section 5 provides a step-by-step guide for setting up the EKF for application. Finally, Section 6 shows an experimental validation.

Theory
The principle of the extended Kalman filter for SoC estimation is shown in Figure 1. The EKF compares the measured cell voltage and the cell voltage predicted by a battery model. The model also predicts internal, immeasurable states. One of these states is the SoC of the battery cell. In a second step, the EKF corrects the internal states under consideration of the estimated accuracies of the measurement and the model prediction. Thus, it provides a corrected estimation of the SoC.
The sign of the current is positive for discharging and negative for charging processes. Note that unlike [1], this paper uses the charge q instead of the state of charge z = q/Q cell , which makes equation (2) independent of the cell capacity Q cell .
In contrast to many other approaches [7,[10][11][12][13][14][15], the used model includes a hysteresis element, as proposed in [1]. The importance of this additional element is supported by the measurement of charging and discharging OCV curves, as described later. The effect of an instantaneous hysteresis as mentioned in [1] is not considered here.

Extended Kalman Filter for Battery SoC Estimation
The proposed model is nonlinear. In particular, the hysteresis voltage does not depend linearly on the current i. v oc (q) is also non-linear. This is why the original Kalman filter [23] cannot be used here. The extended Kalman filter (EKF) can handle these nonlinear dependencies [9]. The standard algorithm of the EKF is shown in Table 1. Here, predicted quantities are labeled with a hat (ẑ), and corrected variables with a tilde (z). u and y are input and output values, and therefore are not labeled.
For applying the EKF to SoC estimation, the general system description of the EKF is compared with the battery model and equivalents are identified. The comparison in Table 2 demonstrates this process.
After the identification of these equivalents, the matrices needed for the EKF algorithm can be derived. For a shorter notation, the following expressions are defined: Then the EKF matrices A, B and C are obtained, as stated by Equations (8)- (10).
The measurement noise matrix R is found as the precision of the voltage measurement: The determination of the process noise matrix Q[k], which is defined as Q[k] = Cov(z[k]) is a harder task. A possible approach is presented in Section 3.1. Kalman gain:

Useful Extensions to the EKF Algorithm
For a well-performing EKF, the tuning and initialization of the filter is critical [21,24]. The relevant matrices for tuning are the measurement noise R[k] and the process noise Q[k]. For the initialization, the initial statex[0] and its errorP[0] have to be set [21].
The determination of R[k] is already presented in Equation (11). However, the standard EKF algorithm presents no method for the derivation of the remaining variables Q[k] and The following extensions give methodic approaches for the tuning and initialization of the EKF. In Sections 3.1 and 3.2, the Q matrix is derived from the errors of the model parameters and the noise of the current measurement. The setting ofP[0] andx[0] is described in Section 3.3 in detail.

Determination of the Process Noise from Parameter Errors
No model fits reality perfectly. The process noise matrix Q quantizes the inaccuracies in the state equation of the given model. The origins of these inaccuracies are improper parameter values or the structure of the model [24].
In other words, the Q matrix indicates how precisely the state z[k] can be predicted when the state z[k − 1] is known. Hence, Q is added to the system state error P at each time step (see Table 1). Q significantly impacts the strength of the EKF's correction of the state prediction.
An appropriate choice of the process noise matrix Q is a difficult but important task [24]. The authors of [24] remark that Q is often not systematically determined in EKF designs. In many implementations of an EKF for SoC estimation [7,[10][11][12], this matrix is established experimentally.
However, an empirical choice of the design matrices cannot guarantee a working filter and cannot be generalized for other models. Therefore, this paper describes how to systematically derive Q based on the method described in [24].
According to [24], the Q matrix can be derived from the covariance of the parameters in the model. For the mathematical description, the parameters are summarized in a vector p which is for the model used here. The associated covariance matrix is called Q p . Then, the process noise covariance is obtained as where Applying this definition of J to the model used here results in the matrix shown in Equation (15).
Since J can change at every iteration, depending on the current and the system state, the Q matrix can also change. The notation [k] is therefore necessary.
A variable Q matrix is more sensible than a constant, as used in [7,[10][11][12]. Now the Kalman filter knows that the state of the system is known more precisely in certain situations. For example, if i[k − 1] = 0, most of the entries in J disappear. This is reasonable: If no current flows, v R 0 is exactly zero, without any uncertainty. The hysteresis voltage is . The model uncertainty of each RC voltage decreases in time as the voltage itself decreases. This behavior is only represented by an adaptive Q matrix.
Finally, for the calculation of the process noise matrix Q[k], just the parameter covariances Q p are required. The parameters are assumed to be uncorrelated. The standard deviation or uncertainty of a parameter x is denoted by σ x , the variance σ 2 x . The parameters are assumed to be uncorrelated. Then the parameter covariance matrix Q p is obtained as

Current Error Contribution to the Process Noise
Another source of error in the prediction of z[k] from z[k − 1] is the current. In the real battery system, the current is gained by a noisy measurement. The error of this measurement is one of the reasons why simple Coulomb-counting SoC estimators have poor long-term accuracy [9].
There exists no covariance matrix for the input variable in the theoretical description of the EKF like the R matrix for the measurement variable. Instead, the Q matrix is meant to contain all uncertainties of the system equation, including input errors implicitly.
For the systematical derivation of the Q matrix, the current error has to be included computationally. For this purpose, [25] propose a method for linear Kalman filtering with noisy input variables. A similar derivation can be performed for the extended Kalman filter. Its idea is analogue to the considerations of [24] regarding the derivation of the Q matrix from parameter errors, and is presented in the following. Let By Taylor series expansion and omission of higher-order terms, the state equation can be rewritten as This results in the following calculation of the Q matrix: where Q std [k] is the process noise without input (or current) error consideration. Using a current sensor with precision σ i (i) (the precision can depend on the current in general), the matrix S[k] is for the proposed battery model and the definition of the input variable vector u. The process noise is finally calculated as For a better understanding, the entry Q 00 is regarded, which gives the uncertainty of the prediction of the charge q: For Q 00 = 0, the Kalman filter could not significantly correct the charge q predicted by Equation (2) and hence produce almost the same result as a Coulomb counter. By the proposed Q matrix, the current error determines how freely the Kalman filter can deviate from the Coulomb-counting prediction.
In [21] the authors propose a similar derivation of Q from the current error. In contrast to this work, they assume perfect models with no parameter errors, and therefore do not regard the first part of the Q matrix calculation according to Section 3.1.

Initialisation
For the initialisation of the EKF algorithm, the system statez[0] and its errorP[0] have to be estimated. The initial state might be well-known under certain conditions, whereas the error of the initial state is quite hard to guess.
It is useful to start the EKF in a defined state, for example after a long resting time. Then the best estimation for the initial state is where q 0 is chosen such that v oc For an estimation of the initial state errorP[0], a method described in [24] can be applied. This method is also used in [21]. The Ref. [24] supposes to estimate an upper limit z u and a lower limit z l for the initial system state and calculate the initial state error as The limits z u , and z l will be established in the following equations. Hereby, it is assumed that i[0] = 0 and the system was in rest for ∆t rest before the initialization.
The only state that can be determined precisely is v R 0 [0] = 0 due to i[0] = 0. That means the upper and lower limits for this state are 0.
In general, the RC elements are not completely discharged at initialization, resulting in an error on the estimation v R j [0] = 0. Let i max be the maximum allowed absolute value for currents in the system. Then the limits for the initial RC voltages are Independent of the current, the hysteresis voltage varies between M and −M, so that the limits can be estimated as The most difficult state is q, since it is obtained from the assumption that the measured voltage at initialization is OCV, as the other voltages v R 0 , v R j and v h are all estimated to be 0. The error of q[0] is a consequence of the errors of the other states. For the derivation of the limits q min [0] and q max [0], Equation (6) is regarded: The limits for q can then be estimated as: Finally, the limits z l and z u are obtained as and can be used in Equation (35) for the calculation of the initial state errorP[0].

OCV Measurement
Before the Kalman filter can work with the model, the relationship v oc (q) must be known. This relationship is called the OCV curve, and will be implemented as a lookup table (LUT). A common method for OCV measurement is pulsed charging and discharging [26]. Starting from SoC = 100 %, the battery is discharged in small steps. After each current pulse, the battery cell is allowed to rest for a long time. During this phase, the RC voltages decrease to zero. The procedure is repeated until the battery is empty. The voltages at the end of each resting phase yield the OCV discharge curve. Afterwards, charging pulses and resting phases are executed until the battery is full again. The resting voltages yield the charge OCV curve.
The alignment of the two OCV curves is reached by a correct setting of the coulombic efficiencyη. Thus, the OCV measurement can already give an estimation of this parameter if the current was measured precisely. Figure 3 shows the results of an exemplary OCV measurement. In each step, a charge of 1 Ah is charged or discharged. The resting time after each pulse is 1 h.
The maximum polarization voltage M of the hysteresis element is defined as half the difference of charge and discharge OCV curves and can be extracted directly from the OCV measurement. It can then be implemented as LUT M(q).

Model Parameter Identification
The second necessary characterization step is the estimation of the parameter values. For this purpose, measurement data of the cell need to be collected. Figure 4 shows a possible experiment.  The experiment consists of two phases. First, one or multiple chirp signals bring the battery to a defined state [1]. The effect of these frequency sweeps is that the RC voltages v R j In the second phase, a rectangular current profile is run. Then the model is fit to the measured voltage. This is done by minimizing the residual sum of squares (RSS) between the predicted cell voltagev (according to the model Equation (6)) and the measured voltage v: min For the minimization, common direct search methods may be used. A popular option is the Nelder-Mead algorithm. MATLABs fminsearch is a standard implementation [27].
Since the parameters may vary with SoC, temperature or aging the parametrization experiment should be repeated under different conditions and with multiple cells. The parameters may then be implemented as LUTs. The precision of the parameters is obtained as the standard deviation of the parameter values of identical cells at equal conditions. When the parameters are not implemented as LUTs, the standard deviation calculations should consider the system's complete operating range.

Step-by-Step Guide to Implementation
This chapter gives an overview of the necessary steps to get a working EKF implementation for SoC estimation. It summarizes the tasks that have to be performed before and during operation of the battery system. Figure 5 shows this as a step-by-step guide.

Model parameters
Fit model to measured data to get values and standard deviations for R 0 , R j , τ j and γ at different conditions for many cells (Section 4.2).
Sensor accuracy Look at the datasheets of the utilized sensors for voltage and current measurement to determine the precisions σ v and σ i (i) (Equation (11) and Section 3.2).

Lookup tables or averaged values
Implement v oc as LUT, M and R 0 , R j , τ j , γ and their standard deviations as LUTs or averaged values (Section 4.2). (12)) and standard deviations in the Q p [k] (Equation (22)) matrix. Use linear interpolation.

Correction
Correction step of the standard EKF algorithm (Table 1).

Experimental Setup
The battery cell which will be evaluated as an example in the following is part of a battery system. The system consists of 96 cells that are connected in series and partitioned into four modules. These modules are organized in a master-slave principle. Each module contains an Infineon AURIX Tricore microcontroller that executes the battery management system (BMS) software, including the proposed EKF algorithm. To connect the battery to the grid, a Delta Elektronika SM 500-CP-90 inverter is used. The setup is shown in Figure 6.
The BMS measures the cell voltage by an LTC6811 battery monitoring chip that provides a precision of σ v = 0.1 mV. It measures the cell current by a VAC T60404 fluxgate current sensor with a precision of σ i = 100 mA. The offset of the current sensor is compensated roughly at startup.

Evaluated Cell
The cell under test is a NMC pouch cell. Its OCV curve is presented in Figure 3. The following cell parameters and standard deviations have been experimentally determined during the preparation of the EKF (see Figure 5): R 0 = 1.31 mΩ ± 0.20 mΩ R 1 = 0.72 mΩ ± 0.10 mΩ In the tested version, v oc and M are implemented as LUTs. The standard deviation of M is set to σ M (q) = 0.2 · M(q). Due to their comparatively small variation in the examined operating range, the remaining parameters are implemented as constant values with constant standard deviations.
With the safety limits v min = 3.058 V and the v max = 4.092 V, the usable capacity Q cell is found from the OCV curve as Q cell = 38.16 Ah.

Experiment Procedure
The procedure of the experiment is shown in Figure 7. It mainly consists of a rectangular charge and discharge current profile. After every four repetitions of this profile, the battery is discharged for a longer period and then held in rest for the same time. As a result, the voltage captured at the end of the resting phase can be assumed to be discharged OCV, which yields a reference point for the SoC.
Charge to SoC = 100 % Initialization of the EKF algorithm Repeat 4x:

Comparative Analysis
The results of the proposed EKF SoC estimator (E1) will be compared to three other estimators: (E2) an EKF with a roughly tuned process noise matrix Q (E3) a Coulomb counter with the internal current sensor and the assumed capacity of Q cell = 38. 16 Ah. (E4) a Coulomb counter with the precise current sensor of the inverter and cell capacity correction.
E4 is assumed to be the best estimation, and therefore works as the reference SoC. The capacity correction is performed after the experiment based on the measured data. Note that E4 is therefore not applicable for real-time estimation. In contrast, the other three estimators are run in real-time.
For E2, the following Q matrix is used: = diag(4, 2 · 10 −5 , 2 · 10 −5 , 1 · 10 −4 , 2 · 10 −8 ) Note that in our implementation, q is in Ah, whereas the voltages are in mV, which leads to a comparatively large value in the first entry. The order of magnitude for this entry is adapted from [10,12], which proposes a value of 0.01 that is a squared SoC value (variance of the state). This means 10 % standard deviation in SoC. However, the Ref. [7] chooses a SoC standard deviation of 0.32 %. For comparison, an averaged value of 2 Ah (≈ 5 % SoC) is selected and squared to get 4 (Ah) 2 as the first entry in Q. The magnitudes of the voltages v R j strongly depend on the cell-specific resistances, and therefore cannot be adapted from the literature directly. They are roughly estimated from the resistance values and an average current (R j · i avg ). The hysteresis voltage is not mentioned in any of the referenced papers, but it is in the order of 10 mV. The estimation of v R 0 is quite exact, so the corresponding entry in Q is calculated from the inaccuracy of the current sensor (R 0 · σ i ).

Results
The cell voltage and current measured by the BMS are shown in Figure 8. At t ≈ 15 h, one of the cells in the battery system is fully discharged. Therefore, the tested cell cannot be discharged lower than 3.2 V. Furthermore, the last 15 A discharge interval (according to Figure 7) cannot be fully executed. Therefore, only four OCV capture points are recorded. Figure 9 presents the SoCs estimated by the above-mentioned estimators, E1-E4. Additionally, it shows the SoCs calculated via the OCV discharge curve (see Figure 3) evaluated at the OCV capture points. The error bars result from the possibility that the hysteresis element is not fully polarized after a 30 min discharge. The points show the case of full polarization.  The high-accuracy current sensor measured a charge difference of 35.83 Ah between t = 0 h and the fourth OCV capture point at t = 13 h. The charge difference between these two points in the OCV curve ( Figure 3) is only 34.11 Ah. Therefore, for the gauging of the reference E4, we assume a cell capacity of 40.09 Ah instead of 38.16 Ah, as used by E1 to E3.
For better visibility of the results, the errors of E1-E3 compared to the reference E4 are depicted in Figure 10. 6.6. Discussion 6.6.1. Validity of the Reference In the literature, the reference, often called true SoC or real SoC, is gained by Coulombcounting [10,11,13,21]. This may work for a high-precision current sensor and a perfectly known cell capacity. Capacity and SoC estimation experiments have to be conducted under equal conditions. This is done by monitoring the cell in a controlled temperature chamber (in [21]). In the present system, the cell is in a different condition during the experiment than during the recording of the OCV-SoC dependency described in Section 4.1.
The correction is performed with the fourth OCV point that is captured during the experiment. The fourth OCV point has a small error bar due to the steep region in the OCV curve. Therefore, the reference has a small uncertainty. E4 also hits the three other OCV capture points, which confirms the gauging. The capacity difference of nearly 2 Ah can be explained by the higher temperature in the cell compared to the OCV measurement, which is 29°C during the experiment. A possible error of the current sensor also accumulates into the corrected cell capacity. Figure 9 shows that the proposed algorithm E1 has the highest accuracy. It only diverges a little from the reference, and hits all OCV capture points at least within each error range. Besides this, Figure 10 confirms the quality of this estimator. The error of the proposed EKF is roughly in a range of ±1% and clearly outperforms the two compared estimators, E2 and E3.

SoC Estimation Algorithms
E2 shows an acceptable performance, with an error of a range of ±4%. However, significant fluctuations occur, especially after the resting phases. The performance of E2 might be improved, when the constant Q matrix is determined more elaborately, such as by trial and error.
The estimation of the Coulomb counter with the internal current sensor (E3) shows the lowest error in the beginning. However, the error grows until the end of the experiment. This is the expected behaviour of a Coulomb-counting estimator. The errors have two origins: the accumulated error in the current measurement and the error of the assumed cell capacity.

Conclusions
This work shows the implementation process of a state of charge (SoC) estimator for lithium-ion batteries based on an extended Kalman filter (EKF) using an equivalent circuit model (ECM). The used model includes a hysteresis element to represent the difference between charge and discharge behavior.
For the adjustment of the EKF, methodic approaches are presented. The EKF tuning can be directly derived from sensor and model accuracy. This accelerates development by omitting effortful empirical determination of the process noise matrix Q. All required steps are presented in a step-by-step guide.
The proposed EKF is experimentally compared to a roughly tuned EKF. It achieves a maximum error of ±1 % and good stability. It outperforms the roughly tuned EKF, which shows a maximum error of ±4 % and significant fluctuations. The difference in the performance demonstrates how sensitive the EKF reacts to the process noise matrix. Therefore, careful EKF tuning is vital.

Outlook
The presented method has been designed to be applicable to other cell chemistries, ECMs, and sensors. This should be experimentally validated in a wide range of operational conditions. This could include increased currents, a wide temperature range, and extended runtimes to facilitate aging processes. We expect that the accuracy of the SoC estimator could be further increased by implementing LUTs for more model parameters instead of using averaged values.

Conflicts of Interest:
The authors declare no conflict of interest.

Abbreviations
The following abbreviations are used in this manuscript: BMS