## 3 Detection Method Design and Implementation
### Vehicle Detection Algorithm Design  
In this paper, the state machine mechanism is used to implement our proposed two-sensors data fusion vehicle detection method. For the sake of easy analysis, we binarized the relevant results before the method is implemented.  
\[F(n)=\left\{\begin{array}{c}1,\ abs(\ A(n)-B(n))\geq T(n)\\ 0,\ otherwise\end{array}\right. \tag{5}\]  
In Equation (5), \(T(n)\) is the decision threshold of magnetism-based or received-signal-strength-based vehicle detection method, the values are derived from a large amount of data analysis. \(F(n)\) is the binary vehicle detection result of magnetism-based or received-signal-strength-based detection method, a value of 1 indicates that a car has been detected and vice versa.  
The proposed two-sensors data fusion vehicle detection method is achieved by fusing the data of geomagnetism and received signal strength to overcome the limitations and instability of traditional single magnetism-based detection method. The algorithm principle is shown as follows:  
\[D(n)=\left\{\begin{array}{c}1,\ if(\ D(n-1)==0\ and\ F_{mag}(n)==1\ and\ n>0)\\ 0,\ if(\left(D(n-1)==1\ and\ F_{mag}(n)==0\ and\ F_{rss}(n)==0\ and\ n>0\right)\\where \(D(n)\) and \(D(n{-}1)\) is the current and previous binarization vehicle detection status of our proposed two-sensors data fusion vehicle detection method. \(F_{mag}(n)\) is the current binarization detection result of the magnetism-based detection method according to Equation (5). \(F_{rss}(n)\) is the current binarization detection result of the received-signal-strength-based vehicle detection method according to Equation (5). From Equation (6), we can consider a vehicle detected when the amount of change in the geomagnetic signal is greater than decision threshold and left when the amount of change both in the geomagnetic signal and the received signal strength are less than the respective decision threshold.  
Due to the existence of geomagnetic signal blind zone, it may cause the value of the binarization result \(F_{mag}(n)\) to change between 0 and 1 in short time. To overcome this problem, we introduce a continuous counting mechanism to eliminate the misjudgment caused by the short-term jump, the three introduced counters are vehicle entering counter \(CNT_{vehicle-entering}\), magnetism-based method vehicle leaving counter \(CNT_{mag-based-leaving}\) and received-signal-based method vehicle leaving counter \(CNT_{rss-based-leaving}\). Since the sampling rate of sensors are 100 Hz, according to the Nyquist Theorem and general traffic parameters, the vehicle entering counter's threshold \(Cy\) is set to 10.  
Generally, if the speeds of vehicles are fast on a road, the distance between the front and rear vehicles on the same lane will be much larger than that running at the low speeds. Therefore, the time interval between the front and rear vehicles running at fast speeds is similar to that running at low speeds. According to the above analysis, the vehicle leaving counter's threshold \(Cn\) is set to 30 in the paper. Overall, the vehicle detection status will change only when the value of each counter is greater than the respective threshold.  
The proposed two-sensors data fusion detection method in the paper is implemented based on the state machine mechanism.  
The state machine contains 4 states and is realized as shown in Figure 5: \(S_{init}\) initializes the value of relevant parameters.  
\(S_{non}\) measures the length of the high level magnetic pulse and updates reference baselines.  
\(S_{vehicle-existence}\) measures the length of the low level magnetic pulse and detects a vehicle.  
\(S_{vehicle-leaving}\) measures the length of the low level RSS pulse and waits the vehicle to leave.  
Figure 5: The state machine of the two-sensors data fusion vehicle detection method.