I am working on a Baby Crying Detection model using logistic regression.

Out of $581$ audios, $222$ are of a baby crying. Each audio is of $5$ seconds.

what I have done is convert each audio into numbers. and those numbers go into a .csv file. so first I took $100$ samples from each audio, then $1000$ samples, and then all $110250$ samples into a .csv file, and at the end of each of them was a number 1 (crying) or 0 (not crying). Then I trained the model using logistic regression from that .csv file.

The Problem I m facing is that with $100$ samples the 64% accuracy on each audio, while with 1000 samples and 110250 samples(Full dataset) it reaches to 66% accuracy only. How can I improve the accuracy of my model to upto 80% using logistic regression.

I can only use simple logistic regression because I have to deploy the model on Arduino.

  • 1
    $\begingroup$ How are you processing the raw audio samples "into numbers". For audio processing this is typically some variant of FFT power spectrums. MFCC has been popular for speech. Are you doing anything like that? Please explain more about the pre-processing before reaching the predictive model and edit those details into the question. In addition, is sampling triggered by anything (such as hitting a volume threshold)? Or is sampling done at fixed intervals? $\endgroup$ Mar 27, 2021 at 18:44
  • $\begingroup$ Simply i use librosa library to get audio files into an array. then that array is pasted into a CSV file. I know about MFCC. lets say i pick 100 samples. I pick 100 samples at equal intervals, then those 100 samples per audio (100x581 table of total audios) go through logistic regression which gives output of 100 weights or coefficients. Then arduino takes 100 samples from live recording, these 100 samples and trained model 100 samples are multiplied one to one, then added, then sigmoid function is applied. if output value is greater than 0.5 it means baby is crying. my friend told this method $\endgroup$ Mar 28, 2021 at 11:40
  • $\begingroup$ i cannot use MFCC because i need to deploy the model on arduino and all the calculation on arduino needs 100 weights, while MFCC outputs just one value per audio $\endgroup$ Mar 28, 2021 at 11:42

1 Answer 1


Try Rectification

Improve the features available to your model, Remove some of the NOISE present in the data.

  • In audio data, a common way to do this is to smooth the data and then rectify it so that the total amount of sound energy over time is more distinguishable.
# Rectify the audio signal
audio_rectified = audio.apply(np.abs)
  • You can also calculate the absolute value of each time point. This is also called Rectification because you ensure that all time points are positive.

  • Smooth your data by taking the rolling mean in a window of say 50 samples

audio_rectified_smooth = audio_rectified.rolling(50).mean()
  • Calculating the envelope of each sound and smoothing it will eliminate much of the noise and you have a cleaner signal.

Calculate Spectrogram

  • Calculate a spectrogram of sound(i.e combining of windows Fourier transforms). This describes what spectral content (e.g., low and high pitches) are present in the sound over time. there is a lot more information in a spectrogram compared to a raw audio file. By computing the spectral features, you have a much better idea of what's going on.

    • this is similar to how we calculate rolling mean :

      • We calculate multiple Fourier transforms in a sliding window to see how it changes over time. For each time point, we take a window of time around it, calculate a Fourier transform for the window, then slide to the next window. The result is a description of the Fourier transform as it changes throughout the time-series called a short-time Fourier transform or STFT.

        • Choose a windows size and shape
        • At a timepoint, calculate the FFT for that window
        • Slide the window over by one
        • Aggregate the results
    • Calculating the STFT

      • We can calculate the STFT with librosa $\rightarrow$ import librosa as lr
      • There are several parameters we can tweak (such as window size)
      • For our purposes, we'll convert into decibels which normalizes the average values of all frequencies.
      • We can then visualize it with the specshow() function

This is how you can calculate the STFT

# Calculating the STFT
# Import the functions we'll use for the STFT
from librosa.core import stft, amplitude_to_db
from librosa.display import specshow

# Calculate our STFT
audio_spec = stft(audio, hop_length=HOP_LENGTH, n_fft=SIZE_WINDOW)

# Convert into decibels for visualization
spec_db = amplitude_to_db(audio_spec)

# Visualize
specshow(spec_db, sr=sfreq, x_axis='time', y_axis='hz', hop_length=HOP_LENGTH)

Try Spectral feature engineering

you can also perform the Spectral feature engineering on your baby audio data

  • since each time series has a different spectral pattern.
  • We can calculate these spectral patterns by analyzing the spectrogram.
  • For example, spectral bandwidth and spectrum centroids describe where most of the energy is at each moment in time.
# Calculate the spectral centroid and bandwidth for the spectrogram
bandwidths = lr.feature.spectral_bandwidth(S=spec)[0]
centroids = lr.feature.spectral_centroid(S=spec)[0]
# Display these features on top of the spectrogram
ax = specshow(spec, x_axis='time', y_axis='hz', hop_length=HOP_LENGTH)
ax.plot(times_spec, centroids)
ax.fill_between(times_spec, centroids - bandwidths / 2,centroids + bandwidths / 2, alpha=0.5)

Now you can Combine spectral(spec $\rightarrow$ spectral dataframe) and temporal features in a classifier

centroids_all = []
bandwidths_all = []
for spec in spectrograms:
    bandwidths = lr.feature.spectral_bandwidth(S=lr.db_to_amplitude(spec))
    centroids = lr.feature.spectral_centroid(S=lr.db_to_amplitude(spec))
    # Calculate the mean spectral bandwidth
    # Calculate the mean spectral centroid
# Create our X matrix
X = np.column_stack([means, stds, maxs, tempo_mean, tempo_max, tempo_std, bandwidths_all, centroids_all])

for your logistic regression models

  • One of the ways to improve accuracy is by optimizing the prediction probability cutoff scores generated by your logit model.
  • You can Normalize all your features to the same scale before putting them in a machine learning model.
  • Look for class imbalance in your data.
  • You can Optimize on other Metrics also such as Log Loss and F1-Score.
  • Tune the hyperparameters of your model. In the case of LogisticRegression, parameter $C$ is a hyperparameter.
  • 1
    $\begingroup$ thanks alot for giving your time :) the audio files have nearly no noise in it as they are specially recorded sounds from a mobile application of baby crying. i have tried normalizing the values in the array from the range of -1 to 1, TO 0 to 1. Moreover i have tried using ANN which uses spectrograms method and it gave me an accuracy of 100% after 100 epoches. The problem is that i cannot use spectrograms or MFCCs, kindly read my comments in the above question so you will have an idea. Well my data is imbalanced since 222 out of 581 audios are of baby crying. Thanks alot once again! :) $\endgroup$ Mar 28, 2021 at 11:57
  • 1
    $\begingroup$ @MuhammadWaqarAnwar FFT is an efficient algorithm, and libraries for it are available for the arduino - see e.g. youtube.com/watch?v=djGOOL2mqV8 $\endgroup$ Sep 26, 2021 at 14:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .