Code example for LinkedList

Methods: add, get, poll, size

0
         
        SensorData entry = new SensorData();
        entry.timestamp = event.timestamp;
        entry.value = scalar;
         
        mSensorValues.add(entry);
         
        // const window size for moving average  
        movingAverageOpt = movingMeanOpt(movingAverageOpt);
        //float movingAverage = movingMean(); 
         
        //checkSignChange(movingAverage); 
        checkSignChange(movingAverageOpt);
         
        countSteps(); 
        // DEBUG ONLY 
        //int steps = countSteps(); 
        //Log.i("SensorReader", entry.timestamp + ":" + String.valueOf(entry.value) + ", steps: " 
        //	+ String.valueOf(steps) +":" + String.valueOf(movingAverageOpt)); 
        // + ":" + String.valueOf(movingAverage)); 
         
        /* 
        try { 
        	mLogStream = new FileWriter(mLogFile, true); 
            mLogStream.write(entry.timestamp + "," + String.valueOf(entry.value) + "," 
            		+ String.valueOf(mExponentialMovingAverage) + "," 
            		+ String.valueOf(movingAverage) + "," 
            		+ String.valueOf(steps) + "\n"); 
            mLogStream.close(); 
        } catch(IOException e) { 
            Log.e("SensorReader", "Could not write file " + e.getMessage());  
        }*/ 
	} 
	 
    /** 
     * only needed to fit interface implementation needs 
     */ 
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// not needed 
	} 
	 
	/** 
	 *	clear stored sensor data 
	 */ 
	public void clear() { 
		mSensorValues.clear();
	} 
	 
	/** 
	 * calculate movingMean of the previous m values 
	 */ 
	private float movingMean() { 
		Float sum = (float)0;
		int n = mSensorValues.size();
		int start = n > mWindowSizeMovingAverage ? n-mWindowSizeMovingAverage : 0; 
		for (int i=start; i < n; ++i) {
		   sum += mSensorValues.get(i).value;
		} 
		return ((float)(sum/(n-start)));
	} 
	 
	/** 
	 * compute the moving mean without loops by updating 
	 */ 
	private float movingMeanOpt(float curMovingMean){
		int n = mSensorValues.size();
		int start = n > mWindowSizeMovingAverage ? n-mWindowSizeMovingAverage : 0;
		if (start > 0){
			curMovingMean = curMovingMean - mSensorValues.get(start-1).value/mWindowSizeMovingAverage;
			return ((float)(curMovingMean+mSensorValues.get(n-1).value/mWindowSizeMovingAverage));
		} 
		else{ 
			return ((float)((curMovingMean*(n-1)+mSensorValues.get(n-1).value)/n));
		} 
		} 
	 
	/** 
	 * calculate exponential weighted moving average 
	 */ 
	private float ewma(float scalar) {
        mExponentialMovingAverage = mAlpha * scalar + (1-mAlpha) * mExponentialMovingAverage;
        return mExponentialMovingAverage;
	} 
	 
	/** 
	 * check sign change 
	 */ 
	private void checkSignChange(float movingAverage) {
		int n = mSensorValues.size();
		float pre = mExponentialMovingAverage - movingAverage;
		float post = ewma(mSensorValues.get(n-1).value) - movingAverage;
		 
		boolean isSignChanged = Math.abs(( Math.abs(pre+post) - Math.abs(pre) - Math.abs(post) )) > 0.001 &&
				// zero if pre and post have the same sign 
				movingAverage > mGravityThreshold;
				 
		mSensorValues.get(n-1).sign = isSignChanged;
		 
		if (isSignChanged) {
			mNumberOfChangedSigns+=1;
		} 
	} 
	 
	/** 
	 * count steps 
	 */ 
	private int countSteps() { 
		int steps=0;
		int n = mSensorValues.size();
		double deltaTime = mSensorValues.get(n-1).timestamp - mSensorValues.get(0).timestamp;
		deltaTime /= 1000000000; // nanosec -> sec
		 
		//Log.i("deltaTime", String.valueOf(deltaTime)); 
		//Log.i("n", String.valueOf(n)); 
		 
		if (deltaTime > mTimeInterval) {
			//Log.i("> mTimeInterval", String.valueOf(deltaTime)); 
			/*for (int i=0; i<n; ++i) { 
				if (mSensorValues.get(i).sign) { 
					steps+=1; 
				} 
			}*/ 
			steps = mNumberOfChangedSigns/2; // 2 Null-Durchgänge je Schritt
			steps *= (int)Math.ceil(60/mTimeInterval);
			 
			// don't send every change 
			long currentTimeStamp = System.currentTimeMillis();
			if ( ((currentTimeStamp - mLastChangedSend) / 1000 ) > mStepSendInterval) {
				mLastChangedSend = currentTimeStamp;
				mStepChangeListener.onStepChanged(steps);
				//Log.i("StepChangeListener", "onChanged"); 
			} 
			 
			if (mSensorValues.get(0).sign) {
				mNumberOfChangedSigns-=1;
			} 
			 
			mSensorValues.poll();
		} 
		return steps;
	} 
};