|
| 1 | +package com.shimmerresearch.algorithms; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertTrue; |
| 4 | + |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.BufferedWriter; |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.FileWriter; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStreamReader; |
| 11 | + |
| 12 | +import org.junit.FixMethodOrder; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runners.MethodSorters; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Mark Nolan, Ruaidhri Molloy |
| 18 | + * |
| 19 | + */ |
| 20 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 21 | +public class API_00002_Filters { |
| 22 | + |
| 23 | + private static final boolean REPLACE_REFERENCE_CSVS = false; |
| 24 | + |
| 25 | + /** |
| 26 | + * Passes invalid configuration into the filter initialisation in order to throw an error. |
| 27 | + */ |
| 28 | + @Test |
| 29 | + public void Test_001_InvalidFilterConfig() { |
| 30 | + try { |
| 31 | + Filter filter = new Filter(Filter.LOW_PASS, 50.0, new double[]{75}); |
| 32 | + assertTrue("Shouldn't get to this line", false); |
| 33 | + } catch (Exception e) { |
| 34 | + System.out.println("Test correctly threw an error"); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Basic low-pass filter test |
| 40 | + */ |
| 41 | + @Test |
| 42 | + public void Test_002_LowPass() { |
| 43 | + String testId = "Test_002"; |
| 44 | + int filterType = Filter.LOW_PASS; |
| 45 | + double samplingRate = 1024.0; |
| 46 | + double[] cornerFrequency = { 10 }; |
| 47 | + int nTaps = 200; |
| 48 | + |
| 49 | + String sourceCsv = "S:/Applications Team/Resources/Data Repository/JUnitTests/Shimmer-Java-Android-API/API_00002_Filters/WhiteNoise_001.csv"; |
| 50 | + String referenceCsv = "S:/Applications Team/Resources/Data Repository/JUnitTests/Shimmer-Java-Android-API/API_00002_Filters/Reference/" + testId + ".csv"; |
| 51 | + |
| 52 | + runTestCommon(testId, sourceCsv, referenceCsv, filterType, samplingRate, cornerFrequency, nTaps); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Basic high-pass filter test |
| 57 | + */ |
| 58 | + @Test |
| 59 | + public void Test_003_HighPass() { |
| 60 | + String testId = "Test_003"; |
| 61 | + int filterType = Filter.HIGH_PASS; |
| 62 | + double samplingRate = 1024.0; |
| 63 | + double[] cornerFrequency = { 400 }; |
| 64 | + int nTaps = 200; |
| 65 | + |
| 66 | + String sourceCsv = "S:/Applications Team/Resources/Data Repository/JUnitTests/Shimmer-Java-Android-API/API_00002_Filters/WhiteNoise_001.csv"; |
| 67 | + String referenceCsv = "S:/Applications Team/Resources/Data Repository/JUnitTests/Shimmer-Java-Android-API/API_00002_Filters/Reference/" + testId + ".csv"; |
| 68 | + |
| 69 | + runTestCommon(testId, sourceCsv, referenceCsv, filterType, samplingRate, cornerFrequency, nTaps); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Basic band-stop filter test |
| 74 | + */ |
| 75 | + @Test |
| 76 | + public void Test_004_BandStop() { |
| 77 | + String testId = "Test_004"; |
| 78 | + int filterType = Filter.BAND_STOP; |
| 79 | + double samplingRate = 1024.0; |
| 80 | + double[] cornerFrequency = { 100, 400 }; |
| 81 | + int nTaps = 200; |
| 82 | + |
| 83 | + String sourceCsv = "S:/Applications Team/Resources/Data Repository/JUnitTests/Shimmer-Java-Android-API/API_00002_Filters/WhiteNoise_001.csv"; |
| 84 | + String referenceCsv = "S:/Applications Team/Resources/Data Repository/JUnitTests/Shimmer-Java-Android-API/API_00002_Filters/Reference/" + testId + ".csv"; |
| 85 | + |
| 86 | + runTestCommon(testId, sourceCsv, referenceCsv, filterType, samplingRate, cornerFrequency, nTaps); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Basic band-pass filter test |
| 91 | + */ |
| 92 | + @Test |
| 93 | + public void Test_005_BandPass() { |
| 94 | + String testId = "Test_005"; |
| 95 | + int filterType = Filter.BAND_PASS; |
| 96 | + double samplingRate = 1024.0; |
| 97 | + double[] cornerFrequencies = { 100, 400 }; |
| 98 | + int nTaps = 200; |
| 99 | + |
| 100 | + String sourceCsv = "S:/Applications Team/Resources/Data Repository/JUnitTests/Shimmer-Java-Android-API/API_00002_Filters/WhiteNoise_001.csv"; |
| 101 | + String referenceCsv = "S:/Applications Team/Resources/Data Repository/JUnitTests/Shimmer-Java-Android-API/API_00002_Filters/Reference/" + testId + ".csv"; |
| 102 | + |
| 103 | + runTestCommon(testId, sourceCsv, referenceCsv, filterType, samplingRate, cornerFrequencies, nTaps); |
| 104 | + } |
| 105 | + |
| 106 | + private void runTestCommon(String testId, String sourceCsv, String referenceCsv, int filterType, double samplingRate, double[] cornerFrequency, int nTaps) { |
| 107 | + System.out.println("\n ------------------- " + testId + " start -------------------\n"); |
| 108 | + |
| 109 | + printTestDetails(filterType, samplingRate, cornerFrequency, nTaps); |
| 110 | + |
| 111 | + System.out.println("Test steps:"); |
| 112 | + try { |
| 113 | + System.out.println("\t1) Loading source CSV"); |
| 114 | + double[][] sourceCsvArray = csvToDoubleArray(sourceCsv, 0); |
| 115 | + if(sourceCsvArray==null) { |
| 116 | + assertTrue("dataArray is null", false); |
| 117 | + } |
| 118 | + double[] sourceSignal = sourceCsvArray[0]; |
| 119 | + |
| 120 | + System.out.println("\t2) Initialising the filter"); |
| 121 | + Filter filter = new Filter(filterType, samplingRate, cornerFrequency, nTaps); |
| 122 | + |
| 123 | + System.out.println("\t3) Filtering the data"); |
| 124 | + double[] filteredSignal = filter.filterData(sourceSignal); |
| 125 | + |
| 126 | + if(REPLACE_REFERENCE_CSVS) { |
| 127 | + System.out.println("\tWARNING! Replacing the reference CSV"); |
| 128 | + saveReferenceCsv(referenceCsv, filteredSignal); |
| 129 | + } |
| 130 | + |
| 131 | + System.out.println("\t4) Loading reference CSV"); |
| 132 | + double[][] referenceCsvArray = csvToDoubleArray(referenceCsv, 0); |
| 133 | + double[] referenceSignal = referenceCsvArray[0]; |
| 134 | + |
| 135 | + System.out.println("\t5) Comparing the filtered array vs. the reference array"); |
| 136 | + compareDoubleArrays(filteredSignal, referenceSignal); |
| 137 | + } catch (Exception e) { |
| 138 | + e.printStackTrace(); |
| 139 | + assertTrue("Error -> see console", false); |
| 140 | + } |
| 141 | + |
| 142 | + System.out.println("\n ------------------- " + testId + " end -------------------\n"); |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + private void printTestDetails(int filterType, double samplingRate, double[] cornerFrequency, int nTaps) { |
| 147 | + String filterTypeStr = ""; |
| 148 | + if(filterType==Filter.LOW_PASS) { |
| 149 | + filterTypeStr = "Low-pass"; |
| 150 | + } else if(filterType==Filter.HIGH_PASS) { |
| 151 | + filterTypeStr = "High-pass"; |
| 152 | + } else if(filterType==Filter.BAND_PASS) { |
| 153 | + filterTypeStr = "Band-pass"; |
| 154 | + } else if(filterType==Filter.BAND_STOP) { |
| 155 | + filterTypeStr = "Band-stop"; |
| 156 | + } |
| 157 | + |
| 158 | + String cornerFreqs = "{"; |
| 159 | + for(int i=0;i<cornerFrequency.length;i++) { |
| 160 | + cornerFreqs += String.valueOf(cornerFrequency[i]); |
| 161 | + if(i<cornerFrequency.length-1) { |
| 162 | + cornerFreqs += ", "; |
| 163 | + } |
| 164 | + } |
| 165 | + cornerFreqs += "}"; |
| 166 | + |
| 167 | + System.out.println("Filter Type = " + filterTypeStr |
| 168 | + + "\nSampling Rate = " + samplingRate |
| 169 | + + "\nCorner Freq(s) = " + cornerFreqs |
| 170 | + + "\nOrder = " + nTaps); |
| 171 | + } |
| 172 | + |
| 173 | + private void compareDoubleArrays(double[] filteredSignal, double[] referenceSignal) { |
| 174 | + if(filteredSignal==null || referenceSignal==null) { |
| 175 | + assertTrue("An array is null", false); |
| 176 | + } |
| 177 | + if(filteredSignal.length!=referenceSignal.length) { |
| 178 | + assertTrue("Array lengths are not equal", false); |
| 179 | + } |
| 180 | + |
| 181 | + for(int i=0;i<referenceSignal.length;i++) { |
| 182 | + assertTrue("Arrays are not equal at index:" + i, filteredSignal[i]==referenceSignal[i]); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + private void saveReferenceCsv(String referenceCsv, double[] filteredSignal) { |
| 187 | + FileWriter fw = null; |
| 188 | + try { |
| 189 | + fw = new FileWriter(referenceCsv, false); |
| 190 | + } catch (IOException ioe) { |
| 191 | + ioe.printStackTrace(); |
| 192 | + } |
| 193 | + |
| 194 | + BufferedWriter bw = new BufferedWriter(fw); |
| 195 | + String tempStr = ""; |
| 196 | + try { |
| 197 | + for (double d:filteredSignal) { |
| 198 | + tempStr = String.valueOf(d); |
| 199 | + bw.write(tempStr); |
| 200 | + bw.newLine(); |
| 201 | + } |
| 202 | + bw.close(); |
| 203 | + } catch (IOException ioe) { |
| 204 | + ioe.printStackTrace(); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + private double[][] csvToDoubleArray(String csvFile, int qtyHeaderLines) throws Exception { |
| 209 | + double[][] dataArray = null; |
| 210 | + |
| 211 | + BufferedReader br = null; |
| 212 | + try { |
| 213 | + FileInputStream fIn = new FileInputStream(csvFile); |
| 214 | + br = new BufferedReader(new InputStreamReader(fIn)); |
| 215 | + String line = ""; |
| 216 | + |
| 217 | + int lineCount = 0; |
| 218 | + int colCount = 0; |
| 219 | + //count Lines |
| 220 | + while ((line = br.readLine()) != null) { |
| 221 | + lineCount++; |
| 222 | + //Pick a line after the header lines to detect the number of channels |
| 223 | + if(lineCount==qtyHeaderLines+1) { |
| 224 | + String[] data = line.split(","); |
| 225 | + colCount = data.length; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + lineCount-=qtyHeaderLines; |
| 230 | + |
| 231 | + // "reset" to beginning of file (discard old buffered reader) |
| 232 | + fIn.getChannel().position(0); |
| 233 | + br = new BufferedReader(new InputStreamReader(fIn)); |
| 234 | + |
| 235 | + dataArray = new double[colCount][lineCount]; |
| 236 | + |
| 237 | + lineCount = 0; |
| 238 | + while ((line = br.readLine()) != null) { |
| 239 | + if(lineCount>=qtyHeaderLines) { |
| 240 | + String[] data = line.split(","); |
| 241 | + for(int i=0;i<data.length;i++) { |
| 242 | + String d = data[i]; |
| 243 | + dataArray[i][lineCount-qtyHeaderLines] = Double.parseDouble(d); |
| 244 | + } |
| 245 | + } |
| 246 | + lineCount++; |
| 247 | + |
| 248 | + } |
| 249 | + } catch (Exception e) { |
| 250 | + e.printStackTrace(); |
| 251 | + throw(e); |
| 252 | + } finally { |
| 253 | + if(br!=null) { |
| 254 | + br.close(); |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + return dataArray; |
| 259 | + } |
| 260 | + |
| 261 | +} |
0 commit comments