A Fast Indoor/Outdoor Transition Detection Algorithm Based on Machine Learning

The widespread popularity of smartphones makes it possible to provide Location-Based Services (LBS) in a variety of complex scenarios. The location and contextual status, especially the Indoor/Outdoor switching, provides a direct indicator for seamless indoor and outdoor positioning and navigation. It is challenging to quickly detect indoor and outdoor transitions with high confidence due to a variety of signal variations in complex scenarios and the similarity of indoor and outdoor signal sources in the IO transition regions. In this paper, we consider the challenge of switching quickly in IO transition regions with high detection accuracy in complex scenarios. Towards this end, we analyze and extract spatial geometry distribution, time sequence and statistical features under different sliding windows from GNSS measurements in Android smartphones and present a novel IO detection method employing an ensemble model based on stacking and filtering the detection result by Hidden Markov Model. We evaluated our algorithm on four datasets. The results showed that our proposed algorithm was capable of identifying IO state with 99.11% accuracy in indoor and outdoor environment where we have collected data and 97.02% accuracy in new indoor and outdoor scenarios. Furthermore, in the scenario of indoor and outdoor transition where we have collected data, the recognition accuracy reaches 94.53% and the probability of switching delay within 3 s exceeds 80%. In the new scenario, the recognition accuracy reaches 92.80% and the probability of switching delay within 4 s exceeds 80%.


Introduction
Indoor/outdoor (IO) context sensing plays a vital role for numerous applications, for example, human localization and tracking [1][2][3][4][5], activity recognition [6,7] and transportation mode [8][9][10], power management and medical care [11]. For seamless positioning and navigation [12,13], IO detection is a bridge between indoor and outdoor localization. To improve positioning accuracy and reduce power consumption, multi-source fusion positioning system triggers specific positioning and fusion strategies according to the results of IO detection. Smartphones automatically adjust screen brightness according to the IO status and environment condition (e.g., time, weather). The IO status provides personalized service such as adaptively adjusting the device volume.
in Daejeon, South Korea and the best accuracy (96.79%) was achieved when the score calculation range was 50, and the threshold value, 2000. In addition, the transition time of their method took only 3.81 s on average. Canovas et al. [28] employed a binary classification technique on the received signal strength indicator (RSSI) from 802.11 access points to identify a pedestrian's indoor or outdoor status. They conducted experiments on their campus, with a mean error rate around 2.5%. MagIO [29] utilized machine learning algorithms including Support Vector Machines (SVM), Gradient Boosting Machines (GBM), Random Forest (RF), K-Nearest Neighbor (kNN) and Decision Trees (DT) to deal with magnetic signals for IO detection. Experiments showed that Naive Bayes and random forest possess the capability to achieve an accuracy of 80% and higher with magnetic data alone. An ensemble-based stacking approach is presented, as well, which achieves an accuracy of 85.30% for a campus area, shopping mall and subway station using three different smartphones. Wang et al. [30] applied a machine learning algorithm to classify the neighboring GSM station's signal in different environments and identify the users' current context by signal recognition. They test the algorithm in four different environments in their campus. The results show that their algorithm is capable of identifying open outdoors, semi-outdoors, light indoors and deep indoors environments with 100% accuracy using four nearby GSM stations' signal strength. Radu [31] considered employing co-training according to the feature of light, magnetic and cell sensors for detection. It can automatically learn characteristics of new environments and devices and thereby provides a detection accuracy exceeding 90% even in unfamiliar circumstances. Anagnostopoulos [32] leveraged J48 and other machine learning algorithms to detect the IO state. They utilized multiple contextual features such as activity, barometric, ambient light, GSM, magnetometer variance, etc. Using all sensors; they could achieve 99% classification accuracy with a 10-fold cross-validation test. Wi-FiBoost [33] was designed to utilize AdaBoost [20] determines in a fast and accurate way whether a device is inside or outside particular buildings. They conducted all their experiments in two facilities located on their campus and showed that the resulting performance, a mean error rate around 2.5%. Some of the mentioned above algorithm depend on the measurement of signals from a large number of visible neighbor cellular towers. However, most of the current smartphones do not support recording measurements from all neighbor cellular towers. Also, in the mentioned above algorithms, all the algorithms except Sung et al. [27] did not evaluate the indoor and outdoor scene switching delay.
On the other hand, it is difficult to obtain satisfactory classification results with only two classification labels, since a variety of signal variations in complex scenarios and the similarity of indoor and outdoor signal sources in IO transition regions. Due to the GNSS signal, light intensity, geomagnetism, Wi-Fi and other sensor features are different in the open outdoors and deep indoors. Distinguishing IO state in this two environment is easy. However, the actual indoor and outdoor scenes encountered in the urban area are not all the above two ideal scenes, such as on the overpass, near the tall buildings, inside the glass curtain wall, close to the indoor patio, etc. We define these ideal and non-ideal indoor and outdoor environments as complex scenes. By defining the categories of complex scenes, we present the diversity of scenes for data collection.
For the mentioned above reasons, a lot of previous studies proposed an ambiguous state like semi-outdoor or shallow indoors to obtain better experiments result. Both fixed detection rules or threshold-based methods achieve satisfactory detection accuracy in ideal open outdoors or closed indoors environments, however, the IO detection accuracy of the abovementioned methods significantly decreases or they are even unable to identify the IO transition areas as shown in Figure 1. However, an uncertain status like semi-outdoor and shallow indoors is difficult for many applications to interpret since the environmental characteristics there are not defined. In this paper, we focus on these complex scenarios, but the final detection status only includes indoors and outdoors. Furthermore, the accuracy and transition delay of IO transition delay in complex scenarios is also a problem we are concerned with. To accomplish IO detection in complex scenarios, we must address the following two major challenges. We define the scenes without data collection in the training stage and the scenes without specific parameters as new environments. First, the poor performance of IO detection in the new environment is due to a variety of signal variations, since users may be in new scenarios that not match the training phase, such as in a room, near a window, under an overpass, in the open outdoors and so on. Detecting all environments with fixed rules and constraints is impractical. Second, it is difficult to detect the transition between indoor and outdoor environments correctly within a short time due to the similarity of indoor and outdoor signal sources when switching between indoors and outdoors in complex scenarios.
To address both challenges mentioned above, we leverage Global Navigation Satellite System (GNSS) measurements from Android smartphones to detect IO complex environments. Because of the availability and accuracy of satellite signals tend to be less affected by factors other than the environment, we extract spatial geometry distribution, time sequence and statistical features from the GNSS measurements through Android smart mobile devices. Then, we applied supervised machine learning algorithms to predict IO status. Finally, we regard the predicted IO status as the observations of Hidden Markov Model (HMM) [17] to accurately recognize IO status and immediately detect the transition between IO in complex scenarios. To the best of our knowledge, this paper is the first that uses a stacking model with HMM for IO detection.
The main contributions of our work are summarized as follows: 1) We propose a novel IO detection algorithm employing an ensemble model based on stacking. To further filter the occasional detection errors and improve the reliability of IO detection in complex scenarios, we adopt the HMM to the detection results obtained by the ensemble model. 2) We focus on IO switching detection to guarantee the continuity of IO detection. To improve the IO detection accuracy and reduce IO switching delay, we analyze and extract spatial geometry distribution, time sequence and statistical features of GNSS measurements using different sliding windows in Android smartphone rather than other GNSS receivers. 3) Also, to evaluate the proposed algorithm in typical IO scenarios, we compare our proposed algorithm with two state-of-the-art IO detection methods using GNSS information on four different datasets. The experimental results showed that, in the complex IO scenarios, our proposed algorithm achieved higher IO detection accuracy and lower switching delay than other algorithms under the new test environments.

System Overview
We divided complex environments into four types: deep indoors, shallow indoors, semioutdoors and open outdoors, as shown in Figure 2. Deep indoors is the area far from windows, balcony and glass curtain wall, while shallow indoors is the area opposite of deep indoors. Semioutdoors is a semi-open area covered by modern buildings, overpasses and patios, while open outdoors is in the non-covered area with better sky exposure. To accomplish IO detection in complex scenarios, we must address the following two major challenges. We define the scenes without data collection in the training stage and the scenes without specific parameters as new environments. First, the poor performance of IO detection in the new environment is due to a variety of signal variations, since users may be in new scenarios that not match the training phase, such as in a room, near a window, under an overpass, in the open outdoors and so on. Detecting all environments with fixed rules and constraints is impractical. Second, it is difficult to detect the transition between indoor and outdoor environments correctly within a short time due to the similarity of indoor and outdoor signal sources when switching between indoors and outdoors in complex scenarios.
To address both challenges mentioned above, we leverage Global Navigation Satellite System (GNSS) measurements from Android smartphones to detect IO complex environments. Because of the availability and accuracy of satellite signals tend to be less affected by factors other than the environment, we extract spatial geometry distribution, time sequence and statistical features from the GNSS measurements through Android smart mobile devices. Then, we applied supervised machine learning algorithms to predict IO status. Finally, we regard the predicted IO status as the observations of Hidden Markov Model (HMM) [17] to accurately recognize IO status and immediately detect the transition between IO in complex scenarios. To the best of our knowledge, this paper is the first that uses a stacking model with HMM for IO detection.
The main contributions of our work are summarized as follows:

1)
We propose a novel IO detection algorithm employing an ensemble model based on stacking.
To further filter the occasional detection errors and improve the reliability of IO detection in complex scenarios, we adopt the HMM to the detection results obtained by the ensemble model. 2) We focus on IO switching detection to guarantee the continuity of IO detection. To improve the IO detection accuracy and reduce IO switching delay, we analyze and extract spatial geometry distribution, time sequence and statistical features of GNSS measurements using different sliding windows in Android smartphone rather than other GNSS receivers. 3) Also, to evaluate the proposed algorithm in typical IO scenarios, we compare our proposed algorithm with two state-of-the-art IO detection methods using GNSS information on four different datasets. The experimental results showed that, in the complex IO scenarios, our proposed algorithm achieved higher IO detection accuracy and lower switching delay than other algorithms under the new test environments.

System Overview
We divided complex environments into four types: deep indoors, shallow indoors, semi-outdoors and open outdoors, as shown in Figure 2. Deep indoors is the area far from windows, balcony and glass curtain wall, while shallow indoors is the area opposite of deep indoors. Semi-outdoors is a semi-open area covered by modern buildings, overpasses and patios, while open outdoors is in the non-covered area with better sky exposure.  We constructed a dataset by associating the GNSS measurements from Android smartphones and the truth value of the abovementioned scenarios. We extracted 36 features under three types of spatial geometry distribution, time sequence and statistical from GNSS measurements. Depending on the scenarios, the indoors and outdoors scenarios were marked as positive sample and negative sample, respectively. Thus, the IO detection task was transformed into a supervised binary classification problem. Multiple single classification models and a classification model based on stacking were used for training and testing. After the classification model forecast, a hidden Markov model inferred the current environment types from the predictions as final detection results. We evaluated the accuracy and context switch latency of our proposed algorithm under complex scenarios using four different datasets. Figure 3 illustrates the algorithm framework of the proposed IO detection.

Data Collection
GNSS measurements were collocated at 1Hz using different Android smartphones. According to GNSS measurements, we define G as follows: We constructed a dataset by associating the GNSS measurements from Android smartphones and the truth value of the abovementioned scenarios. We extracted 36 features under three types of spatial geometry distribution, time sequence and statistical from GNSS measurements. Depending on the scenarios, the indoors and outdoors scenarios were marked as positive sample and negative sample, respectively. Thus, the IO detection task was transformed into a supervised binary classification problem. Multiple single classification models and a classification model based on stacking were used for training and testing. After the classification model forecast, a hidden Markov model inferred the current environment types from the predictions as final detection results. We evaluated the accuracy and context switch latency of our proposed algorithm under complex scenarios using four different datasets. Figure 3 illustrates the algorithm framework of the proposed IO detection.  We constructed a dataset by associating the GNSS measurements from Android smartphones and the truth value of the abovementioned scenarios. We extracted 36 features under three types of spatial geometry distribution, time sequence and statistical from GNSS measurements. Depending on the scenarios, the indoors and outdoors scenarios were marked as positive sample and negative sample, respectively. Thus, the IO detection task was transformed into a supervised binary classification problem. Multiple single classification models and a classification model based on stacking were used for training and testing. After the classification model forecast, a hidden Markov model inferred the current environment types from the predictions as final detection results. We evaluated the accuracy and context switch latency of our proposed algorithm under complex scenarios using four different datasets. Figure 3 illustrates the algorithm framework of the proposed IO detection.

Data Collection
GNSS measurements were collocated at 1Hz using different Android smartphones. According to GNSS measurements, we define G as follows:

Data Collection
GNSS measurements were collocated at 1Hz using different Android smartphones. According to GNSS measurements, we define G as follows: where G denotes a set of n visible satellites smartphones recorded in a second. g i denotes visible satellite information, including the constellation type, ID in the constellation, Carrier Noise Ratio(CNR), azimuth and elevation when available. The datagram information (including $GPGGA, $GPGSA, $GPGSV, and other datagrams) in the NEMA-0183 protocol was obtained through the interface provided by the Android system. We define S as follows: where S contains the value of Position Dilution of Precision (PDoP), Horizontal Dilution of Precision (HDoP), and Vertical Dilution of Precision (VDoP) of the current environment by parsing the $GPGSA datagram from NEMA-0183 protocol.

Data Pre-Processing
Environmental change is a continuous process and we need to consider how signal changes within a period for context awareness. Therefore, we introduced a sliding window that contained more descriptive information to preprocess data. Sliding window with different size covers diverse information to detect environment. Figure 4 shows the new GNSS measurements set in the collection sequence when the sliding window length k is 3. When the window size k is 1, the set of GNSS information G and S are used to extract features of the current environment. When the window length k is greater than 1, sliding window obtain time sequence information of GNSS measurements, we define three new sets of GNSS measurements G u , G p and S u as follows: where G u represents the union set and G p represents the intersection set of the G according to the visible satellite constellation and the ID in the constellation as the unique identification when the window length is k. S u represents the set of all values of S when the window length is k. where G denotes a set of n visible satellites smartphones recorded in a second. g i denotes visible satellite information, including the constellation type, ID in the constellation, Carrier Noise Ratio(CNR), azimuth and elevation when available. The datagram information (including $GPGGA, $GPGSA, $GPGSV, and other datagrams) in the NEMA-0183 protocol was obtained through the interface provided by the Android system. We define S as follows: where S contains the value of Position Dilution of Precision (PDoP), Horizontal Dilution of Precision (HDoP), and Vertical Dilution of Precision (VDoP) of the current environment by parsing the $GPGSA datagram from NEMA-0183 protocol.

Data Pre-Processing
Environmental change is a continuous process and we need to consider how signal changes within a period for context awareness. Therefore, we introduced a sliding window that contained more descriptive information to preprocess data. Sliding window with different size covers diverse information to detect environment. Figure 4 shows the new GNSS measurements set in the collection sequence when the sliding window length k is 3. When the window size k is 1, the set of GNSS information G and S are used to extract features of the current environment. When the window length k is greater than 1, sliding window obtain time sequence information of GNSS measurements, we define three new sets of GNSS measurements u G , p G and u S as follows: where u G represents the union set and p G represents the intersection set of the G according to the visible satellite constellation and the ID in the constellation as the unique identification when the window length is k. u S represents the set of all values of S when the window length is k.
, , The sliding window plays two important roles in the data pre-proposal stage. The first is to reduce the noise fluctuation. Since the GNSS signal received by smartphones is unstable, we calculate the statistical features at the current moment in set p G and u S . The other one is to obtain more GNSS information to extract time sequence features. The information of a single moment can hardly reflect the change of signal in time sequence. We utilize different sliding window size (e.g., 2, 3, 5) to smooth the current time GNSS signal and extract time sequence features at different time intervals. The sliding window plays two important roles in the data pre-proposal stage. The first is to reduce the noise fluctuation. Since the GNSS signal received by smartphones is unstable, we calculate the statistical features at the current moment in set G p and S u . The other one is to obtain more GNSS information to extract time sequence features. The information of a single moment can hardly reflect the change of signal in time sequence. We utilize different sliding window size (e.g., 2, 3, 5) to smooth the current time GNSS signal and extract time sequence features at different time intervals. In our experiments, we compared different kinds of features in different sliding window size (e.g., 2, 3, 5) in Section 3.2.1.

Feature Extraction
Feature selection affects significantly to the performance of scenario recognition classifier. Table 1 describes the 36 kinds of features extracted by GNSS measurements.

Visible Satellite Geometry Topology
The satellite azimuth is based on the standard of the direct north direction (about the geomagnetic South Pole). The satellite antenna points to the east or west to adjust an angle, which reflects the position of the satellite in space. Its value ranges from 0 to 360 • . Figure 5 shows the cumulative probability of the number of visible satellites azimuth using different smartphones in the range of 90 and 180 • in a week. In outdoor scenarios, within the range of 90 • , the ratio of the number of the satellite is mainly concentrated in the range from 0.4 to 0.6. Meanwhile, within the range of 180 • , the ratio of the number of the satellite is mainly concentrated in the range from 0.6 to 0.8. However, in indoor scenarios, the ratio of the number of satellite start from 0 due to there is no signal. Furthermore, the ratio of the number of the satellite is mainly concentrated in the range from 0.8 to 1. different smartphones in the range of 90 and 180° in a week. In outdoor scenarios, within the range of 90°, the ratio of the number of the satellite is mainly concentrated in the range from 0.4 to 0.6. Meanwhile, within the range of 180°, the ratio of the number of the satellite is mainly concentrated in the range from 0.6 to 0.8. However, in indoor scenarios, the ratio of the number of satellite start from 0 due to there is no signal. Furthermore, the ratio of the number of the satellite is mainly concentrated in the range from 0.8 to 1. We deeply analyzed and compared the distribution of satellite azimuth in complex indoor and outdoor scenarios. In deep indoors, it is difficult to receive the visible satellite signal because the reinforced concrete and the wall structure block the satellite signal. In shallow indoors, through satellite signals may penetrate the glass curtain wall and window, the azimuth range of observable satellites is limited due to limited exposure to the sky. In semi-outdoors, although blocked by tall buildings and overpass, the scope of the exposed sky is larger than that in shallow indoors, the satellite signal can still be detected after reflecting by the multi-path effect. In the open outdoors, the visual satellite is dispersed in the sky with a strong signal.  We deeply analyzed and compared the distribution of satellite azimuth in complex indoor and outdoor scenarios. In deep indoors, it is difficult to receive the visible satellite signal because the reinforced concrete and the wall structure block the satellite signal. In shallow indoors, through satellite signals may penetrate the glass curtain wall and window, the azimuth range of observable satellites is limited due to limited exposure to the sky. In semi-outdoors, although blocked by tall buildings and overpass, the scope of the exposed sky is larger than that in shallow indoors, the satellite signal can still be detected after reflecting by the multi-path effect. In the open outdoors, the visual satellite is dispersed in the sky with a strong signal.
In the open outdoors, the visual satellite is dispersed in the sky with a strong signal. Figure 6a demonstrates an example of sky plot and availability of visible satellites under complex scenarios using a Huawei Mate 9 smartphone. Figure 6b is a sketch that demonstrates the distribution of the maximum number of satellites within the range of 90 • and 180 • in the indoor environment with French windows. Based on the above analysis, we extracted the geometric topological features of the visible satellite in set G. We divided the range of azimuth from 0 to 360 • into 36 sectors at an interval of 10 • . Then, we constructed a 36-dimensional feature vector d to represent the distribution of satellites in these sectors. The vector elements include 0 and 1. 0 indicates no visible satellites in this sector. 1 indicates visible satellites in this sector. The proportion of sector occupied by visible satellites was considered as one feature as follows: The satellite azimuth is based on the standard of the direct north direction (about the geomagnetic South Pole). The satellite antenna points to the east or west to adjust an angle, which reflects the position of the satellite in space. Its value ranges from 0 to 360°. Figure 5 shows the cumulative probability of the number of visible satellites azimuth using different smartphones in the range of 90 and 180° in a week. In outdoor scenarios, within the range of 90°, the ratio of the number of the satellite is mainly concentrated in the range from 0.4 to 0.6. Meanwhile, within the range of 180°, the ratio of the number of the satellite is mainly concentrated in the range from 0.6 to 0.8. However, in indoor scenarios, the ratio of the number of satellite start from 0 due to there is no signal. Furthermore, the ratio of the number of the satellite is mainly concentrated in the range from 0.8 to 1. We deeply analyzed and compared the distribution of satellite azimuth in complex indoor and outdoor scenarios. In deep indoors, it is difficult to receive the visible satellite signal because the reinforced concrete and the wall structure block the satellite signal. In shallow indoors, through satellite signals may penetrate the glass curtain wall and window, the azimuth range of observable satellites is limited due to limited exposure to the sky. In semi-outdoors, although blocked by tall buildings and overpass, the scope of the exposed sky is larger than that in shallow indoors, the satellite signal can still be detected after reflecting by the multi-path effect. In the open outdoors, the visual satellite is dispersed in the sky with a strong signal.  To further explore the topological relationship of satellites in spatial, the maximum proportion of the number of satellites within the range of 90 • and 180 • of the azimuth variation of visible satellites in G is extracted as features. We defined a function f (i, az j ) that calculated whether a satellite is within the range of satellite azimuth. The formula for calculating the GS_num_proportion_range is as follows: where range represents the range of satellite azimuth. sector. 1 indicates visible satellites in this sector. The proportion of sector occupied by visible satellites was considered as one feature as follows: To further explore the topological relationship of satellites in spatial, the maximum proportion of the number of satellites within the range of 90° and 180° of the azimuth variation of visible satellites in G is extracted as features. We defined a function ( ) j f i,az that calculated whether a satellite is within the range of satellite azimuth. The formula for calculating the GS_num_proportion_range is as follows: where range represents the range of satellite azimuth.  In rare cases, there are lower DoP values in an indoor environment. Although there is a certain amount of error in relying on GPS positioning in outdoor scenarios, the value of DoP is generally low. Since it takes a short time for us to collect each piece of data, and smartphones obtain the DoP value through a calculation process, there is a high probability that the DoP value in outdoor scenarios exceeds 10 in our statistical results. We extracted PDoP, HDoP andVDoP as features from S . In rare cases, there are lower DoP values in an indoor environment. Although there is a certain amount of error in relying on GPS positioning in outdoor scenarios, the value of DoP is generally low. Since it takes a short time for us to collect each piece of data, and smartphones obtain the DoP value through a calculation process, there is a high probability that the DoP value in outdoor scenarios exceeds 10 in our statistical results. We extracted PDoP, HDoP andVDoP as features from S.

The Number of Visible Satellites Vary in Weight
IO transitions are a continuous process. It is worth noting that time sequence features contain more useful information. Figure 8 demonstrates the changes in the number of visible satellites under different devices when switching between indoor and outdoor scenarios. Due to the weak satellite signal in the indoor scenarios, the number of satellites detected by the terminals is low. When the smartphones switch from indoors to outdoors, the number of visible satellites increases rapidly. However, when switching from outdoors to indoors, it can be seen that the satellite is in the tracking status, and the receiver conducts targeted integration and other processing on the tracked satellites, the terminal can still detect these satellites within a certain period. different devices when switching between indoor and outdoor scenarios. Due to the weak satellite signal in the indoor scenarios, the number of satellites detected by the terminals is low. When the smartphones switch from indoors to outdoors, the number of visible satellites increases rapidly. However, when switching from outdoors to indoors, it can be seen that the satellite is in the tracking status, and the receiver conducts targeted integration and other processing on the tracked satellites, the terminal can still detect these satellites within a certain period.

Visible Satellite CNR vary ratio
Visible satellite CNR vary ratio is also an essential feature in time sequence. Figure 9a shows the variation trend of GPS-12 CNR with different devices when IO transition under the same scenario. Figure 9b shows the trend of all visible satellite CNR using Mate 9. The variation trend of satellite CNR is close to that of the number of satellites in Figure 8 when IO transition. It is worth noting that the satellite CNR varies widely over time in both outdoor and indoor scenarios. It is not advisable to extract features by relying on the variation trend of single satellite CNR. To mitigate the effects of signal diversity in a different environment and alleviate the influences of device heterogeneity on GNSS measurements, we utilized the number of satellites at the current time to calculate the weight of the change of the number of satellites. Inspired by the changes in the number of visible satellites, we defined GS_Num _w t 1 −t 2 as follows: where cnt t 1 denotes the number of satellites at current time t, cnt t 2 denotes the number of satellites at time t-k. k denotes the window size (e.g., 1, 2, 3). In the experiment, we compared the influence of the feature under different window size on indoor and outdoor detection.

Visible Satellite CNR vary ratio
Visible satellite CNR vary ratio is also an essential feature in time sequence. Figure 9a shows the variation trend of GPS-12 CNR with different devices when IO transition under the same scenario. Figure 9b shows the trend of all visible satellite CNR using Mate 9. The variation trend of satellite CNR is close to that of the number of satellites in Figure 8 when IO transition. It is worth noting that the satellite CNR varies widely over time in both outdoor and indoor scenarios. It is not advisable to extract features by relying on the variation trend of single satellite CNR. To avoid the influence mentioned above of the variation trend of single satellite CNR, we mined and utilized the variation trend of all visible satellites as features in time sequence. We defined a function ( ) , x y f that represents the variation of the satellite CNR in two moments as follows: To avoid the influence mentioned above of the variation trend of single satellite CNR, we mined and utilized the variation trend of all visible satellites as features in time sequence. We defined a function f (x,y) that represents the variation of the satellite CNR in two moments as follows: where cnr x i denotes the satellite CNR at current time t, cnr y i denotes the satellite CNR at time t-k. k denotes the window size (e.g., 1, 2, 3). In the experiment, we compared the influence of the feature under different window size on indoor and outdoor detection.
We extracted the variation ratio of all visible satellite in G u as features. We defined the formulas for the descending ratio P(down), rising ratio P(up) and flat ratio P(hold) of all visible satellite CNR at different times as Equations (11), (13) and (14), respectively.

Statistical Features
The number of satellites and the distribution of satellite CNR is different in indoor and outdoor scenarios. As shown in Figure 10a, the cumulative probability of indoor scenarios is close to 0.7 when the number of visible satellites is under 8, while the cumulative probability of outdoor scenarios is lower than 0.05 when the number of visible satellites is under 8. Figure 10b shows that the satellite CNR collected by different devices in the same scenario is different, but the distribution density tends to be consistent. In other words, the statistical data of the number of satellites and CNR are effective features to distinguish complex indoor and outdoor scenarios. To utilize more descriptive information under sliding windows of different lengths, we not only regard the number of the visible satellite as a feature, but also extract Mean, Variance, Std, Min, Max, Median, Range, InterQuartile Range, Skewness and Kurtosis of satellite CNR from G and p G as features. Furthermore, we consider the mean of PDoP, HDoP and VDoP in u S . To utilize more descriptive information under sliding windows of different lengths, we not only regard the number of the visible satellite as a feature, but also extract Mean, Variance, Std, Min, Max, Median, Range, InterQuartile Range, Skewness and Kurtosis of satellite CNR from G and G p as features. Furthermore, we consider the mean of PDoP, HDoP and VDoP in S u .

Single Classification Model
In the training phase, we try to train different model using a variety of machine learning algorithms such as RF [34], SVM [35], Adaptive Boosting (AdaBoost) [20], XGBoost (XGB) [36] and LightGBM (LGB) [37]. The training data contains GNSS information features extracted from different sliding window sizes. In the testing phase, we evaluated different classifier for indoors/outdoors detection.

Classification Model Based on Stacking Ensemble
Stacking is a model ensembling technique, which uses the initial training data to learn some base learners and uses the predicted results of these learners as a new training set to generate a new model. In general, the stacked model outperforms each of the individual models due to its smooth nature and ability to highlight each base model where it performs best and discredit each base model where it performs poorly. As shown in Figure 11, we used a 2-layer stacking model for training. XGBoost, LightGBM, AdaBoost and Random Forest are used to train the base model in the first layer to generate the train set and the test set for the second layer. Logistic regression is employed to output the final prediction in the second layer.

Hidden Markov Model
HMM is a model based on probability statistics. In this paper, we use the first-order HMM, which assumes the current scenario state is only affected by the previous state. The probabilities of each state at each epoch can be inferred by the Viterbi algorithm [38] from the observations sequence. In general, an HMM comprises five elements as follows: 1) The state space S that consists of two hidden states: indoor and outdoor, which are denoted as S0 and S1.
2) The set of observations at each epoch refers to the predicted result of the supervised model

Hidden Markov Model
HMM is a model based on probability statistics. In this paper, we use the first-order HMM, which assumes the current scenario state is only affected by the previous state. The probabilities of each state 1) The state space S that consists of two hidden states: indoor and outdoor, which are denoted as S 0 and S 1 .
2) The set of observations at each epoch refers to the predicted result of the supervised model on the testing set.
3) The matrix of state transition probabilities was set by prior experience. Table 2 lists the values of transition probability. 4) The matrix of emission probabilities refers to normalized confusion matrix of the predicted results of the supervised model on the testing set. Table 3 shows the emission probabilities of each state to each feature. 5) The initial state X 1 probabilities set as follows:

Data Collection
We conducted all our experiments under a variety of weather conditions, including sunny, cloudy and hazy days in the Beijing urban area. Volunteers collected GNSS data in 58 scenarios, including indoors, outdoors and IO transitions on campus, and in a shopping mall, restaurant, office building, pedestrian street, an overpass and a residential area, with four different types of phones (Huawei Mate 8, Huawei Mate 9, Huawei Honor 8, Vivo X9) within a month. The mobile phone system version was Android 7.0 or above.
Our volunteers move naturally with their phones held in front of their chest when they collected data in the complex indoor and outdoor scenarios. The only constraint was to modify the ground-truth IO label when they switch between indoor and outdoor scenarios. Especially, we collected data from deep indoors, shallow indoors, semi-open outdoors and open outdoors to provide the credibility of IO detection in complex scenarios. A group of collected data was divided into three categories: indoor data collection, outdoor data collection, and IO transition data collection. The data collection time of each log file was more than 1 minute.

Dataset Segmentation
To ensure the performance on the validation set approximates to it on the test set, we split the dataset by mobile phone serial number and scenario. To train the universal I/O classification model, we selected a dataset_0 that contained 118,432 data items including 15 indoor and outdoor scenes and 18 indoor and outdoor switching scenes. We hope that a classification model can accurately identify both pure indoor and outdoor scenes and achieve brilliant performance in indoor and outdoor switching scenes.
In order to assess the performance of our model in different scenarios, we selected dataset_1, dataset_2, dataset_3 and dataset_4 to comprehensively evaluate our classification model from the aspects of recognition accuracy and switching delay. Especially, dataset_1 and dataset_3 were used to evaluate the performance of IO detection. Dataset_2 and dateset_4 were used to evaluate the performance of IO transitions detection. The data in these four test sets are untrained. The scenario of dataset_1 and dataset_2 are the same as the IO scenarios and I/O switching scenarios of dataset_0 respectively, while dataset_3 and dataset_4 were collected in the new scenario. Dataset_ 3 and dataset_ 4 would be used to evaluate our model's ability to adapt to the new test environment.
Dataset_1 was used to evaluate the recognition performance of indoor and outdoor scenes that had been trained in the training stage. 17,722 data items were collected in these indoor and outdoor scenes in dataset_1. Dataset_2 was used to evaluate the recognition performance of I/O switching scenes that have been trained in the training stage. 31,290 data items were collected in these I/O switching scenes in dataset_2. Dataset_3 was used to evaluate the recognition performance of indoor and outdoor scenes that had been untrained in the training stage. 17,199 data items were collected in these indoor and outdoor scenes in dataset_3. Dataset_4 was used to evaluate the recognition performance of I/O switching scenes that have been trained in the training stage. 11,218 data items were collected in these I/O switching scenes in dataset_4. Table 4 presents the detail distribution of five datasets.

Accuracy Evaluation of Different Features and Models
We evaluated the accuracy of five single classification models including RF, SVM, AdaBoost, XGB and LGB, as well as a classification model based on stacking ensemble and a stacking model with HMM in four datasets. The detection accuracy was compared under different kinds of features including only statistical features in different sliding window length k, only spatial geometry distribution features; only time sequence features in different sliding window length k and a combination of the above features. Tables 5-8 show the comparison results on Dataset_1, Dataset_2, Dataset_3 and Dataset_4, respectively.    Table 7. Indoor/Outdoor detection accuracy using different features and models on Dataset_3.  Table 8. Indoor/Outdoor detection accuracy using different features and models on Dataset_4. • The different sliding window size influenced the accuracy of different datasets. For statistical features in four datasets, using the statistical features of the current time (k = 1) can obtain higher accuracy, while for the time sequence features, the bigger the window size was, we obtained the higher accuracy.

•
The accuracy of only use statistical features on Dataset_1, Dataset_2, Dataset_3 and Dataset_4 was 0.9893, 0,9344, 0.9319 and 0.9185, respectively. However, in the vast majority of cases, the accuracy was under 0.91 when only used time sequence features. The accuracy reached 0.9662 on Dataset_1 when only used spatial geometry distribution features, while in other datasets, the accuracy was less than 0.81. This result showed that statistical features play the most crucial role in IO detection. Since the number of features contained in spatial geometry distribution and time sequence was small, only using these two kinds of features lead to low accuracy.

•
It was worth noting that spatial geometry distribution features and time sequence features improved accuracy more than 0.01 on Dataset_2 and Dataset_4. Furthermore, the improved accuracy of the two kinds of features mentioned above was closed to 0.04 on Dataset_3. Therefore, spatial geometry distribution features and time sequence features contributed to IO detection.

•
In indoor and outdoor scenarios, the optimal accuracy of Dataset1 was 0.0209 higher than that of Dataset3 using the same kinds of features. In IO transition scenarios, the optimal accuracy of Dataset2 was 0.0173 higher than that of Dataset4 using the same kinds of features. While the accuracy of our proposed algorithm in the new test environment was lower than that in the environment where collected training data, the overall accuracy in dataset_3 and dataset_4 were more than 0.9280, that means the proposed algorithm robust among different complex environments (non-sampled environments).

Feature Importance Analysis
According to the accuracy on all test datasets, LightGBM performed best in all single classification model. To intuitively reflect the different kinds of features used in the LightGBM classifier training stage, we focus on the top 25 features importance. Figure 12 shows the ranking of feature importance. According to the accuracy on all test datasets, LightGBM performed best in all single classification model. To intuitively reflect the different kinds of features used in the LightGBM classifier training stage, we focus on the top 25 features importance. Figure 12 shows the ranking of feature importance. As shown in Figure 12, we found that the mean of satellite CNR feature at a current time, GS_num_proportion_90 and the skewness of satellite CNR played the most three crucial roles in the training stage. The statistical features play the most important role in the training phase that accounts for 67.97% of the top 25 features due to a large number of statistical features were extracted. It is worth noting that the time sequence related features account for 17.78% and spatial geometry distribution related features account for 14.25%, also reflecting the effectiveness of these two kinds of related features. As shown in Figure 12, we found that the mean of satellite CNR feature at a current time, GS_num_proportion_90 and the skewness of satellite CNR played the most three crucial roles in the training stage. The statistical features play the most important role in the training phase that accounts for 67.97% of the top 25 features due to a large number of statistical features were extracted. It is worth noting that the time sequence related features account for 17.78% and spatial geometry distribution related features account for 14.25%, also reflecting the effectiveness of these two kinds of related features.

Transition Delay
In this section, we evaluated the IO transition delay based on the proposed IO detection algorithm. According to the detection accuracy in Table 5, we focused on the model based on stacking ensemble and a stacking model with HMM. We first verified the performance of the mentioned above two algorithms on dataset_2. Figure 13 shows the cumulative probability of transition delay using different algorithms on dataset_2. Under the classification model based on stacking ensemble, the transition delay from indoor to outdoor is lower than that from outdoor to indoor. There is little difference between indoor to outdoor transition delay and outdoor to indoor transition delay by a stacking model with HMM. The cumulative distribution probability of the stacking model and a stacking model with HMM algorithm of switching delay in 3 s reached more than 80%. We also evaluated the transition delay of our algorithm in new scenarios. Figure 14 shows the cumulative probability of transition delay using different algorithms on Dataset_4. There is little difference between indoor to outdoor transition delay and outdoor to indoor transition delay under these two algorithms. The cumulative probability of two algorithms of transition delay in 4 s reached more than 80%. The transition delay of Dataset_4 was slightly higher than that of Dataset_2. The above experiments show that our algorithm meets the requirement of quickly detect IO transition. Furthermore, the performance of our algorithm in transition delay decreases slightly in the new environment using new smartphones. Meanwhile, the HMM model has no obvious effect on reducing transition delay. We also evaluated the transition delay of our algorithm in new scenarios. Figure 14 shows the cumulative probability of transition delay using different algorithms on Dataset_4. There is little difference between indoor to outdoor transition delay and outdoor to indoor transition delay under these two algorithms. The cumulative probability of two algorithms of transition delay in 4 s reached more than 80%. The transition delay of Dataset_4 was slightly higher than that of Dataset_2. We also evaluated the transition delay of our algorithm in new scenarios. Figure 14 shows the cumulative probability of transition delay using different algorithms on Dataset_4. There is little difference between indoor to outdoor transition delay and outdoor to indoor transition delay under these two algorithms. The cumulative probability of two algorithms of transition delay in 4 s reached more than 80%. The transition delay of Dataset_4 was slightly higher than that of Dataset_2. The above experiments show that our algorithm meets the requirement of quickly detect IO transition. Furthermore, the performance of our algorithm in transition delay decreases slightly in the new environment using new smartphones. Meanwhile, the HMM model has no obvious effect on reducing transition delay. The above experiments show that our algorithm meets the requirement of quickly detect IO transition. Furthermore, the performance of our algorithm in transition delay decreases slightly in the new environment using new smartphones. Meanwhile, the HMM model has no obvious effect on reducing transition delay.

Performance Comparison with other Algorithms
We compared the proposed algorithm with other state-of-the-art IO detection methods (SatProbe [21] and Gao et al. [22]). SatProbe only used the number of GPS visible satellites as a more direct indicator of IO status (indoor, semi-outdoor and outdoor). In SatProbe, if the detected satellite count is no more than 2, then the IO status is indoor. If there are six or more satellites in view, then the IO status is outdoor. For situations between, if the ambiguity persists, the IO status is determined to be semi-outdoor. Gao et al. extracted the number of visible satellites that CNR more than 25 dB-Hz (numCNR 25 ) and the sum of all visible satellites CNR that more than 25 db-Hz (sumCNR 25 ) based on the availability and strength of GNSS signals (GPS and GLONASS) as features. Then, a Hidden Markov Model was used to infer the current environment types (indoors, intermediate and outdoors) according to those extracted features.

Evaluation of Visible GPS Satellite Number Algorithm
However, there is no ambiguity state like semi-outdoors or intermediate in our datasets. We focus on these complex scenarios, but the final detection states only include indoors and outdoors. Therefore we did not use the same experimental parameters. For SatProbe, we used different visible GPS satellite numbers as thresholds for IO detection to obtain the optimal performance of the algorithm in our datasets. Figure 15 shows the experimental results. However, there is no ambiguity state like semi-outdoors or intermediate in our datasets. We focus on these complex scenarios, but the final detection states only include indoors and outdoors. Therefore we did not use the same experimental parameters. For SatProbe, we used different visible GPS satellite numbers as thresholds for IO detection to obtain the optimal performance of the algorithm in our datasets. Figure 15 shows the experimental results. As shown in Figure 15, when the threshold value set to 6, SatProbe achieved the highest accuracy in Dataset_2 (0.8066) and Dataset_3 (0.9720), and the second highest accuracy in Dataset_1 (0.9619) and Dataset_4 (0.7972). Since lack of fuzzy state as semi-outdoor, only depending on the number of GPS satellites, thus resulting in the performance of IO transition is very poor. As shown in Figure 15, when the threshold value set to 6, SatProbe achieved the highest accuracy in Dataset_2 (0.8066) and Dataset_3 (0.9720), and the second highest accuracy in Dataset_1 (0.9619) and Dataset_4 (0.7972). Since lack of fuzzy state as semi-outdoor, only depending on the number of GPS satellites, thus resulting in the performance of IO transition is very poor.
(c) Dataset_3 (d) Dataset_4 Figure 15. The accuracy of indoor and outdoor detection using different GPS number as a threshold on four datasets.
As shown in Figure 15, when the threshold value set to 6, SatProbe achieved the highest accuracy in Dataset_2 (0.8066) and Dataset_3 (0.9720), and the second highest accuracy in Dataset_1 (0.9619) and Dataset_4 (0.7972). Since lack of fuzzy state as semi-outdoor, only depending on the number of GPS satellites, thus resulting in the performance of IO transition is very poor.
(a) Dataset_2 ( b) Dataset_4  Next, we evaluated the IO transition delay when the threshold is 6. Figure 16 described the cumulative distribution probability of time delay on Dataset_2 and Dataset_4. To achieve more than 80% cumulative probability of indoor to outdoor transition on Dataset_2 and Dataset_4, the IO transition delay must be greater than 8 s and 8 s, respectively. To achieve more than 80% cumulative probability of outdoor to indoor transition on Dataset_2 and Dataset_4, the IO transition delay must be greater than 18 s and 14 s, respectively. It is difficult to detect the transition from outdoor scenes to indoor scenes in a short time only depending on the number of satellites. Such a long transition delay is difficult to accept for seamless indoor and outdoor positioning.

Evaluation of GNSS Signals Algorithm
This algorithm also divided the detection status into three types, which was similar to the definition of a fuzzy intermediate state of SatProbe. Hence, we modified the mean and variance of the Gaussian distribution in the emission matrix of the algorithm according to the statistical data of the features extracted by the algorithm from the training set. Furthermore, we adjusted the transition probabilities of HMM. We evaluated the accuracy of the algorithm on four datasets. As shown in Figure 17, the accuracy of this algorithm in Dataset_2 (0.8778) and Dataset_4 (0.8774) is nearly 0.10 lower than that in Dataset_1 (0.9753) and Dataset_3 (0.9798). Similarly, the IO detection accuracy of IO transition regions is lower than indoor and outdoor scenarios. Next, we evaluated the IO transition delay when the threshold is 6. Figure 16 described the cumulative distribution probability of time delay on Dataset_2 and Dataset_4. To achieve more than 80% cumulative probability of indoor to outdoor transition on Dataset_2 and Dataset_4, the IO transition delay must be greater than 8 s and 8 s, respectively. To achieve more than 80% cumulative probability of outdoor to indoor transition on Dataset_2 and Dataset_4, the IO transition delay must be greater than 18 s and 14 s, respectively. It is difficult to detect the transition from outdoor scenes to indoor scenes in a short time only depending on the number of satellites. Such a long transition delay is difficult to accept for seamless indoor and outdoor positioning.

Evaluation of GNSS Signals Algorithm
This algorithm also divided the detection status into three types, which was similar to the definition of a fuzzy intermediate state of SatProbe. Hence, we modified the mean and variance of the Gaussian distribution in the emission matrix of the algorithm according to the statistical data of the features extracted by the algorithm from the training set. Furthermore, we adjusted the transition probabilities of HMM. We evaluated the accuracy of the algorithm on four datasets. As shown in Figure 17, the accuracy of this algorithm in Dataset_2 (0.8778) and dataset 4 (0.8774) is nearly 0.10 lower than that in Dataset_1 (0.9753) and Dataset_3 (0.9798). Similarly, the IO detection accuracy of IO transition regions is lower than indoor and outdoor scenarios. Next, we evaluated the IO transition delay on Dataset_2 and Dataset_4. Figure 18 showed the cumulative distribution probability of time delay on Dataset_2 and Dataset_4. To achieve more than 80% cumulative probability of indoor to outdoor transition on Dataset_2 and Dataset_4, the IO transition delay must be greater than 7 s and 4 s, respectively. To achieve more than 80% cumulative probability of outdoor to indoor transition on Dataset_2 and Dataset_4, the IO transition delay must be greater than 12 s and 10 s, respectively. The transition efficiency of the algorithm [22] is better than that of SatProbe. However, 12 s is still unacceptable. Next, we evaluated the IO transition delay on Dataset_2 and Dataset_4. Figure 18 showed the cumulative distribution probability of time delay on Dataset_2 and Dataset_4. To achieve more than 80% cumulative probability of indoor to outdoor transition on Dataset_2 and Dataset_4, the IO transition delay must be greater than 7 s and 4 s, respectively. To achieve more than 80% cumulative probability of outdoor to indoor transition on Dataset_2 and Dataset_4, the IO transition delay must be greater than 12 s and 10 s, respectively. The transition efficiency of the algorithm [22] is better than that of SatProbe. However, 12 s is still unacceptable. Figure 17. The accuracy of indoor and outdoor detection using algorithm [22] on four datasets. Next, we evaluated the IO transition delay on Dataset_2 and Dataset_4. Figure 18 showed the cumulative distribution probability of time delay on Dataset_2 and Dataset_4. To achieve more than 80% cumulative probability of indoor to outdoor transition on Dataset_2 and Dataset_4, the IO transition delay must be greater than 7 s and 4 s, respectively. To achieve more than 80% cumulative probability of outdoor to indoor transition on Dataset_2 and Dataset_4, the IO transition delay must be greater than 12 s and 10 s, respectively. The transition efficiency of the algorithm [22] is better than that of SatProbe. However, 12 s is still unacceptable.

Overall Performance Comparison
In this section, we evaluated the performance of our proposed algorithm and the two comparison algorithms on four datasets as a whole. The detail experiments results are shown in Table 9. From Table 9 we obtain the following main conclusions: • In indoor and outdoor scenes, three indoor and outdoor recognition algorithms have high accuracy. Compared with the other two algorithms, the accuracy of our detection algorithm was 0.0158 higher than that of the other two algorithms in Dataset_1, and algorithm [22] was 0.0096 higher than our algorithm in Dataset_3.

•
In indoor and outdoor transition scenes, our detection algorithm is superior to the other two algorithms regarding accuracy and transition delay. The detection accuracy of our algorithm on Dataset_2 and Dataset_4 is 0.1387 and 0.1308 higher than SatProbe. The indoor to outdoor transition delay of out algorithm on Dataset_2 and Dataset_4 is 5 s and 4 s faster than that of SatProbe. Furthermore, the outdoor to indoor transition delay of our algorithm on Dataset_2 and Dataset_4 is 15 s and 10 s faster than that of SatProbe.

•
The detection accuracy of our algorithm on Dataset_2 and Dataset_4 is 0.0675 and 0.0546 higher than algorithm [22]. The indoor to outdoor transition delay of our algorithm on Dataset_2 is 4 s faster than that of the algorithm [22]. Furthermore, the outdoor to indoor transition delay of our algorithm on Dataset_2 and Dataset_4 is 9 s and 6 s faster than that of the algorithm [22]. • From the above data, we can find that the indoor and outdoor detection algorithm we proposed can accurately identify the indoor and outdoor state in a complex environment. Especially, our algorithm enables the ability to quickly detect the indoor and outdoor transition in 4 s with a probability of more than 0.8 that other algorithms cannot.

Algorithm Complexity Evaluation
To measure the complexity of the proposed algorithm, we compared the training time cost of each classification model running on a computer with Intel E5-2680 CPU and 64GB memory. Table 10 lists the time cost of training different classification models. Since we only extracted low-dimensional features from GNSS measurements, it did not require expensive training time cost. The training speed of LightGBM was superior to other classification models. Furthermore, the model ensemble process required an abundant computational procedure to train multiple models, and it was broadly in line with what we expected. We comprehensively considered the accuracy and training time, LightGBM used the minimum training time to obtain a higher prediction accuracy in five single classification models. While the classification model based on stacking ensemble with HMM obtained the optimal accuracy on Dataset_2 and Dataset_4, its performance on training was the worst due to the stacking model cost too much time.

Conclusions
In this paper, we have proposed a fast indoor/outdoor transition detection algorithm based on machine learning without any infrastructure. We extracted statistical and time sequence features under different sliding window lengths and spatial geometry distribution features from GNSS measurements. To evaluate IO detection accuracy and transition delay of the proposed algorithm, we conducted experiments in the complex indoor and outdoor environments in urban Beijing. The evaluation results demonstrate that the IO detection accuracy was 99.11% in indoor and outdoor scenarios where we have collected data and 97.02% in new indoor and outdoor scenarios. Furthermore, IO detection accuracy was 94.53% in indoor and outdoor transition scenarios where we have collected data and the probability of switching delay within 3 s exceeds 80%. In the new scenarios, IO detection accuracy was 92.80% and the probability of switching delay within 4 s exceeds 80%. The proposed algorithm outperforms other existing IO detection methods and satisfies the requirement of indoor and outdoor seamless navigation and positioning.
In our future work, we will further improve detection accuracy, shorten the switching delay and expand the scale of the experiment. Also, we will consider extracting more universality features to eliminate device heterogeneity. Furthermore, we will design an indoor and outdoor seamless navigation and positioning system based on smartphone for pedestrians.

Patents
The proposed algorithm is applying for a patent and now has been handed over to the agency.