-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioTest.java
411 lines (337 loc) · 15.5 KB
/
AudioTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.OptionalDouble;
import org.apache.commons.math3.util.DoubleArray;
import org.apache.commons.math3.util.Precision;
import org.apache.commons.math3.util.ResizableDoubleArray;
import org.jtransforms.fft.DoubleFFT_1D;
// You can parse AudioData objects or File objects
public class AudioTest {
public static TestResult testNoise(AudioData audioDatCalib, AudioData audioDatDut, limitsHzDb limits,
String filterCoeffLocation) throws IOException {
TestResult TestResultObj = new TestResult(0);
try {
// Disregard audio at the beginning of the file
AudioData adCalibShort = disregardBeginning(limits, audioDatCalib);
AudioData adDutShort = disregardBeginning(limits, audioDatDut);
// Read FIR filter coefficients from file
double[] firCoeffs = readFilter(filterCoeffLocation);
// Circular convolution with FIR Filter
double[] timeDomDutCC = firFilter(adDutShort.timeDomDataFirstChannel, firCoeffs);
double[] timeDomCaliCC = firFilter(adCalibShort.timeDomDataFirstChannel, firCoeffs);
// wavWriter wavWriter = new wavWriter();
// productionLineTestDsp.wavWriter.write("CCIR.wav", timeDomDutCC, 16000);
double temp = 2 * (dB(rms(timeDomDutCC)) - dB(rms(timeDomCaliCC)));
TestResultObj.dutValue = Precision.round(temp, 1);
if ((TestResultObj.dutValue > limits.noiseLimitDb)||(TestResultObj.dutValue < -2*(limits.noiseLimitDb))) {
TestResultObj.isPass = false;
}
TestResultObj.comment = "DUT mic self noise minus reference: " + Double.toString(TestResultObj.dutValue)
+ " dB";
return TestResultObj;
} catch (IOException e) {
System.err.println(e);
e.printStackTrace();
throw e;
}
}
// Tests if the spectral difference between reference and golden sample signal
// exceeds a tolerance in a certain band
// method for testing two arrays of double
public static TestResult testSensitivity(AudioData audioDatCalib, AudioData audioDatDut, limitsHzDb limits) {
// Disregard audio at the beginning of the file
AudioData adCalibShort = disregardBeginning(limits, audioDatCalib);
AudioData adDutShort = disregardBeginning(limits, audioDatDut);
// shorten longer file
AudioData zpOut = shortenLongerFile(adCalibShort.timeDomDataFirstChannel, adDutShort.timeDomDataFirstChannel);
// make frequency vector
double[] freqVec = makeFreqVec(audioDatDut.samplingRate,
zpOut.timeDomDataSecondChannel.length / limits.smoothingBwBins);
// look up indizies corresponding to frequency values
int[] toleranceIdx = hz2Idx(freqVec, limits.toleranceHz);
int rangeLen = toleranceIdx[(toleranceIdx.length - 1)] - toleranceIdx[0];
// initialize return variable
TestResult TestResultObj = new TestResult(rangeLen);
// get smoothed Fourier transforms in dB
double[] freqDomCalibDb = dB(smooth(dft(zpOut.timeDomDataFirstChannel), limits.smoothingBwBins));
double[] freqDomDutDb = dB(smooth(dft(zpOut.timeDomDataSecondChannel), limits.smoothingBwBins));
double[] differenceVectorDbAbs = new double[rangeLen];
String outString = "device failed test in band numbers: ";
for (int u = 0; u < limits.toleranceHz.length - 1; u++) {
boolean toggle = true;
for (int i = toleranceIdx[u]; i <= toleranceIdx[u + 1] - 1; i++) {
// difference vectors (abs and not abs)
differenceVectorDbAbs[i - toleranceIdx[0]] = java.lang.Math
.abs(freqDomDutDb[i] - freqDomCalibDb[i]);
TestResultObj.diffVecDB[i - toleranceIdx[0]] = freqDomDutDb[i] - freqDomCalibDb[i];
// this is the ugliest hack
double valAbs = differenceVectorDbAbs[i - toleranceIdx[0]];
double val = TestResultObj.diffVecDB[i - toleranceIdx[0]];
if ((valAbs == Double.NEGATIVE_INFINITY) || (valAbs == Double.POSITIVE_INFINITY)
|| Double.isNaN(valAbs)) {
differenceVectorDbAbs[i - toleranceIdx[0]] = 0;
}
if ((val == Double.NEGATIVE_INFINITY) || (val == Double.POSITIVE_INFINITY) || Double.isNaN(val)) {
TestResultObj.diffVecDB[i - toleranceIdx[0]] = 0;
}
// The DUT fails the test when the absolute difference is higher than the
// tolerance
if (differenceVectorDbAbs[i - toleranceIdx[0]] >= limits.toleranceDb[u]) {
TestResultObj.isPass = false;
// Generate output comment
if (toggle) {
outString = outString.concat(Integer.toString(u + 1) + " ");
toggle = false;
}
TestResultObj.comment = (outString);
}
}
if (TestResultObj.isPass) {
TestResultObj.comment = ("device passed test.");
}
}
// Cut frequency vector for plotting
System.arraycopy(freqVec, toleranceIdx[0], TestResultObj.freqVec, 0, rangeLen);
// Make third octave vector for CSV logging
makeThirdOctaveVec(TestResultObj);
return TestResultObj;
}
// In case you pass wav File Objects instead of audio data objects
public static TestResult testSensitivity(File wavFileNameCalib, File wavFileNameDut, limitsHzDb limits)
throws Exception {
try {
// read wav to double
AudioData audioDatCalib = wav2AudioData(wavFileNameCalib);
AudioData audioDatDut = wav2AudioData(wavFileNameDut);
TestResult TestResultObj;
TestResultObj = testSensitivity(audioDatCalib, audioDatDut, limits);
return TestResultObj;
} catch (Exception e) {
System.err.println(e);
e.printStackTrace();
throw e;
}
}
// In case you pass wav File Objects instead of audio data objects
public static TestResult testNoise(File wavFileNameCalib, File wavFileNameDut, limitsHzDb limits,
String filterCoeffLocation) throws Exception {
try {
// read wav to double
AudioData audioDatCalib = wav2AudioData(wavFileNameCalib);
AudioData audioDatDut = wav2AudioData(wavFileNameDut);
TestResult TestResultObj;
TestResultObj = testNoise(audioDatCalib, audioDatDut, limits, filterCoeffLocation);
return TestResultObj;
} catch (Exception e) {
System.err.println(e);
e.printStackTrace();
throw e;
}
}
private static AudioData disregardBeginning(limitsHzDb limits, AudioData audioDat) {
// Disregard first samples due to audio problems
int disregardSamp = (int) (limits.disregardSecs * audioDat.samplingRate);
int newLen = audioDat.timeDomDataFirstChannel.length - disregardSamp;
AudioData out = new AudioData(newLen);
System.arraycopy(audioDat.timeDomDataFirstChannel, disregardSamp, out.timeDomDataFirstChannel, 0, newLen);
return out;
}
// Convert wav file to Audio Data
private static AudioData wav2AudioData(File fileToBeRead) throws WavFileException, IOException {
try {
// Open the wav file
WavFile wavFile = WavFile.openWavFile(fileToBeRead);
// Get number of frames and convert to int
long nFrames = wavFile.getNumFrames();
int nFramesInt = (int) nFrames;
// Create buffers with the right length
double[] timeDomData = new double[nFramesInt];
// Read Frames
wavFile.readFrames(timeDomData, nFramesInt);
// Get sampling rate
long samplingRate = wavFile.getSampleRate();
// Make return object
AudioData audioDataObj;
audioDataObj = new AudioData(timeDomData.length);
audioDataObj.samplingRate = (int) samplingRate;
audioDataObj.timeDomDataFirstChannel = timeDomData;
// Close Wav File
wavFile.close();
return audioDataObj;
} catch (Exception e) {
System.err.println(e);
e.printStackTrace();
throw e;
}
}
private static void makeThirdOctaveVec(TestResult TestResultObj) {
int length = 16;
int[] thirdOctaveFreqs = new int[length];
thirdOctaveFreqs[0] = (int) 250;
thirdOctaveFreqs[1] = (int) 315;
thirdOctaveFreqs[2] = (int) 400;
thirdOctaveFreqs[3] = (int) 500;
thirdOctaveFreqs[4] = (int) 630;
thirdOctaveFreqs[5] = (int) 800;
thirdOctaveFreqs[6] = (int) 1000;
thirdOctaveFreqs[7] = (int) 1250;
thirdOctaveFreqs[8] = (int) 1600;
thirdOctaveFreqs[9] = (int) 2000;
thirdOctaveFreqs[10] = (int) 2500;
thirdOctaveFreqs[11] = (int) 3150;
thirdOctaveFreqs[12] = (int) 4000;
thirdOctaveFreqs[13] = (int) 5000;
thirdOctaveFreqs[14] = (int) 6300;
thirdOctaveFreqs[15] = (int) 8000;
int[] thrirdOctaveFVecIdx = hz2Idx(TestResultObj.freqVec, thirdOctaveFreqs);
for (int i = 0; i < length; i++) {
TestResultObj.diffVecDBThird[i] = Precision.round(TestResultObj.diffVecDB[thrirdOctaveFVecIdx[i]], 1);
}
}
private static double[] firFilter(double[] inputVector, double[] filterCoeffs) {
int length = filterCoeffs.length;
double[] impulseResponse = filterCoeffs;
double[] delayLine = new double[length];
int count = 0;
double[] output = new double[inputVector.length];
for (int u = 0; u < inputVector.length; u++) {
delayLine[count] = inputVector[u];
double result = 0.0;
int index = count;
for (int i = 0; i < length; i++) {
result += impulseResponse[i] * delayLine[index--];
if (index < 0)
index = length - 1;
}
if (++count >= length)
count = 0;
output[u] = result;
}
return output;
}
private static double[] readFilter(String csvFile) throws IOException {
String line = "";
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
// extend output
DoubleArray out = new ResizableDoubleArray();
// read values
while ((line = br.readLine()) != null) {
out.addElement(Double.parseDouble(line));
}
return out.getElements();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
private static double rms(double[] timeDomData) {
double sqSum = 0;
for (int i = 0; i < timeDomData.length; i++) {
sqSum += timeDomData[i] * timeDomData[i];
}
return Math.sqrt(sqSum / timeDomData.length);
}
// Shorten longer file
private static AudioData shortenLongerFile(double[] firstArray, double[] secondArray) {
int lengthDif = firstArray.length - secondArray.length;
if (lengthDif > 0) { // the first array is longer -> shorten the first array
AudioData audioDatComb = new AudioData(secondArray.length);
double[] nest = new double[secondArray.length];
// put second array in the nest
System.arraycopy(firstArray, 0, nest, 0, secondArray.length);
audioDatComb.timeDomDataFirstChannel = nest;
audioDatComb.timeDomDataSecondChannel = secondArray;
return audioDatComb;
}
if (lengthDif < 0) { // the second array is longer -> shorten the second array
AudioData audioDatComb = new AudioData(firstArray.length);
// fill a nest with beautiful zeros
double[] nest = new double[firstArray.length];
// put second array in this nest
System.arraycopy(secondArray, 0, nest, 0, firstArray.length);
audioDatComb.timeDomDataFirstChannel = firstArray;
audioDatComb.timeDomDataSecondChannel = nest;
return audioDatComb;
}
if (lengthDif == 0) {
AudioData audioDatComb = new AudioData(secondArray.length);
audioDatComb.timeDomDataFirstChannel = firstArray;
audioDatComb.timeDomDataSecondChannel = secondArray;
return audioDatComb;
}
AudioData audioDatComb = new AudioData(0);
return audioDatComb;
}
// discrete Fourier transform
private static double[] dft(double[] timeDomData) {
// Prep for transform
int nFramesInt = timeDomData.length;
double[] freqDomData = new double[nFramesInt];
freqDomData = timeDomData.clone();
DoubleFFT_1D fftObj = new DoubleFFT_1D(nFramesInt);
// Transform
fftObj.realForward(freqDomData);
// square
double[] freqDomDataSq = freqDomData;
for (int i = 0; i < freqDomData.length; i++) {
if (freqDomData[i] == 0) {
freqDomData[i] = Double.MIN_VALUE;
}
freqDomDataSq[i] = (int) Math.pow(freqDomData[i], 2);
}
return freqDomDataSq;
}
// in dB
private static double[] dB(double[] data) {
double[] dataInDb = data;
for (int i = 0; i < data.length; i++) {
dataInDb[i] = 10 * Math.log10(data[i]);
}
return dataInDb;
}
private static double dB(double data) {
double dataInDb = 10 * Math.log10(data);
return dataInDb;
}
// make a frequency vector for plots
private static double[] makeFreqVec(int samplingRate, int nSamples) {
double[] freqVec = new double[nSamples];
double deltaF = (double) samplingRate / (2 * nSamples);
for (int i = 1; i < (int) nSamples; i++) {
freqVec[i] = Math.log10(deltaF * i);
}
return freqVec;
}
// Really basic smoothing function
private static double[] smooth(double[] data, int smoothingBwBins) {
double[] tempArray = new double[smoothingBwBins];
int nNewBins = data.length / smoothingBwBins;
double[] smoothedData = new double[nNewBins];
for (int i = 1; i < nNewBins; i++) {
System.arraycopy(data, (i - 1) * smoothingBwBins, tempArray, 0, smoothingBwBins);
smoothedData[i] = Arrays.stream(tempArray).sum() / smoothingBwBins;
}
return smoothedData;
}
// convert frequencies to indices
private static int[] hz2Idx(double[] freqVec, int[] range) {
double[] tempDiff = new double[freqVec.length];
int[] idxArray = new int[range.length];
Object min = new Object();
for (int u = 0; u < range.length; u++) {
for (int i = 0; i < freqVec.length; i++) {
tempDiff[i] = java.lang.Math.abs(freqVec[i] - Math.log10(range[u]));
}
min = Arrays.stream(tempDiff).min();
int i = 0;
while (tempDiff[i] != ((OptionalDouble) min).getAsDouble()) {
i++;
}
idxArray[u] = i;
}
return idxArray;
}
}