# IoT Implementation of Kalman Filter to Improve Accuracy of Air Quality Monitoring and Prediction
## 4 Results
### Kalman Filter Algorithm Implementation
#### 4.2.3 Setting Parameters
Finally, all the predicted values of a posteriori estimate \(x_{k}\) can be obtained, from the initial value to the convergence process, i.e.,:  
\[x(Q_{n})_{96\times 1}=[x_{1},x_{2},\cdots,x_{96}] \tag{8}\]  
(4) Calculate RMSE. In the case where \(Q\) takes a different value \(Q_{n}\), the RMSE (root-mean-square error) between each predicted value \(x_{k}\) and the corresponding true value is calculated:  
\[RMSE(x,\hat{x})_{Q_{n}}=\sqrt{\frac{1}{m}\sum_{i=1}^{m}(x_{i}-\hat{x}_{i})^{2}} \tag{9}\]  
Figure 6: Prediction trend of CO concentration by Kalman Filter algorithm when \(Q=0\) or \(Q=1\). (a) \(Q=0\); (b) \(Q=1\). where \(x\) is the true value, i.e., the data of the monitoring site, and \(\hat{x}\) is the predicted value calculated by the KF. Then for each \(Q\) value \(Q_{n}\), a corresponding \(RMSE(x,\hat{x})_{Q_{n}}\) is output. (5) Compare \(RMSE(x,\hat{x})\). In step (4), since \(\hat{x}\) contains the predicted value in the non-converged state of the system, the RMSE value is relatively large during the parameter tuning process. In addition, for each type of pollutant, the original RMSE has different ranges, since the absolute value calculated from the predicted values and true values is different. Therefore, the \(RMSE(pollutant)\), where the \(pollutant\) represents the SO\({}_{2}\), NO\({}_{2}\), CO, O\({}_{3}\), PM2.5, PM10, respectively, is normalized by the commonly used min-max normalization method to have the same range. After normalization, each \(RMSE(pollutant)\) range is between (0,1), resulting in intuitive comparison. Figure 7 reflects the trend of normalized RMSE of six types of air pollutants during the process of \(Q\) search. The smaller the value of RMSE, the less unconverted predicted value will be reflected in the side, which indicates that the faster the model converges and the better the fitting effect. At this time, the value of \(Q\) is more favorable to predicting the pollutants (the trend of different pollutants is different, so the optimal value of \(Q\) for each type of pollutant will be different. According to the model built in this paper, the more stable the change, the smaller the optimal value of \(Q\) will be). Figure 7: RMSE for each pollutant with different \(Q\) value in the search interval. (**a**) SO\({}_{2}\); (**b**) NO\({}_{2}\); (**c**) CO; (**d**) O\({}_{3}\); (**e**) PM2.5; (**f**) PM10. (6) Best \(Q\)  
From Figure 7, we can get the optimal value of the process error \(Q\) as follows:  
\[Q_{best}(SO_{2},NO_{2},CO,O_{3},PM2.5,PM10)=[0.467,\ 0.273,\ 0.089,\ 0.572,\ 0.151,\ 0.133] \tag{10}\]  
In the tuning process, when \(Q\) is taken to \(Q_{best}\), the iterative convergence process for each type of pollutant concentration that uses the KF algorithm for prediction is shown in Figure 8.  
It can be seen from the figure that when \(Q=Q_{best}\), the KF algorithm can converge after about 60 iterations, and the predicted result after convergence can more accurately reflect the change of the true value. (7) Test \(Q\). Taking the predicted CO concentration as an example, it is tested whether the optimal prediction effect can be obtained when the optimal value of \(Q\) is taken. From step (6), we can have \(Q(CO)=0.089\) and use the KF algorithm to predict the concentration of CO at 0:00 on 16 February 2019. The  
Figure 8: The prediction trend of the Kalman Filter algorithm for each pollutant concentration with optimal \(Q\) value. (a) SO\({}_{2}\); (b) NO\({}_{2}\); (c) CO; (d) O\({}_{3}\); (e) PM2.5; (f) PM10. convergence process of the algorithm is shown in Figure 9. The plus sign is the observation value obtained by sensors sampling 100 times for CO concentration within 10 min from 23:00 to 23:10 on the 15 February 2019. The blue line is the process of the KF algorithm combined with error observations for 100 iterations. In this test, the last value after convergence of the blue line (model) will be used as the predicted value at 0:00 on the 16 February 2019.