Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /*
  2.      This example reads audio data from an Invensense's ICS43432 I2S microphone
  3.      breakout board, and prints out the samples to the Serial console. The
  4.      Serial Plotter built into the Arduino IDE can be used to plot the audio
  5.      data (Tools -> Serial Plotter)
  6.      
  7.      Circuit:
  8.      * Arduino/Genuino Zero, MKRZero or MKR1000 board
  9.      * ICS43432:
  10.        * GND connected GND
  11.        * 3.3V connected 3.3V (Zero) or VCC (MKR1000, MKRZero)
  12.        * WS connected to pin 0 (Zero) or pin 3 (MKR1000, MKRZero)
  13.        * CLK connected to pin 1 (Zero) or pin 2 (MKR1000, MKRZero)
  14.        * SD connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero)
  15.      
  16.      created 17 November 2016
  17.      by Sandeep Mistry
  18.      */
  19.      
  20.     #include <I2S.h>
  21.      
  22.     void setup() {
  23.       // Open serial communications and wait for port to open:
  24.       // A baud rate of 115200 is used instead of 9600 for a faster data rate
  25.       // on non-native USB ports
  26.       Serial.begin(115200);
  27.       while (!Serial) {
  28.         ; // wait for serial port to connect. Needed for native USB port only
  29.       }
  30.      
  31.       // start I2S at 16 kHz with 32-bits per sample
  32.       if (!I2S.begin(I2S_PHILIPS_MODE, 16000, 32)) {
  33.         Serial.println("Failed to initialize I2S!");
  34.         while (1); // do nothing
  35.       }
  36.     }
  37.      
  38.     #define SAMPLES 128 // make it a power of two for best DMA performance
  39.      
  40.     void loop() {
  41.       // read a bunch of samples:
  42.       int samples[SAMPLES];
  43.      
  44.       for (int i=0; i<SAMPLES; i++) {
  45.         int sample = 0;
  46.         while ((sample == 0) || (sample == -1) ) {
  47.           sample = I2S.read();
  48.         }
  49.         // convert to 18 bit signed
  50.         sample >>= 14;
  51.         samples[i] = sample;
  52.       }
  53.      
  54.       // ok we hvae the samples, get the mean (avg)
  55.       float meanval = 0;
  56.       for (int i=0; i<SAMPLES; i++) {
  57.         meanval += samples[i];
  58.       }
  59.       meanval /= SAMPLES;
  60.       //Serial.print("# average: " ); Serial.println(meanval);
  61.      
  62.       // subtract it from all sapmles to get a 'normalized' output
  63.       for (int i=0; i<SAMPLES; i++) {
  64.         samples[i] -= meanval;
  65.         //Serial.println(samples[i]);
  66.       }
  67.      
  68.       // find the 'peak to peak' max
  69.       float maxsample, minsample;
  70.       minsample = 100000;
  71.       maxsample = -100000;
  72.       for (int i=0; i<SAMPLES; i++) {
  73.         minsample = min(minsample, samples[i]);
  74.         maxsample = max(maxsample, samples[i]);
  75.       }
  76.       Serial.println(maxsample - minsample);
  77.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement