Practical Methods for Vehicle Speed Estimation Using a Microprocessor-Embedded System with AMR Sensors

The proper operation of computing resources in a microprocessor-embedded system plays a key role in reducing computing time. Processing the variable amount of collected data in real-time improves the performance of a microprocessor-embedded system. In this regard, a vehicle’s speed measurement system is no exception. The computing time for evaluating any speed value is expected to be reduced as much as possible. Four computational methods, including cross-correlation, are discussed. An exemplary pair of recorded signals presenting the change in magnetic field magnitude is analyzed. The sample delay values are compared. The results of the evaluated speed and the execution time of the program code are presented for each method based on a dataset of 200 randomly driven vehicles. The results of the performed tests confirm that the cross-correlation-based methods are not always reliable in situations when the sample size is small, i.e., it is a segment of the impulse response caused by a driving vehicle.


Introduction
Time series are used in many domains of applied science and engineering. In recent years, scientists and engineers have been working on vehicle detection systems and vehicle speed estimation using wireless sensor networks (WSNs) [1]. Magnetic sensors, as part of a vehicle's detection system, are mounted under or on the surface of roadway lanes or roadside. The use of two (or more) anisotropic magnetoresistive (AMR) sensors, which are deployed a short and known distance apart, facilitate the estimation of the speed of the vehicle passing over the AMR sensors. This speed is calculated by finding the delay between two signals, i.e., the time difference. The use of these sensors is an alternative to the inductive loop detectors (ILD) [2].
The error in the estimated speed depends on the characteristics of the used sensors, i.e., a range of measured magnetic induction (it is selectable), operating mode (noise is greater in a low-power mode than in a high-performance mode), and output data rate (it is not greater than 1 kHz) [3].
The way of driving a car over the sensors has an effect on the result. In the ideal case, while a car follows the line of two sensors at a constant speed, the shapes of both signals are the same and their areas under the curves are equal. In reality, it is a rare situation. In addition, environmental conditions, such as temperature changes and background magnetic noise, can cause an additional error.
the Paluknys district in Lithuania. The 32-bit STM32F401RBT6 microcontroller (STMicroelectronics Inc., Santa Clara, CA, USA) is powered by a 3 V DC supply. The sampling rate of this system is 2 kHz. An SPI interface is used for communication between the microcontroller and the sensors. RAW data from sensor nodes along with the pictures of passing vehicles are recorded on a hard disk and stored in a database. The sensor nodes represent the x, y, z components of the magnetic field induction. The exemplary changes in the magnitude of the magnetic field induction are presented in Figure 2. Data access and video stream are enabled by a website and the Long-Term Evolution (LTE) network.
Sensors 2018, 18, x 3 of 12 of this system is 2 kHz. An SPI interface is used for communication between the microcontroller and the sensors. RAW data from sensor nodes along with the pictures of passing vehicles are recorded on a hard disk and stored in a database. The sensor nodes represent the x, y, z components of the magnetic field induction. The exemplary changes in the magnitude of the magnetic field induction are presented in Figure 2. Data access and video stream are enabled by a website and the Long-Term Evolution (LTE) network.

Speed Estimation
When used from a static site, radar speed-measuring devices (down-the-road) display, with an accuracy of +2, −3 km/h, the correct speed of a target vehicle that is traveling at 32 to 160 km/h [11]. Recently, a lot of research has been carried out developing them [12,13].  of this system is 2 kHz. An SPI interface is used for communication between the microcontroller and the sensors. RAW data from sensor nodes along with the pictures of passing vehicles are recorded on a hard disk and stored in a database. The sensor nodes represent the x, y, z components of the magnetic field induction. The exemplary changes in the magnitude of the magnetic field induction are presented in Figure 2. Data access and video stream are enabled by a website and the Long-Term Evolution (LTE) network.

Speed Estimation
When used from a static site, radar speed-measuring devices (down-the-road) display, with an accuracy of +2, −3 km/h, the correct speed of a target vehicle that is traveling at 32 to 160 km/h [11]. Recently, a lot of research has been carried out developing them [12,13].

Speed Estimation
When used from a static site, radar speed-measuring devices (down-the-road) display, with an accuracy of +2, −3 km/h, the correct speed of a target vehicle that is traveling at 32 to 160 km/h [11]. Recently, a lot of research has been carried out developing them [12,13].
Two longitudinally spaced sensor nodes are required to estimate vehicle speed in the system ( Figure 1). The most accurate results possible are obtained by deploying sensors that are further away from each other [5,14]. However, too long of a distance between the sensors involves a risk of signal distortion, which may be caused by a vehicle maneuver. It affects the speed result. This distance is 30 cm in the designed vehicle detection system.
At first, the computations of the vehicle's speed were performed using four algorithms. Input data relate to 200 vehicles of different types that were driven at speeds from 30 to 150 km/h. The respective sample delay varied from 12 to 60 samples.

Methods and Computational Complexity
Speed was evaluated using four methods: • method 1-the location of the maximum value of the cross-correlation function in the time domain [15], • method 2-the location of the minimum value of the SAD function in the time domain [9], • method 3-the location of the maximum value of the circular convolution of two sequences (using DFT and IDFT algorithms) [5,16], • method 4-the difference in gravity (mass) centers of two discrete signals [17].
In method 1, the location of the highest peak of the cross-correlation function f gives the sample delay ∆n, which is recalculated to the time delay ∆t 1 . The time delay is given by the formula where B 1 and B 2 are the magnitudes of the magnetic field signal, N is the sample size, n is the lag (a delay in samples), ∆n is the sample delay, and t s is the sampling period. In method 2, the minimum location of the SAD function g gives the sample delay, which is recalculated to the time delay ∆t 2 : The equation used in method 3 enables the calculation of the linear convolution. The IDFT of the element by element product of the DFTs of the two sequences is computed as follows [5]: Using method 3, the time delay is given by In method 4, the centers of mass of two discrete signals B 1 (n) and B 2 (n) are located at The difference between CoM 1 and CoM 2 gives the sample delay and, after recalculation, the time delay. Each aforesaid method applied for a speed estimation method is characterized by different computational complexities (Table 1). A low complexity of the calculations, in order to implement it in the embedded system, is the main objective. However, the accuracy of the speed estimation must be assured. The other method found in the literature is presented in [10]. It is based on the signal matching and four regions selecting from two signal nodes. The presented algorithm is complicated and is not verified in this paper.

Differences between Results When There Is No Filtering and the Sample Size N is Large
The sample size N, required to compute the speed, was constant and equal to 3000 for every driven vehicle. The large sample size means that the input dataset contains more than 100 values of background noise before and after the impulse response caused by a driven vehicle ( Figure 2). The results obtained by methods 1, 2, 3, and 4 were compared with the reference radar readings. In Figures 3 and 4, we can see these differences in the results that were made for 200 randomly driven vehicles, i.e., small city cars, sedans, wagons, SUVs, trucks, and buses. By comparing Figure 3a with Figure 3b, we can see that the absolute speed errors are smaller when computations are performed with magnitude data rather than with z-component data. Methods 1 and 3 provide the same results in 99% of cases when the magnitude-based data were used to compute speed ( Figure 3a). The scattering of error in the case of methods 1 and 3 ( Figure 3a) is small: the absolute error is less than 3 km/h in 75% of the cases and less than 5 km/h in 94.5% of the cases. The scattering of error in the case of method 2 is higher-less than 3 km/h in 60.5% of cases and less than 5 km/h in 83% of cases.
Method 4 is strongly dependent on the value of threshold, denoted as TR, (Figure 4) as opposed to the other methods. The scattering of magnitude-based differences is large: for 40.5% of traveling vehicles, the difference in samples is more than 3; for 28.5%, more than 5 (when threshold TR is fixed as TR-time mean of background noise).
Methods 1, 2, and 3 are more accurate than method 4. However, they are computationally more complicated.
to the other methods. The scattering of magnitude-based differences is large: for 40.5% of traveling vehicles, the difference in samples is more than 3; for 28.5%, more than 5 (when threshold TR is fixed as TR-time mean of background noise).
Methods 1, 2, and 3 are more accurate than method 4. However, they are computationally more complicated.   . The absolute differences between the estimated value from method 4 with reference to the value from method 3 using TR as a parameter, the magnitude of magnetic field data was used for the calculations.

Differences between Results When Sample Size N Is Small
In practical applications, large and constant sample size N is unacceptable when using methods 1 and 2 because the execution of the speed calculation function takes too much time ( Table 2). Sample size N cannot be too large because sometimes the distance between two driven vehicles on the traffic lane is short (trucks can travel even 10 m one after another at speed 90 km/h) and there is a risk of including two vehicles in one speed estimation sample window. The small sample size means that the input dataset contained no background values and only one segment of the impulse response was caused by a driven vehicle (Figures 5 and 6).
In another extreme situation, the speed estimation function has to process a large amount of data if the vehicle is long (e.g., 16.5 m) and travels at 30 km/h. Then, it is necessary to find a method producing similar results with a smaller sample size.
Unfortunately, the cross-correlation-based methods 1 and 3 are not reliable in the situation when the sample size N is smaller than the number of samples above a fixed threshold ( Figure 5). This results in sample delay (vehicle's speed) computed on the basis of an uncompleted dataset. . The absolute differences between the estimated value from method 4 with reference to the value from method 3 using TR as a parameter, the magnitude of magnetic field data was used for the calculations.

Differences between Results When Sample Size N Is Small
In practical applications, large and constant sample size N is unacceptable when using methods 1 and 2 because the execution of the speed calculation function takes too much time ( Table 2). Sample size N cannot be too large because sometimes the distance between two driven vehicles on the traffic lane is short (trucks can travel even 10 m one after another at speed 90 km/h) and there is a risk of including two vehicles in one speed estimation sample window. The small sample size means that the input dataset contained no background values and only one segment of the impulse response was caused by a driven vehicle (Figures 5 and 6).
In another extreme situation, the speed estimation function has to process a large amount of data if the vehicle is long (e.g., 16.5 m) and travels at 30 km/h. Then, it is necessary to find a method producing similar results with a smaller sample size.
Unfortunately, the cross-correlation-based methods 1 and 3 are not reliable in the situation when the sample size N is smaller than the number of samples above a fixed threshold ( Figure 5). This results in sample delay (vehicle's speed) computed on the basis of an uncompleted dataset. This is visible in Table 2, which presents sample delay estimates for different values of N. In cases 4 and 5, methods 1 and 3 provide incorrect results. In this case, the use of method 4 seems to be a better solution.   Table 2 shows that methods 2 and 4 could be effective if we consider using a constant and short N continuously for the signals of moving cars in traffic, i.e., in every moment when a change in magnetic field magnitude will cross a given threshold. This motivated the authors to compute the sample delay in another way. In Figure 6, it is shown that two neighboring peaks are localized in the magnetic field magnitude of the first sensor. Then, sample size N is equal to N2, i.e., the distance of the samples from the start to the second significant peak in the signal. This is visible in Table 2, which presents sample delay estimates for different values of N. In cases 4 and 5, methods 1 and 3 provide incorrect results. In this case, the use of method 4 seems to be a better solution.   Table 2 shows that methods 2 and 4 could be effective if we consider using a constant and short N continuously for the signals of moving cars in traffic, i.e., in every moment when a change in magnetic field magnitude will cross a given threshold. This motivated the authors to compute the sample delay in another way. In Figure 6, it is shown that two neighboring peaks are localized in the magnetic field magnitude of the first sensor. Then, sample size N is equal to N 2 , i.e., the distance of the samples from the start to the second significant peak in the signal. The results obtained for the same vehicle are presented in Table 3. Once more, methods 2 and 4 carry more correct results than methods 1 and 3.

Improvement in Result Resolution
The reason for improving a result resolution is that the error of estimated speeds higher than 90 km/h is large. A linear interpolation function is based on the lowpass interpolation algorithm [18] and it increases the sample rate of the input signal. In Table 4, the speed results are presented for r = 10 where r is an integer interpolation factor. As presented in Table 4, a 10-times higher resolution is obtained. This results in longer execution times, especially when using methods 1 and 2.  The results obtained for the same vehicle are presented in Table 3. Once more, methods 2 and 4 carry more correct results than methods 1 and 3.

Improvement in Result Resolution
The reason for improving a result resolution is that the error of estimated speeds higher than 90 km/h is large. A linear interpolation function is based on the lowpass interpolation algorithm [18] and it increases the sample rate of the input signal. In Table 4, the speed results are presented for r = 10 where r is an integer interpolation factor. As presented in Table 4, a 10-times higher resolution is obtained. This results in longer execution times, especially when using methods 1 and 2. Cases 7 and 9 were tested for 200 vehicles. For the majority of vehicle signatures, method 2 is more accurate than method 4 when N < N L .
The final and most effective way to estimate sample delay is to fix the threshold as a high percentage of maximum peak value in the first signal. As a result, the computations are based on the short fragments of interpolated signals. This is illustrated in Figure 7. The final and most effective way to estimate sample delay is to fix the threshold as a high percentage of maximum peak value in the first signal. As a result, the computations are based on the short fragments of interpolated signals. This is illustrated in Figure 7.     Table 6 contains the estimated sample delay for 30 vehicles that traveled within a narrow range of speed-on average, about 100 km/h. The mean absolute difference is taken as a measure of dispersion and it is related to the average sample delay. This percentage difference, calculated from case 6, which is assumed as a reference (no interpolation), and case 8 (with interpolation) is 1.20% (when using method 1) and 1.25% (when using method 3). Changing the threshold from 90% to 80% causes no difference in results when using method 1. In the case of method 3, the percentage differences are 6.72% (calculated from case 6 and case 11) and 7.65% (calculated from case 6 and case 10).  Methods 2 and 4 provide incorrect results in cases 10 and 11 (Table 5). Comparing cases 8 and 11, there is little difference in the result values. However, the execution time is about 30 times shorter when using method 1. In case 11, both execution time values are acceptable. For speeds higher than 33.3 km/h, these values do not exceed 60 ms. Table 5. Estimated sample delay and program execution time for a vehicle that traveled at 33.3 km/h (threshold as a percentage of maximum peak value in the first signal).  Table 6 contains the estimated sample delay for 30 vehicles that traveled within a narrow range of speed-on average, about 100 km/h. The mean absolute difference is taken as a measure of dispersion and it is related to the average sample delay. This percentage difference, calculated from case 6, which is assumed as a reference (no interpolation), and case 8 (with interpolation) is 1.20% (when using method 1) and 1.25% (when using method 3). Changing the threshold from 90% to 80% causes no difference in results when using method 1. In the case of method 3, the percentage differences are 6.72% (calculated from case 6 and case 11) and 7.65% (calculated from case 6 and case 10).

The Influence of Filtering on the Sample Delay Value
Filtering signals by using the moving average is a way of smoothing a signal and finding precisely the points where a signal crosses the threshold. Filtering the magnitude-based data should take a short time and should have no influence on a result. As we can see in Figure 8, a ten-sample moving average (AVG = 10) has little impact on the speed estimation result. Considering methods 1 and 3, the result has a difference of about ±1 sample (what is on average ±3 km/h at a speed of 75 km/h) with a probability of 95%.

The Influence of Filtering on the Sample Delay Value
Filtering signals by using the moving average is a way of smoothing a signal and finding precisely the points where a signal crosses the threshold. Filtering the magnitude-based data should take a short time and should have no influence on a result. As we can see in Figure 8, a ten-sample moving average (AVG = 10) has little impact on the speed estimation result. Considering methods 1 and 3, the result has a difference of about ±1 sample (what is on average ±3 km/h at a speed of 75 km/h) with a probability of 95%.

Importance of Speed Estimation
A quick and accurate speed result is needed when a vehicle's length is calculated in the next step of automated vehicle classification systems (Figure 9).

Importance of Speed Estimation
A quick and accurate speed result is needed when a vehicle's length is calculated in the next step of automated vehicle classification systems (Figure 9).
Sensors 2018, 18, x 11 of 12 Figure 9. Computation of sample delay as a preliminary task before a vehicle's length estimation and vehicle classification.

Conclusions
Four algorithms and their computational complexity using a microcontroller with two AMR sensors for estimating the speed of a vehicle are presented in this paper. The analysis performed on the signals from three-axis magnetic field sensors mounted on roadways shows that the magnitudebased signals should be processed rather than z-component ones. The efficiency of speed estimation methods was tested using a dataset of 200 vehicles. Methods 1 and 3 provided almost the same results when the magnitude-based data were used to compute speed. However, method 3 was 5 to 30 times faster. Methods 1 and 3 were the most accurate; the average absolute error was 1.8 km/h (referring to the radar readings). The same error using methods 2 and 4 was 3.8 km/h and 15.2 km/h, respectively. Methods 1 and 3 ensured analogous results in 99% of cases when the sample size N was large. When the sample size was short N << NL and the signal threshold was 80%, the lower dispersion in a set of data was observed when using method 1 (percentage difference 1.20% vs. 6.72% in comparison to method 3). This means the absolute difference was up to 8 km/h at a speed of 100 km/h. When using method 1, the execution time was 4 times longer. However, for a long vehicle travelling at 33.3 km/h, the execution time of 60 ms is a satisfactory result, considering that it is shorter at higher speeds.
The alternative methods 2 and 4 provided correct results when speed estimation based on a segment of signal (when N = N2) was considered, as presented in Figure 5. In this case, using the cross-correlation methods was not reliable. A sensitivity on the threshold level (it should be relatively high) and a reconstruction of signals by interpolation are the drawbacks of method 4.
The speed estimation result is a compromise between sample size and execution time. Depending on the selected technique ( Figure 5, Figure 6 or Figure 7), the SAD method or crosscorrelation in time domain is the best choice if it is necessary to handle a small sample size. The use of linear interpolation resulted in achieving a more precise result (Table 6). However, the interpolation is worth being applied if a vehicle travels at speeds higher than 90 km/h. Method 1, as the most accurate and the slowest, can be applied in the systems based on the high performance microcontrollers, while less accurate and quicker methods 2 or 3 may be implemented in low performance units.

Conclusions
Four algorithms and their computational complexity using a microcontroller with two AMR sensors for estimating the speed of a vehicle are presented in this paper. The analysis performed on the signals from three-axis magnetic field sensors mounted on roadways shows that the magnitude-based signals should be processed rather than z-component ones. The efficiency of speed estimation methods was tested using a dataset of 200 vehicles. Methods 1 and 3 provided almost the same results when the magnitude-based data were used to compute speed. However, method 3 was 5 to 30 times faster. Methods 1 and 3 were the most accurate; the average absolute error was 1.8 km/h (referring to the radar readings). The same error using methods 2 and 4 was 3.8 km/h and 15.2 km/h, respectively. Methods 1 and 3 ensured analogous results in 99% of cases when the sample size N was large. When the sample size was short N << N L and the signal threshold was 80%, the lower dispersion in a set of data was observed when using method 1 (percentage difference 1.20% vs. 6.72% in comparison to method 3). This means the absolute difference was up to 8 km/h at a speed of 100 km/h. When using method 1, the execution time was 4 times longer. However, for a long vehicle travelling at 33.3 km/h, the execution time of 60 ms is a satisfactory result, considering that it is shorter at higher speeds.
The alternative methods 2 and 4 provided correct results when speed estimation based on a segment of signal (when N = N 2 ) was considered, as presented in Figure 5. In this case, using the cross-correlation methods was not reliable. A sensitivity on the threshold level (it should be relatively high) and a reconstruction of signals by interpolation are the drawbacks of method 4.
The speed estimation result is a compromise between sample size and execution time. Depending on the selected technique ( Figure 5, Figure 6 or Figure 7), the SAD method or cross-correlation in time domain is the best choice if it is necessary to handle a small sample size. The use of linear interpolation resulted in achieving a more precise result (Table 6). However, the interpolation is worth being applied if a vehicle travels at speeds higher than 90 km/h. Method 1, as the most accurate and the slowest, can be applied in the systems based on the high performance microcontrollers, while less accurate and quicker methods 2 or 3 may be implemented in low performance units.