@@ -65,13 +65,13 @@ public class BitmapGenerator {
6565 private int lastBitmapRequested = 0 ; //keeps track of the most recently requested bitmap window
6666 private int oldestBitmapAvailable = 0 ;
6767 private Context context ;
68+ private HammingWindow hammingWindow ;
6869
6970 //allocate memory here rather than in performance-affecting methods:
7071 private int samplesRead = 0 ;
7172 private double [] fftSamples ;
7273 private double [] previousWindow ; //keep a handle on the previous audio sample window so that values can be averaged across them
7374 private double [] combinedWindow ;
74- private double [] hammingWindow ;
7575 private DoubleFFT_1D dfft1d ; //DoubleFFT_1D constructor must be supplied with an 'n' value, where n = data size
7676 private int val = 0 ;
7777
@@ -91,9 +91,9 @@ public BitmapGenerator(Context context) {
9191 fftSamples = new double [SAMPLES_PER_WINDOW *2 ];
9292 previousWindow = new double [SAMPLES_PER_WINDOW ]; //keep a handle on the previous audio sample window so that values can be averaged across them
9393 combinedWindow = new double [SAMPLES_PER_WINDOW ];
94- hammingWindow = new double [SAMPLES_PER_WINDOW ];
9594 dfft1d = new DoubleFFT_1D (SAMPLES_PER_WINDOW ); //DoubleFFT_1D constructor must be supplied with an 'n' value, where n = data size
9695
96+ hammingWindow = new HammingWindow (SAMPLES_PER_WINDOW );
9797
9898 String colMapString = prefs .getString (PREF_COLOURMAP_KEY , "NULL" );
9999 int colourMap = 0 ;
@@ -109,8 +109,6 @@ public BitmapGenerator(Context context) {
109109 case 6 : colours = HeatMap .PuOr_Backwards_ColorBrewer (); break ;
110110 }
111111
112- generateHammingWindow ();
113-
114112 float newContrast = prefs .getFloat (PREF_CONTRAST_KEY , Float .MAX_VALUE );
115113 if (newContrast != Float .MAX_VALUE ) contrast = newContrast * 3.0f + 1.0f ; //slider value must be between 0 and 1, so multiply by 3 and add 1
116114
@@ -234,7 +232,7 @@ private void processAudioWindow(short[] samples, int[] destArray) { //TODO prev
234232 for (int i = 0 ; i < SAMPLES_PER_WINDOW ; i ++) {
235233 fftSamples [i ] = (double )(samples [i ]);
236234 }
237- applyHammingWindow (fftSamples ); //apply Hamming window before performing STFT
235+ hammingWindow . applyHammingWindow (fftSamples ); //apply Hamming window before performing STFT
238236 spectroTransform (fftSamples ); //do the STFT on the copied data
239237
240238 for (int i = 0 ; i < SAMPLES_PER_WINDOW ; i ++) {
@@ -264,25 +262,6 @@ private int cappedValue(double d) {
264262 }
265263 return (int )(255 *Math .pow ((Math .log1p (d )/Math .log1p (maxAmplitude )),contrast ));
266264 }
267-
268- private void applyHammingWindow (double [] samples ) {
269-
270- //apply windowing function through multiplication with time-domain samples
271- for (int i = 0 ; i < SAMPLES_PER_WINDOW ; i ++) {
272- samples [i ] *= hammingWindow [i ];
273- }
274- }
275-
276- private void generateHammingWindow () {
277- /*
278- * This method generates an appropriately-sized Hamming window to be used later.
279- */
280- int m = SAMPLES_PER_WINDOW /2 ;
281- double r = Math .PI /(m +1 );
282- for (int i = -m ; i < m ; i ++) {
283- hammingWindow [m + i ] = 0.5 + 0.5 * Math .cos (i * r );
284- }
285- }
286265
287266 private void spectroTransform (double [] paddedSamples ) {
288267 /*
@@ -367,8 +346,6 @@ protected Bitmap createEntireBitmap(int startWindow, int endWindow, int bottomFr
367346 startWindow %= WINDOW_LIMIT ;
368347 endWindow %= WINDOW_LIMIT ;
369348
370- //TODO filter
371-
372349 Bitmap ret ;
373350 Canvas retCanvas ;
374351 int bitmapWidth ;
@@ -482,13 +459,14 @@ public Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight)
482459 return Bitmap .createBitmap (bitmapToScale , 0 , 0 , bitmapToScale .getWidth (), bitmapToScale .getHeight (), matrix , true );
483460 }
484461
485- protected short [] getAudioChunk (int startWindow , int endWindow ) {
462+ protected short [] getAudioChunk (int startWindow , int endWindow , int bottomFreq , int topFreq ) {
486463 /*
487464 * Returns an array of PCM audio data based on the window interval supplied to the function.
488465 */
489466 //convert windows into array indices
490467 startWindow %= WINDOW_LIMIT ;
491468 endWindow %= WINDOW_LIMIT ;
469+
492470
493471 short [] toReturn ;
494472
@@ -498,13 +476,13 @@ protected short[] getAudioChunk(int startWindow, int endWindow) {
498476 for (int i = startWindow ; i < WINDOW_LIMIT ; i ++) {
499477 for (int j = 0 ; j < SAMPLES_PER_WINDOW ; j ++) {
500478 //Log.d("Audio chunk","i: "+i+", j: "+j+" i*SAMPLES_PER_WINDOW+j: "+(i*SAMPLES_PER_WINDOW+j));
501- toReturn [(i -startWindow )*SAMPLES_PER_WINDOW +j ] = Short . reverseBytes ( audioWindows [i ][j ]); //must be little-endian for WAV
479+ toReturn [(i -startWindow )*SAMPLES_PER_WINDOW +j ] = audioWindows [i ][j ];
502480 }
503481 }
504482 for (int i = 0 ; i < endWindow ; i ++) {
505483 for (int j = 0 ; j < SAMPLES_PER_WINDOW ; j ++) {
506484 //Log.d("Audio chunk","i: "+i+", j: "+j+" i*SAMPLES_PER_WINDOW+j: "+(i*SAMPLES_PER_WINDOW+j));
507- toReturn [(WINDOW_LIMIT -startWindow +i )*SAMPLES_PER_WINDOW +j ] = Short . reverseBytes ( audioWindows [i ][j ]); //must be little-endian for WAV
485+ toReturn [(WINDOW_LIMIT -startWindow +i )*SAMPLES_PER_WINDOW +j ] = audioWindows [i ][j ];
508486 }
509487 }
510488 }
@@ -514,10 +492,16 @@ protected short[] getAudioChunk(int startWindow, int endWindow) {
514492
515493 for (int j = 0 ; j < SAMPLES_PER_WINDOW ; j ++) {
516494 //Log.d("Audio chunk","i: "+i+", j: "+j+" i*SAMPLES_PER_WINDOW+j: "+(i*SAMPLES_PER_WINDOW+j));
517- toReturn [(i -startWindow )*SAMPLES_PER_WINDOW +j ] = Short . reverseBytes ( audioWindows [i ][j ]); //must be little-endian for WAV
495+ toReturn [(i -startWindow )*SAMPLES_PER_WINDOW +j ] = audioWindows [i ][j ];
518496 }
519497 }
520498 }
499+
500+ Log .d ("" ,"Filtering capture from " +bottomFreq +"Hz to " +topFreq +"Hz." );
501+ BandpassButterworth butter = new BandpassButterworth (SAMPLE_RATE , 4 , (double )bottomFreq , (double )topFreq , 1.0 );
502+ butter .applyBandpassFilter (toReturn );
503+
504+ for (int i = 0 ; i < toReturn .length ; i ++) toReturn [i ] = Short .reverseBytes (toReturn [i ]); //must be little-endian for WAV
521505 return toReturn ;
522506 }
523507
0 commit comments