代码适用于Java但不适用于Android项目

时间:2017-12-27 21:57:38

标签: java android

所以我有一些传感器数据,我需要在我的应用程序中平滑。当我测试它时,它不起作用。它会通过在不同的地方插入错误的值,跳过一些等等而失败。但是当我使用调试器运行代码时,它确实按预期工作!我也尝试将它复制到一个Java项目,它似乎在那里工作得很好!几乎感觉有些线程使用相同的变量或其他什么,我不太了解Android。 这是Java代码,对于我的Android项目来说是相同的,当然除了打印语句:

package com.company;

public class Main {
    private static final int SMOOTH_N = 3;

    public static void main(String[] args) {
    new Main();
    }

    public Main(){
        int[][] x = testRawGestureData();

        System.out.println("Before normal");
        printData(x);
        x = normalizeData(x);
        System.out.println("After normal");
        printData(x);
    }

    /**
     * Using moving window to smooth the data by averaging.
     *
     * @param rawGestureData
     */
    private int[][] normalizeData(int[][] rawGestureData) {
        int[][] tmpArray = new int[22][6]; // Temporary array with two extra rows at the end to help with the smoothing.
        int[][] smoothedGestureData = new int[20][6];

        //Copies raw gesture data to tmpArray.
        for (int i = 0; i < rawGestureData.length; i++) {
            System.arraycopy(rawGestureData[i], 0, tmpArray[i], 0, rawGestureData[i].length);
        }

        //Adds 2 extra rows to the tmpArray.
        System.arraycopy(rawGestureData, 18, tmpArray, 20, smoothedGestureData.length - 18);

        //For each column...
        for (int col = 0; col < 6; col++) {
            int sum = 0;
            int modulusCounter = 1;
            int smoothedRowCounter = 0;

            //For each row...
            for (int row = 0; row < tmpArray.length; row++) {
                sum += tmpArray[row][col];
                System.out.println("rawVal: " + String.valueOf(tmpArray[row][col]));
                //For every third row...
                if (modulusCounter % SMOOTH_N == 0) {
                    int average = Math.round(sum / SMOOTH_N);
                    System.out.println("AVG: " + average);
                    smoothedGestureData[smoothedRowCounter++][col] = average;
                    sum = 0;
                    System.out.println("SMOOTHARRAY");
                    printData(smoothedGestureData);

                    if(row<21){ //Fixes the trouble at the end
                        row -= 2; //Reset the row one step back.
                    }

                    if(row == 13){
                        System.out.println("BREAK");
                    }
                    System.out.println("ROW: " + String.valueOf(row));
                }
                modulusCounter++;
            }
        }
        printData(smoothedGestureData);
        return smoothedGestureData;
    }

    private int[][] testRawGestureData() {
        Random rand = new Random();

        int[][] rawGestureData = new int[20][6];
        for (int i = 0; i < rawGestureData.length; i++) {
            for (int j = 0; j < rawGestureData[i].length; j++) {
                int nbr = i + j + 1;
                rawGestureData[i][j] = nbr;
            }
        }

        return rawGestureData;
    }

    //Print the gesture array.
    private void printData(int[][] rawGestureData) {
        for (int[] row : rawGestureData) {
            StringBuilder currentRow = new StringBuilder();
            for (int measurementData : row) {
                currentRow.append(String.valueOf(measurementData)).append(",");
            }
            System.out.println(currentRow);
        }
        System.out.println("Done printing array");
    }
}

这将给我结果:

Before normal
1,2,3,4,5,6,
2,3,4,5,6,7,
3,4,5,6,7,8,
4,5,6,7,8,9,
5,6,7,8,9,10,
6,7,8,9,10,11,
7,8,9,10,11,12,
8,9,10,11,12,13,
9,10,11,12,13,14,
10,11,12,13,14,15,
11,12,13,14,15,16,
12,13,14,15,16,17,
13,14,15,16,17,18,
14,15,16,17,18,19,
15,16,17,18,19,20,
16,17,18,19,20,21,
17,18,19,20,21,22,
18,19,20,21,22,23,
19,20,21,22,23,24,
20,21,22,23,24,25,
Done printing array

After normal
2,3,4,5,6,7,
3,4,5,6,7,8,
4,5,6,7,8,9,
5,6,7,8,9,10,
6,7,8,9,10,11,
7,8,9,10,11,12,
8,9,10,11,12,13,
9,10,11,12,13,14,
10,11,12,13,14,15,
11,12,13,14,15,16,
12,13,14,15,16,17,
13,14,15,16,17,18,
14,15,16,17,18,19,
15,16,17,18,19,20,
16,17,18,19,20,21,
17,18,19,20,21,22,
18,19,20,21,22,23,
19,20,21,22,23,24,
19,20,21,22,23,24,
19,20,21,22,23,24,
Done printing array

在android中我得到了这个:

    Before normal
    1,2,3,4,5,6,
    2,3,4,5,6,7,
    3,4,5,6,7,8,
    4,5,6,7,8,9,
    5,6,7,8,9,10,
    6,7,8,9,10,11,
    7,8,9,10,11,12,
    8,9,10,11,12,13,
    9,10,11,12,13,14,
    10,11,12,13,14,15,
    11,12,13,14,15,16,
    12,13,14,15,16,17,
    13,14,15,16,17,18,
    14,15,16,17,18,19,
    15,16,17,18,19,20,
    16,17,18,19,20,21,
    17,18,19,20,21,22,
    18,19,20,21,22,23,
    19,20,21,22,23,24,
    20,21,22,23,24,25,
    Done printing array

ataNormalizer: 13,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: Done printing array
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: ROW: 11
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 13
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 14
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 15
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: Ave 14
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: SmoothArray
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 2,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 3,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 4,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 5,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 6,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 7,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 8,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 9,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 10,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 11,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 12,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 13,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 14,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: 0,0,0,0,0,0,
12-27 22:56:24.366 23923-23923/com.project.iotap.iotap D/DataNormalizer: Done printing array
12-27 22:56:24.367 23923-23923/com.project.iotap.iotap D/DataNormalizer: 3,0,0,0,0,0,
12-27 22:56:24.368 23923-23923/com.project.iotap.iotap D/DataNormalizer: 18,0,0,0,0,0,
12-27 22:56:24.369 23923-23923/com.project.iotap.iotap D/DataNormalizer: 2,3,0,0,0,0,
12-27 22:56:24.370 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 7
12-27 22:56:24.371 23923-23923/com.project.iotap.iotap D/DataNormalizer: 12,0,0,0,0,0,
12-27 22:56:24.372 23923-23923/com.project.iotap.iotap D/DataNormalizer: Done printing array
12-27 22:56:24.372 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 12
12-27 22:56:24.373 23923-23923/com.project.iotap.iotap D/DataNormalizer: SmoothArray
12-27 22:56:24.373 23923-23923/com.project.iotap.iotap D/DataNormalizer: 18,0,0,0,0,0,
12-27 22:56:24.374 23923-23923/com.project.iotap.iotap D/DataNormalizer: 12,13,0,0,0,0,
12-27 22:56:24.374 23923-23923/com.project.iotap.iotap D/DataNormalizer: 18,0,0,0,0,0,
12-27 22:56:24.376 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 5
12-27 22:56:24.379 23923-23923/com.project.iotap.iotap D/DataNormalizer: Ave 13
12-27 22:56:24.380 23923-23923/com.project.iotap.iotap D/DataNormalizer: 15,16,0,0,0,0,
12-27 22:56:24.381 23923-23923/com.project.iotap.iotap D/DataNormalizer: Done printing array
12-27 22:56:24.381 23923-23923/com.project.iotap.iotap D/DataNormalizer: SmoothArray
12-27 22:56:24.382 23923-23923/com.project.iotap.iotap D/DataNormalizer: SmoothArray
12-27 22:56:24.382 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 19
12-27 22:56:24.382 23923-23923/com.project.iotap.iotap D/DataNormalizer: SmoothArray
12-27 22:56:24.382 23923-23923/com.project.iotap.iotap D/DataNormalizer: 16,17,18,0,0,0,
12-27 22:56:24.383 23923-23923/com.project.iotap.iotap D/DataNormalizer: 19,20,21,0,0,0,
12-27 22:56:24.383 23923-23923/com.project.iotap.iotap D/DataNormalizer: 7,8,9,0,0,0,
12-27 22:56:24.383 23923-23923/com.project.iotap.iotap D/DataNormalizer: 8,9,10,0,0,0,
12-27 22:56:24.383 23923-23923/com.project.iotap.iotap D/DataNormalizer: ROW: 21
12-27 22:56:24.384 23923-23923/com.project.iotap.iotap D/DataNormalizer: 7,8,9,0,0,0,
12-27 22:56:24.384 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 5
12-27 22:56:24.384 23923-23923/com.project.iotap.iotap D/DataNormalizer: 10,11,12,0,0,0,
12-27 22:56:24.384 23923-23923/com.project.iotap.iotap D/DataNormalizer: 19,20,21,0,0,0,
12-27 22:56:24.384 23923-23923/com.project.iotap.iotap D/DataNormalizer: 8,9,10,0,0,0,
12-27 22:56:24.384 23923-23923/com.project.iotap.iotap D/DataNormalizer: 11,12,13,0,0,0,
12-27 22:56:24.385 23923-23923/com.project.iotap.iotap D/DataNormalizer: 17,18,19,0,0,0,
12-27 22:56:24.385 23923-23923/com.project.iotap.iotap D/DataNormalizer: 4,5,6,7,0,0,
12-27 22:56:24.386 23923-23923/com.project.iotap.iotap D/DataNormalizer: 16,17,18,0,0,0,
12-27 22:56:24.386 23923-23923/com.project.iotap.iotap D/DataNormalizer: 2,3,4,5,0,0,
12-27 22:56:24.386 23923-23923/com.project.iotap.iotap D/DataNormalizer: 9,10,11,0,0,0,
12-27 22:56:24.386 23923-23923/com.project.iotap.iotap D/DataNormalizer: 19,20,21,0,0,0,
12-27 22:56:24.387 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 13
12-27 22:56:24.387 23923-23923/com.project.iotap.iotap D/DataNormalizer: Ave 13
12-27 22:56:24.388 23923-23923/com.project.iotap.iotap D/DataNormalizer: 4,5,6,7,0,0,
12-27 22:56:24.388 23923-23923/com.project.iotap.iotap D/DataNormalizer: 4,5,6,7,0,0,
12-27 22:56:24.388 23923-23923/com.project.iotap.iotap D/DataNormalizer: 8,9,10,11,0,0,
12-27 22:56:24.388 23923-23923/com.project.iotap.iotap D/DataNormalizer: 13,14,15,16,0,0,
12-27 22:56:24.390 23923-23923/com.project.iotap.iotap D/DataNormalizer: 6,7,8,9,0,0,
12-27 22:56:24.390 23923-23923/com.project.iotap.iotap D/DataNormalizer: 8,9,10,11,0,0,
12-27 22:56:24.390 23923-23923/com.project.iotap.iotap D/DataNormalizer: 10,11,12,13,0,0,
12-27 22:56:24.391 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 23
12-27 22:56:24.391 23923-23923/com.project.iotap.iotap D/DataNormalizer: 11,12,13,14,0,0,
12-27 22:56:24.391 23923-23923/com.project.iotap.iotap D/DataNormalizer: 17,18,19,20,0,0,
12-27 22:56:24.392 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 7
12-27 22:56:24.392 23923-23923/com.project.iotap.iotap D/DataNormalizer: ROW: 1
12-27 22:56:24.392 23923-23923/com.project.iotap.iotap D/DataNormalizer: 7,8,9,10,0,0,
12-27 22:56:24.392 23923-23923/com.project.iotap.iotap D/DataNormalizer: 9,10,11,12,0,0,
12-27 22:56:24.393 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 12
12-27 22:56:24.396 23923-23923/com.project.iotap.iotap D/DataNormalizer: 19,20,21,22,0,0,
12-27 22:56:24.396 23923-23923/com.project.iotap.iotap D/DataNormalizer: 14,15,16,17,18,0,
12-27 22:56:24.396 23923-23923/com.project.iotap.iotap D/DataNormalizer: 19,20,21,22,0,0,
12-27 22:56:24.396 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 19
12-27 22:56:24.397 23923-23923/com.project.iotap.iotap D/DataNormalizer: 19,20,21,22,0,0,
12-27 22:56:24.397 23923-23923/com.project.iotap.iotap D/DataNormalizer: SmoothArray
12-27 22:56:24.397 23923-23923/com.project.iotap.iotap D/DataNormalizer: 14,15,16,17,18,0,
12-27 22:56:24.398 23923-23923/com.project.iotap.iotap D/DataNormalizer: 8,9,10,11,12,0,
12-27 22:56:24.398 23923-23923/com.project.iotap.iotap D/DataNormalizer: 19,20,21,22,0,0,
12-27 22:56:24.401 23923-23923/com.project.iotap.iotap D/DataNormalizer: 9,10,11,12,13,0,
12-27 22:56:24.402 23923-23923/com.project.iotap.iotap D/DataNormalizer: 12,13,14,15,16,0,
12-27 22:56:24.402 23923-23923/com.project.iotap.iotap D/DataNormalizer: Ave 16
12-27 22:56:24.402 23923-23923/com.project.iotap.iotap D/DataNormalizer: 12,13,14,15,16,0,
12-27 22:56:24.404 23923-23923/com.project.iotap.iotap D/DataNormalizer: ROW: 12
12-27 22:56:24.404 23923-23923/com.project.iotap.iotap D/DataNormalizer: Ave 21
12-27 22:56:24.405 23923-23923/com.project.iotap.iotap D/DataNormalizer: rawVal: 24
12-27 22:56:24.405 23923-23923/com.project.iotap.iotap D/DataNormalizer: 10,11,12,13,14,15,
12-27 22:56:24.405 23923-23923/com.project.iotap.iotap D/DataNormalizer: 12,13,14,15,16,17,

真正让我恼火的是,它在使用调试器运行应用程序时确实有效,因此很难调试。

0 个答案:

没有答案