1

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. – Edgar Bonet Feb 22 '21 at 09:31
  • 1
    What arduino are you using that can sample at 192ksps? – Majenko Feb 22 '21 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? – chrisl Feb 22 '21 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 – Muhammad Waqar Anwar Feb 22 '21 at 14:03
  • @Majenko i have heard that arduino recording quality is 192kbps, i have not tested it yet. thanks – Muhammad Waqar Anwar Feb 22 '21 at 14:03
  • Where did you hear such nonsense? – Majenko Feb 22 '21 at 14:04
  • @chrisl at the end of the day, i need 100 samples from the recorded audio on the arduino. it can be real time which means arduino just records the 100 sample values and not the audio, or it can be post processing which means that first arduino records the audio and then extracts 100 samples from that audio. either ways i need 100 samples. so that then the 100 samples can be done some calculations on, to finally deploy the model on arduino to check whether baby is crying or not. thanks – Muhammad Waqar Anwar Feb 22 '21 at 14:08
  • @Majenko kindly guide, I am fairly new to arduino and this is my Final Year Project at the university. thanks – Muhammad Waqar Anwar Feb 22 '21 at 14:08
  • Well, firstly the Arduino (as in most basic 8 bit Arduino branded boards, the most common type) really do not do audio. Maybe very low quality, brief, scratch sounds, but not proper audio. Secondly the absolute maximum sampling rate of a single ADC channel is 9615Hz. – Majenko Feb 22 '21 at 14:14
  • Thirdly most Arduinos have such a minuscule amount of memory there's no way you'll ever get multi-second sampling to fit into it. – Majenko Feb 22 '21 at 14:16
  • @Majenko i am confused in Hz and kbps. secondly i know arduino don't have much memory thats why i will be using a memory card reader along with it. i have seen tutorials on how to record audio with arduino on memory card. Moreover, i was thinking what if i directly attach a microphone on A0, and get the analogRead out of it after every 50ms. that will make 100 samples but i doubt that'll work. thanks – Muhammad Waqar Anwar Feb 22 '21 at 14:20
  • 1Hz is one sample per second. 1000Hz is 1kHz and 1000 samples per second is 1ksps. One sample consists of a number of bits depending on the resolution. The bps is the bits per second, or the number of bits per sample multiplied by the number of samples per second. – Majenko Feb 22 '21 at 14:22
  • What are you actually trying to achieve? Don't tell us what you think you want to do, but tell us what you want to achieve as the end result. – Majenko Feb 22 '21 at 14:24
  • 2
    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 '21 at 14:27
  • @Majenko thanks for the valuable information. i am actually trying to train a model for baby crying detection. and then deploying that model on arduino. since many machine learning algorithms require heavy processing power for deployment, but logistic regression method is very lightweight to deploy. in this method we take 100 samples from recorded audio on arduino, and 100 weights from the trained model from dataset. then we multiply these two and then add them. and then apply sigmoid function. then give a threshold of 0.5. the result will be a numerical value and will tell whether baby crying – Muhammad Waqar Anwar Feb 22 '21 at 14:32
  • @Majenko yes that will be 20SPS and we will have 100 samples for a 5 second audio – Muhammad Waqar Anwar Feb 22 '21 at 14:33
  • @Majenko, Baby whale doo doo doo doo doo, baby whale doo... there, now it's stuck in your your head. Thanks for that. – timemage Feb 22 '21 at 15:17

1 Answers1

1

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 */ } samples[i] = analogRead(mic_pin); last_sample += sample_period; }

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

// Predict.
if (sum &gt;= 0) {
    Serial.println(&quot;The baby is crying!&quot;);
}

}

Edgar Bonet
  • 43,033
  • 4
  • 38
  • 76
  • 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 :) – Muhammad Waqar Anwar Feb 22 '21 at 16:13