# changing bitrate of audio recording and taking samples from audio in arduino

I am looking to make a baby cry detection module that is a part of a bigger project i.e. IOT based baby monitoring system. Using AI, i have a dataset (of baby crying and not crying) to train model using binary classification through logistic regression. The problem is that the dataset has a 10kbps sample rate. and arduino records audio on 192kbps probably. so i need the arduino recorded audio to be 10kbps. secondly i need 100 weights or samples from the arduino recorded audio. so lets say the audio is of 5 seconds, so i need a sample after every 50ms so that the total samples would be 100. the main problem is i don't know how to take samples from an audio being recorded or a prerecorded audio using arduino. The 100 samples would then be multiplied by the 100 samples from the trained model and then added and then sigmoid function will be used so that at the end we can know whether the baby is crying or not. All the deployment of the model will be done in arduino. Any help regarding taking the samples or bitrate, would be much appreciated. Thanks in advance! :)

• “kbps” means kilobits per second. That is a unit of bit rate, not of sample rate. If you want help for controlling the sampling rate of the Arduino, tell us what sampling rate you need, and what model of Arduino you are using. Feb 22 at 9:31
• What arduino are you using that can sample at 192ksps?
– Majenko
Feb 22 at 10:19
• With what exactly do you need help? Do you already have a code, that samples the audio and sends it to the rest of your setup? Or are you starting from zero in this part? Feb 22 at 10:30
• @EdgarBonet what i need at the end is 10kbps arduino recording quality or to convert any kbps into 10kbps. i am using arduino mega and node mcu. finally will be working on either one. thanks Feb 22 at 14:03
• If you take 1 sample every 50ms that is a sampling rate of 20sps. According to Nyquist-Shannon theorem that means you'd have a maximum detectable frequency of 10Hz. I fail to see how that would be of any use unless you're a blue whale.
– Majenko
Feb 22 at 14:27

First of all, I am pretty sure your idea of using discrete ADC samples as predictors for your model is hopelessly naive. As explained by Majenko, you cannot expect anything meaningful from a sound recording taken at 20 samples per second. You may have more luck using spectral intensities as predictors. I suggest you start by doing some time–frequency analysis on your training data, just to get some ideas on what predictors may be worth trying. In any case, I strongly suggest you train and test your model on your computer, before committing any code to the Arduino.

That being said, sampling a signal at 20 Hz is quite easy: wait for 50 ms (or 50,000 µs for better accuracy), take one sample, repeat. The only caveat is that you have to use `millis()` or `micros()` rather than `delay()` or `delayMicroseconds()`, otherwise the sampling period will be longer than expected because of the time needed to execute the code.

Applying the sigmoid function just to compare the result to 0.5 makes little sense. You know the sigmoid is larger than 0.5 only when its argument is positive, so you can trivially save yourself expensive floating point operations.

Here is a tentative implementation of (my understanding of) the idea you suggest:

``````const uint8_t mic_pin = A0;
const size_t sample_count = 100;
const uint32_t sample_period = 50000;  // 50,000 us = 50 ms
const int weights[sample_count] = { 12, -42, 23 /* etc... */};

void setup() {
Serial.begin(9600);
}

void loop() {
// Record the samples.
int samples[sample_count];
uint32_t last_sample = micros();
for (size_t i = 0; i < sample_count; i++) {
while (micros() - last_sample < sample_period) { /* wait */ }
last_sample += sample_period;
}

// Compute the weighted sum.
long sum = 0;
for (size_t i = 0; i < sample_count; i++) {
sum += (long) samples[i] * weights[i];
}

// Predict.
if (sum >= 0) {
Serial.println("The baby is crying!");
}
}
``````
• surely i am looking into more methods of machine learning, the big downside of the rest of the models is that they cannot be deployed on arduino. And yes i have looked into spectrograms, they are much more valuable. thanks alot for the information and the code you provided :) Feb 22 at 16:13