Page 1 of 1

I2S DAC overshooting signal form

Posted: Mon Sep 16, 2019 7:09 pm
by dg9ngf
Hello,

I'm playing with my first experiments using an ESP32. My current aim is to play a fixed frequency sine wave as long as a button is pressed. The tone should start and end smoothly and goes through an audio amplifier to a loudspeaker. In the future, the button presses will be analysed and Morse code will be decoded.

For now I'm having some issues with the waveform generation (like the frequency is wrong and the are noisy sounds at the start and end of a tone, but I'll look into this further and may create another topic for that). One of them is that the signal looks a bit strange to me. I've attached a screenshot of what I see on the oscilloscope, measuring the DAC output to ground. It looks the same with or without the amplifier in place. The DAC is connected over two resistors (10k and 330 Ω) to ground, in the middle point the amplifier may be connected.

The timing of the discrete voltage levels looks good, resulting in about 44 kHz sampling rate. Just the transistions between the levels look odd. Is this normal? Am I measuring wrong?

Here's some extract of my code from Arduino:

Code: Select all

#include "driver/i2s.h"

// I2S configuration 
int i2s_num = 0;   // I2S port number
i2s_config_t i2s_config =
{
  .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
  .sample_rate = 44100,
  .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
  .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  .communication_format = I2S_COMM_FORMAT_I2S_MSB,
  .intr_alloc_flags = 0,   // Default interrupt priority
  .dma_buf_count = 8,
  .dma_buf_len = 55,
  .use_apll = false
};

void setup()
{
  // Initialize I2S with configurations above
  i2s_driver_install((i2s_port_t)i2s_num, &i2s_config, 0, NULL);
  i2s_set_dac_mode(I2S_DAC_CHANNEL_RIGHT_EN);   // Pin 25

  while (true)
  {
    // ...
    uint8_t buffer[i2s_config.dma_buf_len];
    // ...
    size_t bytes_written;
    i2s_write((i2s_port_t)i2s_num, buffer, i2s_config.dma_buf_len * sizeof(uint8_t), &bytes_written, 100);
  }
}

void loop()
{
}

Re: I2S DAC overshooting signal form

Posted: Tue Sep 17, 2019 3:20 am
by ESP_Sprite
This looks more-or-less normal. Internally, the DAC consists of a bank of resistors that are switched on or off. The glitch you see is probably from the fact that not all switches switch at exactly the same time. I wouldn't worry about them: there is very little energy in them and the rest of your audio path is probably lowpass enough that it'll filter it out.

Re: I2S DAC overshooting signal form

Posted: Mon Aug 24, 2020 4:49 am
by felixcollins
This looks like normal ringing to me due to the fact that the dac is trying to make a square edge in a band limited system. Like the other answer says, your audio path will filter out most of the high frequency signal to leave only audio frequencies [https://en.wikipedia.org/wiki/Ringing_(signal)]

Re: I2S DAC overshooting signal form

Posted: Wed Aug 26, 2020 2:40 am
by jgustavoam
Playing audio with 8 Bits DAC?
Of course, you are hearing noisy sound.
For audible audio, I suggest that you use at least a 12 or 16 bit DAC.

Re: I2S DAC overshooting signal form

Posted: Sun Nov 05, 2023 12:23 pm
by stevesch
You can actually get pretty decent audio with a properly filtered 8-bit DAC output. An ideally reconstructed signal would apply a low-pass of half of your sampling rate to a stream of DAC impulses. Applying a the same low-pass to the sample-and-hold DAC will give you an approximately correct value (you're effectively adding a bunch of slightly phase-shifted versions of the same signal together across the period of the sample, or if you're familiar w/signal processing, your impulse train has been convolved with a boxcar filter). Like others have said, your audio system, intentionally or not, is usually some kind of low-pass filter. You can just hope for that to work, but you might do a bit better by applying a specifically-designed low-pass filter on the DAC output.

TL;DR: apply a low-pass (half of your sampling rate, or a bit less) to your signal. Simplest low-pass is just an RC filter. If you want better quality you can look up better low-pass filters.

Re: I2S DAC overshooting signal form

Posted: Thu Nov 09, 2023 8:31 pm
by felixcollins