Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can Anyone Tell me how this code works. It is an android app that detects steps

ID: 658657 • Letter: C

Question

Can Anyone Tell me how this code works. It is an android app that detects steps using acclerameter data.

//Code Starts Here

public class StepDetector implements SensorEventListener
{
    private final static String TAG = "StepDetector";
    private float   mLimit = 10;
    private float   mLastValues[] = new float[3*2];
    private float   mScale[] = new float[2];
    private float   mYOffset;

    private float   mLastDirections[] = new float[3*2];
    private float   mLastExtremes[][] = { new float[3*2], new float[3*2] };
    private float   mLastDiff[] = new float[3*2];
    private int     mLastMatch = -1;
  
    private ArrayList<StepListener> mStepListeners = new ArrayList<StepListener>();
  
    public StepDetector() {
        int h = 480; // TODO: remove this constant //--------------------------------------------
        mYOffset = h * 0.5f;
        mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
        mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
    }
  
    public void setSensitivity(float sensitivity) {
        mLimit = sensitivity; // 1.97 2.96 4.44 6.66 10.00 15.00 22.50 33.75 50.62
    }
  
    public void addStepListener(StepListener sl) {
        mStepListeners.add(sl);
    }

    public void clearStepListeners() {
        mStepListeners.clear();
    }

    //---------------------------------------------------
    public void onSensorChanged(int sensor, float[] values) {
        synchronized (this) {
            if (sensor == Sensor.TYPE_ORIENTATION) {
            }
            else {
                int j = (sensor == Sensor.TYPE_ACCELEROMETER) ? 1 : 0;
                if (j == 1) {
                    float vSum = 0;
                    for (int i=0 ; i<3 ; i++) {
                        final float v = mYOffset + values[i] * mScale[j];
                        vSum += v;
                    }
                    int k = 0;
                    float v = vSum / 3;

                    float direction = (v > mLastValues[k] ? 1 : (v < mLastValues[k] ? -1 : 0));
                    if (direction == - mLastDirections[k]) {
                        // Direction changed
                        int extType = (direction > 0 ? 0 : 1); // minumum or maximum?
                        mLastExtremes[extType][k] = mLastValues[k];
                        float diff = Math.abs(mLastExtremes[extType][k] - mLastExtremes[1 - extType][k]);

                        if (diff > mLimit) {

                            boolean isAlmostAsLargeAsPrevious = diff > (mLastDiff[k]*2/3);
                            boolean isPreviousLargeEnough = mLastDiff[k] > (diff/3);
                            boolean isNotContra = (mLastMatch != 1 - extType);

                            if (isAlmostAsLargeAsPrevious && isPreviousLargeEnough && isNotContra) {
                                Log.i(TAG, "step");
                                for (StepListener stepListener : mStepListeners) {
                                    stepListener.onStep();
                                }
                                mLastMatch = extType;
                            }
                            else {
                                mLastMatch = -1;
                            }
                        }
                        mLastDiff[k] = diff;
                    }
                    mLastDirections[k] = direction;
                    mLastValues[k] = v;
                }
            }
        }
    }


    public void onSensorChanged(SensorEvent event) {
        onSensorChanged(event.sensor.getType(), event.values);
    }
  
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub
    }

}

Explanation / Answer

The code basically creates a class that handles the SensorEvents and help to determine if they are a step or not.

Below is the code with proper inline comments beside the code so that you understand what the code does and how it works:-

//Code Starts Here
public class StepDetector implements SensorEventListener   //define class StepDectector which implements interface SensorEventListener     
{
private final static String TAG = "StepDetector";   //declare private data members
private float mLimit = 10;
private float mLastValues[] = new float[3*2];
private float mScale[] = new float[2];
private float mYOffset;
private float mLastDirections[] = new float[3*2];
private float mLastExtremes[][] = { new float[3*2], new float[3*2] };
private float mLastDiff[] = new float[3*2];
private int mLastMatch = -1;
  
private ArrayList<StepListener> mStepListeners = new ArrayList<StepListener>();   //create an arraylist of StepListener object type
  
public StepDetector() {   //constructor to initialize the values
int h = 480; // TODO: remove this constant //--------------------------------------------
mYOffset = h * 0.5f;
mScale[0] = - (h * 0.5f * (1.0f / (SensorManager.STANDARD_GRAVITY * 2)));
mScale[1] = - (h * 0.5f * (1.0f / (SensorManager.MAGNETIC_FIELD_EARTH_MAX)));
}
  
public void setSensitivity(float sensitivity) {   //setter method to set the sensitivity value
mLimit = sensitivity; // 1.97 2.96 4.44 6.66 10.00 15.00 22.50 33.75 50.62
}
  
public void addStepListener(StepListener sl) {   //add a listener to StepListener object
mStepListeners.add(sl);
}
public void clearStepListeners() {   //clear the listener from StepListener objects
mStepListeners.clear();
}
//---------------------------------------------------
public void onSensorChanged(int sensor, float[] values) {   //execute this method when listener detects the sensor is changed, it detects steps and notifies all the listeners that implement StepListener.
synchronized (this) {
if (sensor == Sensor.TYPE_ORIENTATION) {
}
else {
int j = (sensor == Sensor.TYPE_ACCELEROMETER) ? 1 : 0;
if (j == 1) {
float vSum = 0;
for (int i=0 ; i<3 ; i++) {
final float v = mYOffset + values[i] * mScale[j];
vSum += v;
}
int k = 0;
float v = vSum / 3;
float direction = (v > mLastValues[k] ? 1 : (v < mLastValues[k] ? -1 : 0));
if (direction == - mLastDirections[k]) {
// Direction changed
int extType = (direction > 0 ? 0 : 1); // minumum or maximum?
mLastExtremes[extType][k] = mLastValues[k];
float diff = Math.abs(mLastExtremes[extType][k] - mLastExtremes[1 - extType][k]);
if (diff > mLimit) {
boolean isAlmostAsLargeAsPrevious = diff > (mLastDiff[k]*2/3);
boolean isPreviousLargeEnough = mLastDiff[k] > (diff/3);
boolean isNotContra = (mLastMatch != 1 - extType);
if (isAlmostAsLargeAsPrevious && isPreviousLargeEnough && isNotContra) {
Log.i(TAG, "step");
for (StepListener stepListener : mStepListeners) {
stepListener.onStep();
}
mLastMatch = extType;
}
else {
mLastMatch = -1;
}
}
mLastDiff[k] = diff;
}
mLastDirections[k] = direction;
mLastValues[k] = v;
}
}
}
}

public void onSensorChanged(SensorEvent event) {   //execute this method when listener detects the sensor is changed. it is an overridden method with SensorEvent object as parameter
onSensorChanged(event.sensor.getType(), event.values);   //call the onSensorChanged(int,float[]) method by passing the required arguments
}
  
public void onAccuracyChanged(Sensor sensor, int accuracy) {   //execute this method when listener detects the accuracy is changed
// TODO Auto-generated method stub
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote