1 Introduction

In the past three decades, the development of Global Navigation Satellite System (GNSS) has undergone revolutionary changes. At present, mature satellite positioning systems such as GPS (Global Position System), Global Navigation Satellite System (GLONASS), Galileo Satellite Navigation System (GSNS) and Beidou Navigation Satellite System (BDS) have made positioning in outdoor environment no longer a problem. Unfortunately, GNSS cannot operate where satellite signals of sufficient quality cannot be received, for example in most buildings and in dense urban environments (due to the urban canyon effect). Nowadays, with the acceleration of urbanization, people will spend 80% of their time indoors [1]. Especially when people are in large-scale complex indoor buildings, such as large shopping malls, indoor amusement parks, libraries, museums and large supermarkets, because of the large area and complex structure, it is difficult for people to quickly perceive their location from the surrounding environment. Therefore, the application of location-based Indoor Location-Based Services (ILBS), such as indoor road finding, emergency evacuation, mobile guide (shopping or exhibition), games and advertisements, has a broad market.

Driven by the demand, ILBS has become the focus of academic and commercial research and development [2]. Compared with outdoor positioning, indoor environment has a small range of activities, high density and high frequency of human activities. Moreover, the accuracy of indoor location is higher than that of outdoor location. Therefore, the Indoor Positioning System (IPS) is facing great challenges. Fortunately, with the rapid development of wireless communication technology, internet technology and microelectronics technology, smart phones, wearable devices and other mobile terminals equipped with Magnetic, Angular Rate and Gravity (MARG) and other external environment sensing sensors have been widely used. Up to now, different position sensor technologies have been developed and tested with smart phones, such as Wi-Fi [3], RFID [4], Bluetooth [5], geomagnetism [6] and ultra wideband (UWB) [7]. Location methods such as proximity sensing, least squares, dead reckoning and pattern matching (such as fingerprint recognition) are used. Among them, the matching method based on Geomagnetism has attracted many researchers' interest because of its advantages of no additional auxiliary equipment and high positioning accuracy. The advantages of using magnetic fingerprint indoors compared with other media are firstly that no special infrastructure needs to be deployed and the positioning accuracy is high. In particular, with the popularity of smart phones in recent years, devices such as magnetic sensors and acceleration sensors on smart phones have opened up new research directions for fingerprint-based research. Secondly, the collection of geomagnetic fingerprint only needs ordinary smart phones and geomagnetic fingerprint is naturally formed. Compared with WiFi fingerprint database, geomagnetic fingerprint database has better stability and does not rely on other special basic equipment. However, the magnetic fingerprint composed of a single magnetic field modulus only consists of a few characteristic parameters, so there are the problems that the magnetic fingerprint data is not unique in large indoor environment [8].

In order to improve the accuracy of geomagnetic positioning, researchers put forward different improvement plans. The indoor positioning method based on Dynamic Time Warping (DTW) measures and compares the similarity of two magnetic field sequences in a given time period to reduce the positioning error [9,10,11]. Although this method can achieve high positioning accuracy, it is difficult to work in large-scale environment because it is limited by specific walking routes. In addition, the fusion method of PF (Particle Filter) and PDR (Pedestrian Dead-Reckoning) is also used to improve the positioning accuracy [12,13,14,15]. Literature [16] took LSTM networks real-time indoor localization using smartphone magnetic. Literature [17] used Smartphone based indoor localization using permanent magnets. Obeidat et al. [18] introduced indoor localization techniques and wireless technologies. Literature [19] proposed evolutionary optimization strategy for indoor position estimation using smartphones. Roy and Chowdhury [20] introduced machine learning techniques for indoor localization and navigation system. Literature [21] fused WIFI with inertial sensors using LSTM networks. However, the traditional PF scheme will generate particles in a large area, which leads to the blindness of particle positioning. In addition, in the process of resampling, with the increase of the number of iterations, the diversity of particles will gradually reduce. When PDR has a large error, it is easy to lead to positioning failure.

In order to overcome the above problems that the magnetic fingerprint data is not unique in large indoor environment and the blindness of particle positioning, the paper proposed the algorithm that PF and PDR are combined, which the error model is set up by using the result data of statistical PDR so that the particles generated each time are determined by the error model, thus reducing the generation area of particles. In addition, unlike the iteration of particles, the standard regenerates particles in each iteration, thus increasing the diversity of particles. The position estimation is obtained by the weighted average of particles. Experimental results show that the positioning method has a high positioning accuracy compared with the current research.

2 Magnetic-Based Indoor Localization

As shown in Fig. 1, it is the principle of our indoor location scheme based on magnetic fingerprint. In this scheme, PDR results are used as the state model of particle filter, and the matching results of magnetic map and real-time magnetic fingerprint are used as the prediction model of particle filter. First, the offline magnetic map of the experimental environment is established in advance by using the magnetometer in the smartphone. After that, every time a user walks with a smart phone, the sensor in the smart phone will collect the magnetic data and user gait data in real time and hand them to particle filter for magnetic fingerprint matching and positioning.

Fig. 1
figure 1

Schematic diagram of magnetic-based indoor localization

2.1 Orientation Estimation

Gyroscopes and magnetometers are used to estimate the direction of travel of the equipment. Gyroscope can provide accurate attitude of equipment under high dynamic conditions. However, it will increase the drift error with time. The magnetometer is affected by the magnetic distortion in the indoor environment. We use the attitude algorithm proposed by [22] to obtain heading information, namely orientation estimation as shown in Fig. 2. The algorithm first calculates the attitude angle \(\phi_{ma}\) through the combination of magnetometer and accelerometer, and makes a difference with the attitude angle \(\phi\) of the previous moment, as shown in Eq. (1):

$$\phi_{err} = \phi_{ma} - \phi$$
(1)
Fig. 2
figure 2

Improved complementary filter

Then, \(\phi_{err}\) is used as the input of PI (Proportional Integral) controller to correct the attitude angle, as shown in Eq. (2):

$$\phi_{a}^{^{\prime}} = \phi_{ma} - K_{p} \phi_{err} + K_{I} \int {\phi_{err} dt}$$
(2)

where \(K_{p}\), \(K_{I}\) are PI parameters, which \(K_{p}\), \(K_{I}\) and alpha choose in [22]. Finally, the solution Eq. (3) of attitude angle is obtained by integrating the output of gyroscope:

$$\phi (t) = (1 - \alpha )(\phi (t - 1) + \omega \cdot dt) + \alpha \phi_{\alpha }^{^{\prime}}$$
(3)

where α is the time constant related to the response speed and sampling frequency, which defines the reliability of the attitude angle after correction and the attitude angle of the gyroscope output. However, Eqs. (2) and (3) are continuous in time, and should be discrete time to be used. On the one hand, the pitch, roll and heading angles calculated by the method are used to calculate the attitude matrix of the equipment to calculate the magnetic fingerprint; on the other hand, the heading angle is used to obtain the movement direction information of the pedestrian in PDR.

2.2 Gait Detection

According to the action characteristics of human walking, when the pedestrian holds the equipment equipped with accelerometer, the output modulus of accelerometer presents a periodic waveform. Therefore, we can use this feature to detect the gait of pedestrians. In this paper, the peak value of the detected acceleration is used to calculate the steps. However, because the accelerometer is affected by noise, the output will be accompanied by a certain amount of noise. In order to obtain a better output value, we will filter the output low-pass, and get a smooth unimodal curve as shown in Fig. 3. In addition, when the pedestrian holds the mobile phone, it will inevitably make the mobile phone jitter, resulting in the peak value of the accelerometer output. Therefore, in order to eliminate the jitter error, we set the peak detection threshold, as follows Eq. (4):

$$\left\{ \begin{gathered} T_{Tl} < \Delta T < T_{Th} \hfill \\ acc > A_{Th} \hfill \\ \end{gathered} \right.$$
(4)

where, represents a walking cycle; acc is the filtered acceleration value of the output modulus of the accelerometer, which upper cut-off frequency is 10 Hz; TTl and TTh are the upper limit of time and the lower limit of time respectively, which represent the walking frequency range of pedestrians; ATh is the peak threshold of acceleration. When a peak is detected on the output of the accelerometer, and the peak meets the above two conditions, we think that the pedestrian has made a step forward.

Fig. 3
figure 3

Acceleration filtering results

For the detection of wave crest, we can use the following condition Eq. (5) to determine:

$$(acc[k] < acc[k - 1]\& \& acc[k - 1] > acc[k - 2])$$
(5)

where k is discrete time. For the detection of walking period ΔT, we can get it by calculating the time interval between two troughs. However, as mentioned above, when pedestrians walk with mobile phones, hand shaking and other situations will inevitably occur, resulting in the accelerometer output error peak and trough. For the error peak, we use the peak detection threshold to avoid the peak error detection. Therefore, we use the same method to set a threshold value for the detection of wave trough, as follows Eq. (6):

$$acc < A_{Tl}$$
(6)

where, ATl represents the threshold value of wave trough detection. Therefore, when the peak and valley appear between the two thresholds, we can eliminate the error very well. At the same time, in order to avoid the misjudgment when the error value of the peak and valley appears within the range of the threshold limit, we think that when two troughs meet the threshold limit, but the peak does not meet the threshold limit, we only record the time of the second trough. In the case of wave crest, it is similar to wave trough. When two peaks meet the threshold limit, but the valley does not meet the threshold limit, we only mark the last peak.

For the estimation of step size, we give it by empirical model, as shown in Eq. (7):

$$d_{length} = K \cdot \sqrt[4]{{acc_{\max } - acc_{\min } }}$$
(7)

where dlength is the step size of pedestrian, accmax and accmin represent the maximum and minimum acceleration in one step respectively, K is the training constant related to user [23], which is represented by Eq. (8).

$$K = \frac{{\sqrt {(x_{1} - x_{2} )^{2} + (y_{1} - y_{2} )^{2} } }}{{\sum\nolimits_{i = 1}^{Step\_Count} {\sqrt[4]{{acc_{\max } - acc_{\min } }}} }}$$
(8)

where (x1 − x2) and (y1 − y2) are the start and end points of the line track respectively.

The implementation of gait detection algorithm is shown in Table 1.

Table 1 Gait detection algorithm

2.3 Particle Filter

Particle filter algorithm is used for matching and positioning. First, we need to input the initial position, and generate a certain amount of particles with the initial position as the center. After that, every time a gait is detected, the filter regenerates the particles according to the direction estimated by the complementary filtering algorithm and the step estimated by the gait detection algorithm according to the state model of Eq. (9). Finally, the magnetic fingerprints on all particle positions are compared with the measured magnetic fingerprints to generate weights for all particles, so as to obtain the position estimation by weighting.

$$\left[ \begin{gathered} x_{k} \hfill \\ y_{y} \hfill \\ \end{gathered} \right] = \left[ \begin{gathered} x_{k - 1} \hfill \\ y_{y - 1} \hfill \\ \end{gathered} \right] + \left[ \begin{gathered} \sin (\theta + \theta_{n} ) \hfill \\ \cos (\theta + \theta_{n} ) \hfill \\ \end{gathered} \right] \times (d_{length} + d_{n} )$$
(9)

where, \((x_{k} ,y_{y} )\) represents the estimated position of the current movement, \((x_{k - 1} ,y_{y - 1} )\) is the result of the last movement. θ is the direction of this pedestrian movement, and dlength is the step length. θn and dn are the error distribution of the moving direction and the moving step length respectively, which are obtained from the analysis of the results of course estimation and gait detection.

The improved particle filter algorithm is shown in Table 2.

Table 2 Improved particle filter algorithm

In the traditional particle filter scheme, no error was added to the state model constructed according to the direction of movement and the step size, and the particle update was always generated by the calculation of the last particle passing through the state model. If this method limits the particle range, and when the pedestrian direction error and step size error is large, the filter will diverge. If the particle range is not limited, the filter divergence will also result from the insufficient resolution of the magnetic map in a large range. In addition, this method will make particles gather in one place each time due to resampling, thus reducing the diversity of particles. With the increase of iteration times, the filtering divergence will be caused by the accumulation of errors.

Therefore, in the classic particle filter scheme, a group of particles is used to estimate the posterior distribution of the system state in the whole region, which leads to the inherent blindness. Moreover, with the increase of the number of iterations, the diversity of particles is limited in the region of convergence. When the gait detection error is too large, it will lead to positioning failure. In order to reduce the range of particles and increase the diversity of particles, this paper adds error models for direction and step size. In addition, each particle update is generated by the estimated position according to the state model of Eq. (9) to generate the particle distribution conforming to the error model. In the work of this paper, each iteration uses Eq. (9) to regenerate particles based on the previous results (xk-1, yy-1). Therefore, the diversity of particles in each iteration is guaranteed, and the error distribution of moving direction and moving step size limits the region of particle generation, which reduces blindness and the number of particles to improve the calculation efficiency. Its positioning process is shown in Fig. 4. The gray circles in Fig. 4 indicate where the main particles are generated.

Fig. 4
figure 4

Schematic diagram of particle filter process

For the calculation of fingerprint similarity, Euclidean distance and Manhattan distance can be used. Euclidean distance has lower computational complexity. As follows Eq. (10):

$$w_{i} = \exp ( - 0.5 * (F - F_{i} )^{T} (F - F_{i} ))$$
(10)

In Eq. (10), F is the magnetic fingerprint obtained in real time, Fi is the magnetic fingerprint of particle \(P_{i}\) obtained from the magnetic map. wi is the particle weight.

After obtaining the weight of the particle, we resample the particle. For the last particle, we take its geometric median as the position estimation result of the filter, as shown in Eq. (11):

$$\tilde{x} = {\raise0.7ex\hbox{$1$} \!\mathord{\left/ {\vphantom {1 N}}\right.\kern-\nulldelimiterspace} \!\lower0.7ex\hbox{$N$}} * \sum\nolimits_{i = 1}^{N} {x_{i} } ,\;\;\tilde{y} = {\raise0.7ex\hbox{$1$} \!\mathord{\left/ {\vphantom {1 N}}\right.\kern-\nulldelimiterspace} \!\lower0.7ex\hbox{$N$}} * \sum\nolimits_{i = 1}^{N} {y_{i} }$$
(11)

3 Experiment and Result Analysis

In this paper, Xiaomi Mi 6 is used as the experimental equipment, which has ICM20690 6-axis inertial sensor and AK09916 magnetic field sensor. In addition, the positioning experiments in this paper are all done by hand-held smart phones for pedestrians. The specific handheld mode is shown in Fig. 5.

Fig. 5
figure 5

Pedestrian handset

In order to collect experimental data, we invite some volunteers in our laboratory and school. In consideration of the comprehensiveness of data, various genders, ages, and heights of volunteers are guaranteed. In total, 20 volunteers participate in data collection. The tested results are averaged. Based on [23, 24], a smartphone can obtain heading of geomagnetic with many kinds of position in human body. However, [23, 24] proposed method that cannot meet all kinds of situation application, such as real time, which need at least 5 steps obtain human heading. In this paper, in order to quickly verify the effectiveness of the proposed method, and considering the common situations of indoor navigation or positioning of mobile phones, it is assumed that smartphones adopt longitudinal screens, with y direction as the pedestrian's forward direction, x direction as the pedestrian's right direction and z direction as the upper direction, as shown in Fig. 6. In Fig. 6, geographic coordinate system and pedestrian coordinate system are specified in the same direction, that is, their coordinate systems coincide. The advantage of doing this is to reduce the multiple transformations of the coordinate system. As described in the reference [23, 24], it is difficult to determine the pedestrian heading under the circumstance of holding a smart phone and accompanying walking. However, through our research, it is found that it is much easier to determine the pedestrian heading if the pedestrian holds a smartphone and the smartphone direction is fixed in the same direction as the pedestrian, which is also the reason why this method is adopted for experimental verification in this paper. Next, as long as we convert the carrier coordinates into the geographic coordinate system, we can realize the transformation of the position of smartphones and pedestrians in the geographic coordinate system.

Fig. 6
figure 6

Smartphone (the body coordinate system), pedestrian and the earth coordinate system (navigation coordinate system)

The acquisition of magnetic fingerprint (during the construction of magnetic map and the matching and positioning) only refers to the collection of magnetic field data for a period of time to take the mean value, which should be specifically to convert the collected magnetic field data to the geographical coordinate system according to the attitude acquisition algorithm mentioned in the paper, so as to obtain the magnetic fingerprint. The attitude angle is calculated according to the quaternion algorithm from ICM20690. According to the pitch angle φ, roll angle θ and yaw angle ψ calculated by the algorithm, the transformation matrix of the body coordinate system (b system, namely smartphone) and the earth coordinate system (n system) is obtained. If the rotation sequence is z-y-x, the coordinate transformation matrix \(C_{b}^{n}\) is obtained, as shown in Eq. (12). The magnetic data can be processed by the proposed algorithm after the sample data are transformed from b system to n system based on Eq. (12) that are obtained from AK09916.

$$C_{b}^{n} = C_{\theta } C_{\phi } C_{\psi } = \left[ {\begin{array}{*{20}c} {\cos \theta } & 0 & { - \sin \theta } \\ 0 & 1 & 0 \\ {\sin \theta } & 0 & {\cos \theta } \\ \end{array} } \right]\left[ {\begin{array}{*{20}c} 1 & 0 & 0 \\ 0 & {\cos \phi } & {\sin \phi } \\ 0 & { - \sin \phi } & {\cos \phi } \\ \end{array} } \right]\left[ {\begin{array}{*{20}c} {\cos \psi } & {\sin \psi } & 0 \\ { - \sin \psi } & {\cos \psi } & 0 \\ 0 & 0 & 1 \\ \end{array} } \right]$$
(12)

We chose the third floor corridor (2.4 × 36m2) and Hall (25 × 18m2) of the West Building of Xidian University for the experiment as shown in Fig. 7.

Fig. 7
figure 7

Indoor corridor (left) and Hall (right) environment

3.1 Magnetic Map Construction

Before the construction of the magnetic map, we fixed the intelligent collection at a point, collected the magnetic field data for a period of time, and got the results as shown in Fig. 8.

Fig. 8
figure 8

Indoor corridor (left) and Hall (right) environment

It can be seen from Fig. 7 that the error of magnetic sensor obeys Gaussian distribution. Therefore, we take the mean value of the magnetic data as the magnetic fingerprint. We divide the whole experimental space into grids with an interval of 0.6 m. In each grid, we collect data samples of 3 s, and take their mean as fingerprint results. In addition, because the magnetic sensor of smart phone will be interfered by hard iron and other magnetic fields, we will move the mobile phone in the shape of 8 words before each experiment to achieve the calibration effect. Finally, we get the magnetic map of the corridor and hall as shown in Figs. 9 and 10.

Fig. 9
figure 9

Modeling results of hall magnetic map

Fig. 10
figure 10

Modeling results of corridor magnetic map

3.2 Gait Detection

In order to verify that the gait detection algorithm can provide effective gait information for particle filter, we carry out walking and running experiments on the gait detection algorithm. For walking test, we tested five groups of data, including 101 steps, 100 steps, 97 steps, 99 steps and 98 steps. The gait test results are shown in Table 3.

Table 3 Gait detection results in walking state

It can be seen from the test results in Table 3 that pedestrian walking state detection has achieved good results. The average error is less than 1%. For the error, we compared the acceleration curve, and found that when the pedestrian suddenly encountered the uneven road surface in the process of travel, the pedestrian would have an early warning response, which led to the pedestrian acceleration change is not obvious, and finally resulted in the error judgment of the algorithm. Generally, this error will only produce one step error. When the pedestrian returns to normal walking, it can continue to detect the walking state. For the particle filter algorithm based on gait detection results, its error can provide good state transformation information.

For running test, we also tested five groups of data, running 73 steps, 69 steps, 68 steps, 69 steps and 66 steps respectively. The gait test results are shown in Table 4.

Table 4 Gait test results under running state

The experimental results show that the average error of running test is slightly larger than that of walking test. However, for application, it can still provide more accurate gait detection results. In the same way, we compared the error results with the acceleration curve, and found that when the pedestrian running, the gait is more regular, and the acceleration changes obviously. However, due to the large variation of acceleration during running, in order to avoid misjudgment, the thresholds of wave crest and wave trough are increased appropriately. Because of the threshold, the acceleration changes little when we start, so we will miss the first step. Therefore, in the future work, how to adjust the threshold dynamically will be our research content. In general, our detection results can provide good walking state information, and can cooperate with the location algorithm to provide position estimation.

For the step size estimation, after data training, we can adjust K of the algorithm to get a step size with an error less than 5%. Therefore, in order to increase the diversity of particles, we set the step size error distribution dn as the Gaussian distribution with the mean value of 0 and the mean square deviation of 0.1 m.

3.3 Orientation Estimate

In orientation estimation test, we have carried out experiments for more than 200 s, traveling nearly 200 m in total, and conducted turning tests of 90°, -90° and 180°. In addition, we also recorded the estimated course using the uncalibrated gyroscope and the calibrated gyroscope. In the result display, we also compare the attitude angle of the equipment, including pitch angle and roll angle. The results are shown in Fig. 11.

Fig. 11
figure 11

Comparison of attitude calculation results

It can be seen from Fig. 11 that the improved complementary filter has a high accuracy, which can estimate the heading angle of the equipment well, and the real-time attitude information of the equipment obtained is also relatively accurate. After analyzing the error of heading information, we set the error distribution of moving direction \(\theta_{n}\) the Gaussian distribution with mean value of 0 and mean square deviation of 8°.

3.4 Particle Filter Localization

Choosing corridor and hall, we carried out three experiments: straight walking, turning walking and curve walking. At the same time, in order to better display the location results, we choose the magnetic field mode value as the location method of magnetic fingerprint for comparison. The positioning results are shown in Fig. 12 (straight walking), Fig. 13 (turning walking) and Fig. 14 (curve walking) respectively.

Fig. 12
figure 12

Positioning results with straight walking

Fig. 13
figure 13

Walking positioning results with turning

Fig. 14
figure 14

Curve walking positioning results

It can be seen from Figs. 12, 13 and 14 that the positioning results obtained by using the magnetic vector are more accurate, whether it is a straight walk, a turn walk or a curve walk positioning test, because it contains more characteristic information than the magnetic field modulus. Using the magnetic vector, we get an average positioning accuracy of 0.52 m, which can meet the basic indoor positioning needs.

3.5 Results Comparison

Among other indoor positioning methods based on geomagnetism, [25] put forward the idea of using the initial positioning error to determine the particle matching range to solve the divergence problem in particle filter. Through experimental verification, the accuracy of 0.62 m is obtained, but the accuracy is only based on the linear motion. Putta et al. [23] used the AHRS (attitude and heading reference system) to estimate the direction, and used the combination of particle filter algorithm and PDR to achieve an average positioning error of 0.75 m. Huang et al. [12] put forward an improved dead reckoning algorithm and magnetic map matching algorithm to achieve a positioning error of 2 m, which is better than the traditional PDR algorithm. Wang et al. [26] combined with Kalman filter and PDR to limit the particle area in particle filter algorithm, use particle weighted average to calculate the final position, and obtain a linear positioning error of 0.47 m. In contrast, these results only achieve excellent linear positioning results. Recently, [27] proposed a fusion indoor positioning method that integrates the PDR and geomagnetic positioning by using the genetic-particle filter algorithm, and achieved an average positioning accuracy of 1.72 m, which needs to be further improved. The compared results are shown in Table 5. Table 5 shows that the proposed method obtained perfect performance.

Table 5 Compared with other algorithms

4 Conclusion

In this paper, an indoor localization algorithm combining PF and PDR is proposed. The output of the improved gait detection algorithm is used as the state model of PF. In each iteration, the estimation error of the gait detection algorithm is more accurate to determine the particle range, so as to regenerate the particles. The proposed algorithm can improve the inherent blindness and optimize the particle diversity in the iterative process. By comparing the results of two kinds of magnetic maps, it is proved that using magnetic vector can obtain high positioning accuracy, and the average positioning accuracy is 0.52 m. In addition, the presented method can be embedded easily in existing edge/cloud/fog configurations.