I have samples of time-domain data that are constantly updating a LinkedList called "rollHistory" which is then being displayed in a plot, like so:
if (rollHistory.size() > HISTORY_SIZE) {
rollHistory.removeFirst(); //remove first value
}
rollHistory.addLast(sensorReading); //insert latest value
rollHistorySeries.setModel(rollHistory , SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); //added 06/24/2013
aprHistoryPlot.redraw();
}
"HISTORY_SIZE" is the max length of the data to be plotted. This code works fine. However, what I would really like to display is the FFT of the time-domain data instead of the data itself. To accomplish this, I downloaded the JTransform library. The realForwardFull() function seems to expect an array of doubles, so I tried converting the LinkedList (called rollHistory above) to an array, then passing this to the realForwardFull() function. But the rollHistorySeries.setModel command seems to expect a LinkedList. So I then tried converting the output of realForwardFull() back to a linkedlist. The code displays no errors in eclipse, but it shuts down when I try to run it on the phone.
DoubleFFT_1D fftDo = new DoubleFFT_1D(512);
if (rollHistory.size() > HISTORY_SIZE) {
rollHistory.removeFirst();
}
rollHistory.addLast(sensorReading);
Double[] rollHistoryAr = rollHistory.toArray(new Double[0]); // convert rollHistory to array
double[] fft = new double[256 * 2];
System.arraycopy(rollHistoryAr, 0, fft, 0, 256); //copy rollHistoryAr to fft array
fftDo.realForwardFull(fft); //find fft of rollHistory and save to fft
LinkedList rollHistoryFreq = new LinkedList(Arrays.asList(fft)); //convert back to LinkedList
rollHistorySeries.setModel(rollHistoryFreq , SimpleXYSeries.ArrayFormat.Y_VALS_ONLY); //added 06/24/2013
aprHistoryPlot.redraw();
//mGraph.addDataPoint(sensorReading);
}
I tried wrapping a subset of the above code with a try/catch, like this:
try {
//jTransform Attempt
Double[] rollHistoryAr = rollHistory.toArray(new Double[rollHistory.size()]);
double[] fft = new double[256];
System.arraycopy(rollHistoryAr, 0, fft, 0, 256);
dfft.realForward(fft);
LinkedList rollHistoryFreq = new LinkedList(Arrays.asList(fft));
rollHistorySeries.setModel(rollHistoryFreq, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY);
} catch (Exception e)
{ e.printStackTrace(); }
In this case, the app runs without the "Unfortunately...stopped" error, but the graph is empty, and the LogCat displays these messages:
07-13 16:18:18.509: W/System.err(11288): java.lang.ArrayStoreException: java.lang.Integer cannot be stored in an array of type java.lang.Double[]
07-13 16:18:18.509: W/System.err(11288): at java.util.LinkedList.toArray(LinkedList.java:958)
07-13 16:18:18.509: W/System.err(11288): at com.example.MyActivity$ArduinoReceiver.onReceive(MyActivity.java:166)
07-13 16:18:18.509: W/System.err(11288): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758)
07-13 16:18:18.509: W/System.err(11288): at android.os.Handler.handleCallback(Handler.java:725)
07-13 16:18:18.509: W/System.err(11288): at android.os.Handler.dispatchMessage(Handler.java:92)
07-13 16:18:18.509: W/System.err(11288): at android.os.Looper.loop(Looper.java:137) 07-13 16:18:18.509: W/System.err(11288): at android.app.ActivityThread.main(ActivityThread.java:5293)
07-13 16:18:18.509: W/System.err(11288): at java.lang.reflect.Method.invokeNative(Native Method) 07-13 16:18:18.509: W/System.err(11288): at java.lang.reflect.Method.invoke(Method.java:511)
07-13 16:18:18.509: W/System.err(11288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 07-13 16:18:18.509: W/System.err(11288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-13 16:18:18.509: W/System.err(11288): at dalvik.system.NativeStart.main(Native Method)
Does anyone have any ideas as to what I'm doing wrong? I can post more code if necessary. Thanks!