@Override
 protected void onHandleIntent(Intent intent) {
   Log.d(TAG, "onHandleIntent()");
   String[] s = intent.getStringArrayExtra("lastData");
   MyUtil.writeToSDFile(s, "ActivityRegistrator/record", "sensorData_.txt", true);
   Log.d(TAG, "saved file");
 }
예제 #2
0
  public static boolean generateTrainingFile(
      String sensorDataFolder,
      String sensorDataFile,
      String sensorMarksFolder,
      String sensorMarksFile,
      String outFolder,
      String outFile,
      Context context) {
    IWindowData windowData = new WindowHalfOverlap(128);
    IFeatures myFeatures = new MyFeatures();

    Scanner scanSD = null, scanSM = null;
    File sensorData = MyUtil.getSDFile(sensorDataFolder, sensorDataFile);
    File sensorMarks = MyUtil.getSDFile(sensorMarksFolder, sensorMarksFile);

    try {
      scanSD = new Scanner(sensorData);
      scanSD.useLocale(Locale.US);
    } catch (FileNotFoundException e1) {
      return false;
    }

    try {
      scanSM = new Scanner(sensorMarks);
      scanSM.useLocale(Locale.US);
    } catch (FileNotFoundException e1) {
      scanSD.close();
      return false;
    }

    // progress bar
    int total = getNumberOfLines(sensorData);

    /*
       int total = 0;
       float mean = 0;
       boolean correct = getNumberOfLines(sensorData, total, mean);
       if(!correct){
       	scanSM.close();
       	scanSD.close();
       	return false;
       }

       Intent intent = new Intent("com.example.activityregistrator.UPDATE_HZ");
    intent.putExtra("hz", 1/mean);
    context.sendBroadcast(intent);
    */
    float done = 0;

    Long prevTimeSM, postTimeSM, timeSD = -1l, lastTimeSD = 0l, firstTimeSD = -1l;
    int prevLabel, postLabel;

    if (scanSM.hasNextLine()) {
      scanSM.nextLine();
      prevTimeSM = scanSM.nextLong();
      prevLabel = scanSM.nextInt();

      scanSD.nextLine();
      Intent intent = new Intent("com.example.activityregistrator.UPDATE_PROGRESS");
      intent.putExtra("progress", (float) ((++done) / total) * 100);
      context.sendBroadcast(intent);

      // find next time and label
      while (scanSM.hasNextLine()) {
        scanSM.nextLine();
        if (!scanSM.hasNextLong()) break;

        Log.d(TAG, "prevTimeSM: " + prevTimeSM + "; prevLabel: " + prevLabel);

        postTimeSM = scanSM.nextLong();
        postLabel = scanSM.nextInt();

        Log.d(TAG, "postTimeSM: " + postTimeSM + "; postLabel: " + postLabel);

        // each window has unique label
        windowData.clean();

        // search windowData beginning
        // assumption: register only label>0  -->  IGNORE<=0
        while (prevLabel > 0) {

          if (timeSD < 0) {
            if (!scanSD.hasNextLong()) {
              break;
            }
            timeSD = scanSD.nextLong();
          }

          // used to calculate hz
          lastTimeSD = timeSD;
          if (firstTimeSD < 0) firstTimeSD = timeSD;

          if (timeSD >= prevTimeSM) {
            if (timeSD <= postTimeSM) {
              Log.d(TAG, "timeSD: " + timeSD);

              // add x y z to windowData
              float[] linearAccel = {scanSD.nextFloat(), scanSD.nextFloat(), scanSD.nextFloat()};

              if (windowData.addData((float) vecLength(linearAccel))) {
                // we have a complete windowData

                // calculate features
                float[] features = myFeatures.getFeatures(windowData);

                // save to libsvm file with the correct label
                String[] textToFile = new String[1];
                textToFile[0] = prevLabel + " ";
                for (int i = 0; i < features.length; i++) {
                  textToFile[0] += (i + 1) + ":" + features[i] + " ";
                }
                MyUtil.writeToSDFile(textToFile, outFolder, outFile, true);
              }

              if (scanSD.hasNextLine()) {
                scanSD.nextLine();
                intent = new Intent("com.example.activityregistrator.UPDATE_PROGRESS");
                intent.putExtra("progress", (float) ((++done) / total) * 100);
                context.sendBroadcast(intent);
                timeSD = -1l; // get nextLong()
              } else {
                break;
              }
            } else {
              break;
            }
          } else {
            if (scanSD.hasNextLine()) {
              scanSD.nextLine();
              intent = new Intent("com.example.activityregistrator.UPDATE_PROGRESS");
              intent.putExtra("progress", (float) ((++done) / total) * 100);
              context.sendBroadcast(intent);
              timeSD = -1l; // get nextLong()
            } else {
              break;
            }
          }
        }

        prevTimeSM = postTimeSM;
        prevLabel = postLabel;
      }
    }

    scanSD.close();
    scanSM.close();

    Intent intent = new Intent("com.example.activityregistrator.UPDATE_HZ");
    intent.putExtra("hz", done / (((float) (lastTimeSD - firstTimeSD)) / 1000f));
    context.sendBroadcast(intent);

    Log.d(TAG, "[firstTimeSD,lastTimeSD,done]: " + firstTimeSD + " " + lastTimeSD + " " + done);

    return true;
  }