📄 dataglovefilereader.java
字号:
} if ((row < 0) || (column < 0)) { throw new IllegalArgumentException( "Row and column must be greater than or equal to 0"); } if (row >= numRowsPerSample) { throw new IllegalArgumentException("Row " + row + " does not exist. There are " + numRowsPerSample + " rows per sample in the file."); } fileRead = new FileReader(sourceFile); bufRead = new BufferedReader(fileRead); List values = new ArrayList(); int indexInSample = 0; float curTimeStamp = 0.0f; float curSample = 0.0f; int sampleCount = 0; float[] flArray = null; String li = null; while ((li = bufRead.readLine()) != null) { if (li.length() == 0) { // store sample value, but check if samples have to be filled in // in order to get a proper fixed rate track if ((curTimeStamp - (sampleCount * msPerSample)) > (msPerSample / 2)) { LOG.info("Adding fill-in at sample: " + sampleCount); values.add(new Float(curSample)); sampleCount++; } values.add(new Float(curSample)); // begin of new sample reset... indexInSample = 0; sampleCount++; curSample = 0.0f; } else { if (li.charAt(0) == '#') { continue; } if (indexInSample == 0) { flArray = toFloatArray(li); if (flArray.length >= 2) { //should be of length 3 curTimeStamp = flArray[1]; } if (row == 0) { curSample = flArray[column]; } } else if (indexInSample == row) { flArray = toFloatArray(li); if (flArray.length > column) { curSample = flArray[column]; } } indexInSample++; } } LOG.info("Number of samples: " + sampleCount); try { bufRead.close(); } catch (IOException ioe) { } float[] result = new float[sampleCount]; Float fl; for (int i = 0; i < sampleCount; i++) { fl = (Float) values.get(i); result[i] = fl.floatValue(); //if (i < 20) { // System.out.println("i: " + i + " v: " + fl.floatValue()); //} } return result; } /** * Reads a track from the specified cell while applying derivation * calculations. * * @param row row index in sample, zero based * @param column column index in the row, zero based * @param derivative the level of derivation (in time), 0 means the raw * values * * @return a float array * * @throws IOException * @throws IllegalArgumentException DOCUMENT ME! */ public Object readTrack(int row, int column, int derivative) throws IOException { if (!validLogFile) { return null; } if (derivative == 0) { return readTrack(row, column); } if ((row < 0) || (column < 0)) { throw new IllegalArgumentException( "Row and column must be greater than or equal to 0"); } if (row >= numRowsPerSample) { throw new IllegalArgumentException("Row " + row + " does not exist. There are " + numRowsPerSample + " rows per sample in the file."); } fileRead = new FileReader(sourceFile); bufRead = new BufferedReader(fileRead); List values = new ArrayList(); int indexInSample = 0; float curTimeStamp = 0.0f; float curSample = 0.0f; int sampleCount = 0; float[] flArray = null; String li = null; // two dimensional array for iteration; reused for storage // of previous sample float[][] derivArray = new float[derivative + 1][2]; while ((li = bufRead.readLine()) != null) { if (li.length() == 0) { // store sample value, but check if samples have to be filled in // in order to get a proper fixed rate track if ((curTimeStamp - (sampleCount * msPerSample)) > (msPerSample / 2)) { LOG.info("Adding fill-in at sample: " + sampleCount); shiftSamplesInArray(derivArray); derivArray[0][1] = curSample; calculateDerivatives(derivArray); values.add(new Float(derivArray[derivArray.length - 1][1])); sampleCount++; } // before storing calculate derivatives if (sampleCount == 0) { derivArray[0][0] = curSample; } else if (sampleCount == 1) { derivArray[0][1] = curSample; calculateDerivatives(derivArray); // add the first value twice (instead of adding 0 at index 0 values.add(new Float(derivArray[derivArray.length - 1][1])); values.add(new Float(derivArray[derivArray.length - 1][1])); } else { shiftSamplesInArray(derivArray); derivArray[0][1] = curSample; calculateDerivatives(derivArray); values.add(new Float(derivArray[derivArray.length - 1][1])); } // begin of new sample reset... indexInSample = 0; sampleCount++; curSample = 0.0f; } else { if (li.charAt(0) == '#') { continue; } if (indexInSample == 0) { flArray = toFloatArray(li); if (flArray.length >= 2) { //should be of length 3 curTimeStamp = flArray[1]; } } else if (indexInSample == row) { flArray = toFloatArray(li); if (flArray.length > column) { curSample = flArray[column]; } } indexInSample++; } } LOG.info("Derivative: " + derivative + " Number of samples: " + sampleCount); try { bufRead.close(); } catch (IOException ioe) { } float[] result = new float[sampleCount]; Float fl; for (int i = 0; i < sampleCount; i++) { fl = (Float) values.get(i); result[i] = fl.floatValue(); //if (i < 20) { // System.out.println("i: " + i + " v: " + fl.floatValue()); //} } return result; } /** * The parameter is a multidimensional array of unknown length (minimal 2), * containing arrays of length 2. The first array in the arrays contains * the values read from file, the last value will be at index 1, the * previous at index 0. The derivative of these two values will be placed * in the next array at index 1, the previous derivative of that level * has been copied to index 0 before. The desired result is at * [<code>derivArray</code>.length -1] [1]. * * @param derivArray multidimensional array, [x][2] */ private void calculateDerivatives(float[][] derivArray) { for (int i = 0; i < (derivArray.length - 1); i++) { derivArray[i + 1][1] = (derivArray[i][1] - derivArray[i][0]) / deltaT; } } /** * An unknown length array containing float arrays of length 2. The values * at index 0 in these arrays will be given the value of the values at * index 1. Index 1 will then be filled with the new (calculated) value * of the next sample. * * @param derivArray multidimensional array, [x][2] */ private void shiftSamplesInArray(float[][] derivArray) { for (int i = 0; i < derivArray.length; i++) { derivArray[i][0] = derivArray[i][1]; } } /** * Returns the milliseconds per sample value that has been extracted from * the source file Returns whether the value at the specified row and * column represents an angle in degrees. If true values between 0 and * -180 can be converted to a value between 180 and 360. * * @param row the row * @param column the column * * @return true if the cell represent an angle in degrees, false otherwise. */ private boolean shouldConvertAngle(int row, int column) { switch (row) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: return true; case 8: return ((column == 0) || (column == 1)); case 10: case 12: case 14: return ((column >= 3) && (column < 5)); default: return false; } } /** * Returns the milliseconds per sample value that has been extracted from * the source file * * @return the milliseconds per sample value */ public float getMsPerSample() { return msPerSample; } /** * Returns the sample frequency / sample rate that has been extracted from * the source file. * * @return the sample frequency */ public int getSampleFrequency() { return sampleFrequency; } /** * Returns whether the passed file is a valid dataglove log file. * * @return true if the file is a valid dataglove log file, false otherwise */ public boolean isValidFile() { return validLogFile; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -