· 10 years ago · Sep 07, 2016, 11:44 AM
1 protected void runHighPassFilter() throws SQLException {
2 System.out.print("high-pass filter started");
3 String sqlCreate = "CREATE TABLE IF NOT EXISTS ACCEL_HIGH_PASS ( ts integer, z double, z_aver double)";
4 String insert = "insert into accel_high_pass (ts, z, z_aver) values (?,?,?)";
5 stmt = con.createStatement();
6 stmt.executeUpdate(sqlCreate);
7
8 con.setAutoCommit(false);
9 PreparedStatement pstmt = con.prepareStatement(insert);
10
11 stmt.setFetchSize(1000);
12 String sqlHighPassFilter = "select ts, z from accel";
13 ResultSet rs = stmt.executeQuery(sqlHighPassFilter);
14
15 double filteredValue;
16 double gravity = 9.7;
17 final double alpha = 0.9;
18 double z;
19
20 List<AccelHighPass> accels = new ArrayList<AccelHighPass>();
21 AccelHighPass accelHighPass;
22 Queue<AccelHighPass> samples = new CircularFifoQueue<AccelHighPass>(5);
23 System.out.print("reading accel ResultSet started");
24 while (rs.next()) {
25 z = rs.getDouble("z");
26 gravity = alpha * gravity + (1 - alpha) * z;
27 filteredValue = z - gravity;
28
29 accelHighPass = new AccelHighPass(rs.getLong("ts"), filteredValue);
30 accels.add(accelHighPass);
31 }
32 rs.close();
33 stmt.close();
34 System.out.print("reading accel ResultSet finished");
35
36 samples.add(accels.get(0));
37 samples.add(accels.get(1));
38 double avr = 0;
39 System.out.print("inserting accel_high_pass started");
40 for(int head=2, current=0; head<accels.size(); head++, current++){
41 samples.add(accels.get(head));
42 avr = samples.stream().map(a->a.getZ()).mapToDouble(Double::doubleValue).average().orElse(0);
43 accels.get(current).setZ_aver(avr);
44 }
45
46 for(AccelHighPass ahp : accels){
47 pstmt.setLong(1, ahp.getTs());
48 pstmt.setDouble(2, ahp.getZ());
49 pstmt.setDouble(3, ahp.getZ_aver());
50 pstmt.addBatch();
51 }
52 pstmt.executeBatch();
53 con.commit();
54 System.out.print("inserting accel_high_pass finished");
55 System.out.print("high-pass filter finished");
56 }