Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // read a bunch of samples:
  2.       int samples[SAMPLES];
  3.      
  4.       for (int i=0; i<SAMPLES; i++) {
  5.         int sample = 0;
  6.         while ((sample == 0) || (sample == -1) ) {
  7.           sample = I2S.read();
  8.         }
  9.         // convert to 18 bit signed
  10.         sample >>= 14;
  11.         samples[i] = sample;
  12.       }
  13.      
  14.       // ok we hvae the samples, get the mean (avg)
  15.       float meanval = 0;
  16.       for (int i=0; i<SAMPLES; i++) {
  17.         meanval += samples[i];
  18.       }
  19.       meanval /= SAMPLES;
  20.       //Serial.print("# average: " ); Serial.println(meanval);
  21.      
  22.       // subtract it from all sapmles to get a 'normalized' output
  23.       for (int i=0; i<SAMPLES; i++) {
  24.         samples[i] -= meanval;
  25.         //Serial.println(samples[i]);
  26.       }
  27.      
  28.       // find the 'peak to peak' max
  29.       float maxsample, minsample;
  30.       minsample = 100000;
  31.       maxsample = -100000;
  32.       for (int i=0; i<SAMPLES; i++) {
  33.         minsample = min(minsample, samples[i]);
  34.         maxsample = max(maxsample, samples[i]);
  35.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement