atomic14 / m5stack-core2-audio-monitor

A fun little project to test out the mic on the M5Stack CORE2
Creative Commons Zero v1.0 Universal
69 stars 17 forks source link

Need Help: Two different M5 Core 2 device giving different output. #5

Open bhupiister opened 2 years ago

bhupiister commented 2 years ago

I have set a sample code to record audio signal. One device is with my partner in USA and other is with me in India. Both device are M5 Core 2. We both get different Zero line for output. Our code is 100% same as same firmware is built and flash by me.

My Partner's output image My output image

My I2S setting to record audio

esp_err_t err;
const i2s_port_t I2S_PORT = I2S_NUM_0;
// i2s config for reading from both m5stack mic
i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),
    .sample_rate = 16000,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
    .channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
    .communication_format = I2S_COMM_FORMAT_I2S,
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 4,
    .dma_buf_len = dmaBufCount,
};

// i2s pins
i2s_pin_config_t i2s_pins = {
    .bck_io_num = GPIO_NUM_12,
    .ws_io_num = GPIO_NUM_0,
    .data_out_num = I2S_PIN_NO_CHANGE,
    .data_in_num = GPIO_NUM_34};

This is in Loop of 2 milliseconds to record audio. (Sample rate 500Hz)

  esp_err_t result = i2s_read(I2S_PORT, i2sData, 16, &bytesRead, 10);
  if (result == ESP_OK && bytesRead >= 16)
  {
    for (int i = 0; i < bytesRead / 2; i += 2)
    {
      soundAvg += (i2sData[i] + i2sData[i + 1]) / 2;
    }
    soundAvg = soundAvg / 8;
    AvgSound = soundAvg;
  }

If my settings are incorrect. Can you please let me know what should be the optimum setting to record at 500Hz, we are capturing industrial noise along with vibrations being recorded in same loop from MPU6886.

DrJaymz commented 1 year ago

I think the problem is that the microphone is terrible and that every one is different. Mine has a huge DC offset which then settles after a moment and it looks like they all do and they all start at a different value. For frequency analysis though, provided you get clear of the initial spike you can remove the DC value and the frequency will analysis will still be valid. If you got a better result somehow let me know.

sybrenstuvel commented 1 year ago

I've also noted a significant DC offset with my device. I solved it by averaging the samples and removing the average. Seems to work pretty well: https://github.com/atomic14/m5stack-core2-audio-monitor/commit/22b7b0f0f2e816ae4f983fb7f71c43ed33ec2d6a