Time: 2018.3.2 11:58Copy the code

1. Step counting algorithm

There are a lot of resources on the Internet for detailed step counting algorithm. Here is just a simple introduction of the experimental nature, which is not very accurate. In addition, there is no test for detailed compatibility.

Function: Obtain the sensor information on the mobile phone through JS to calculate the simple statistics of steps.

The accelerometer module is used. By analyzing the value recorded by the acceleration sensor module of the phone, the user’s steps are obtained. Step counting algorithm mainly stores the square and open root of three directions of acceleration as basic information. This basic information, if used, is probably a waveform diagram, with a time difference between the values, so that we can most likely transform the fuzzy data into a visual

Something like that

And then you take the number of crests and you tell how many steps you took. I didn’t know what to do at first, an array well it’s probably a large array, how can you treat it like a waveform that the human eye sees? It’s basically how to express what I want to do in code, so that the program can understand what I want to get. Well, it was a long struggle. After the boss’s point, it is not necessary to say that specific data is needed, because the overall data is the trend of the waveform, so there will be an increase and decrease, take its trend, when the increase is 1, when the decrease is -1, and then according to the specific experiment to determine the probability of how many 1 or -1 represents a step. The next is to write out the code, well is not very accurate, temporarily remember, later to change. The code based on MUI framework is as follows:

    var orline = [];
var nowline = null;
var sqr = 0;
var step = 0;
function counterStep(){
	var startTime = new Date().getTime();
	wid = plus.accelerometer.watchAcceleration(function(a) { var currTime = new Date().getTime(); Var diffTime = currtime-startTime; // Current time minus initial time to get current time difference // console.log('Monitor message:'+diffTime);
		sqr = Math.sqrt(a.xAxis * a.xAxis + a.yAxis * a.yAxis + a.zAxis * a.zAxis);
		if(nowLine) {// Trend judgment is performed only when there is an initial valueifOrline.push (1); (SQR >= nowLine) {orline.push(1); Nowline = SQR; // Update the comparison value}else if(SQR < nowline) {orline.push(-1) // If less than orline.push(-1) // the trend is reduced, and the trend is -1 nowline = SQR // update the comparison value}}else{// If the initial value does not exist, record the current data and record the current trend value as 0 nowline = SQR; orline.push(0); }},function(e){},{
		frequency: 22
	});
}

function watchPause() {if (orline.length > 300) {
			console.log(orline);
			var x = y = 0;
			for ( var i = 0; i < orline.length; i++) {
				if(orline[i] == 1) { x++; y=0; x >= 7? step++:step=step; }else if (orline[i] == -1) {
					x=0;
					y++;
				}
			}
			console.log(step/2);
			$('.rsib_icon_step').html(parseInt(step/2));
		}
	plus.accelerometer.clearWatch(wid);
}

Copy the code

reference

1. A MEMS step counting algorithm based on adaptive wave peak detection