2
\$\begingroup\$

So in my embedded systems class we were working with DACs and we build a resistor ladder. We then sent data to the resistor ladder using timers to generate sine waves at a certain frequency. This produced the sound we wanted to hear.

Since then I've build a 10bit DAC resistor ladder with a sampling rate of 1024Hz. I'm trying to send two notes at once to the DAC. I take the current value from the wave of one note, add it with the value of the wave of the other note, scale it appropriately so it doesn't exceed the max range, and then send this combined number to the DAC.

Unfortunately, this is creating a very gross distortion sound. Buried in the noise I can hear the chord I'm trying to produce, but why is there so much noise in the first place? Shouldn't making a chord be as simple as adding the sine waves together?

\$\endgroup\$
6
  • \$\begingroup\$ Are you scaling each sample by the same factor? \$\endgroup\$ Nov 27, 2016 at 10:15
  • \$\begingroup\$ How are you scaling the sum? \$\endgroup\$
    – JRE
    Nov 27, 2016 at 10:15
  • \$\begingroup\$ What sine wave frequency are you generating and is it below half the sample rate to avoid aliasing? \$\endgroup\$
    – Andy aka
    Nov 27, 2016 at 10:16
  • \$\begingroup\$ So basically I have two arrays of length 1024 with values 1024 in the "shape" of a sine wave. I cycle through the sine wave at a certain frequency to produce a sound. I use the same array for multiple note but different indices. So to the DAC I write (wave[i1]/10 + wave[i2]/40), where i1 and i2 are incremented at different rates \$\endgroup\$
    – rcplusplus
    Nov 27, 2016 at 10:46
  • \$\begingroup\$ The length of an array has nothing to do with sampling rate. \$\endgroup\$
    – Andy aka
    Nov 27, 2016 at 11:22

1 Answer 1

3
\$\begingroup\$

A sample rate of 1024 Hz is very low. You will need to filter the output of your DAC with a low-pass reconstruction filter that has very little response (at least 30dB of attenuation) above 500 Hz in order to eliminate the "image" frequencies created by the sampling process.

Suppose you were trying to generate tones at 100 Hz and 150 Hz. The raw output of your DAC will also have frequency components at

  • 1024 - 150 = 874 Hz
  • 1024 - 100 = 924 Hz
  • 1024 + 100 = 1124 Hz
  • 1024 + 150 = 1174 Hz

in additional to higher-order components. These additional components are not harmonically related to your original tones, so you perceive them as "noise".

Normally, a DAC for audio applications would have a sampling rate about 10x to 100x the rate you're using, with a matching output filter. Commonly used sample rates include:

  • 8 kHz (audio up to 3400 Hz) - called "voice grade" because this is used for telephone circuits.
  • 16 kHz (audio up to 7500 Hz) - AM radio grade
  • 32 kHz (audio up to 15 kHz) - FM radio grade
  • 44.1 kHz (audio up to 20 kHz) - "CD quality"
  • 48 to 192 kHz - professional audio rates
\$\endgroup\$
9
  • 1
    \$\begingroup\$ Just in case someone gets the wrong idea: 10x (i.e. 10KHz) is the absolute low end, and would sound quite awful for music other than chiptunes. 22KHz is acceptable, but in most cases you'll want 44KHz or above (48KHz and 96KHz are also common) for CD quality audio. You can find a wide range of external DACs with 16 or 24 bit sample resolution at these frequencies. \$\endgroup\$
    – Polynomial
    Nov 27, 2016 at 12:49
  • 1
    \$\begingroup\$ Oh man... thats gonna put a huge strain on our board. Right now we use timers and ISRs to scroll through waveforms. I just did the math and to play a C5 note at 8000Khz sampling frequency, the ISR needs to be called every 19 cycles (our clock speed is 80Mhz). I'm gonna need to strip down the ISR code so that it's less that 19 cycles, that might be hard. \$\endgroup\$
    – rcplusplus
    Nov 27, 2016 at 17:21
  • 1
    \$\begingroup\$ Also I may be using the term sampling frequency wrong... By that I mean that we sampled a sine wave period of 2pi 1024 times to get an array of values, a lookup table essentially. We just scroll through this sine wave table faster to generate higher frequencies. Not sure if that changes anything... \$\endgroup\$
    – rcplusplus
    Nov 27, 2016 at 17:27
  • 1
    \$\begingroup\$ No, you should not need an 8000 kHz (8 MHz) sampling frequency. C5 is 523.25 Hz, and if you have a sine table of 1024 entries and a sample rate of 8 kHz, you would step through it at a rate of \$\frac{1024 steps/cycle * 523.25 cycles/sec}{8000 samples/sec} = 66.976\$ steps per sample. Note how the unit analysis works out. You can generate non-integer steps by using dds techniques. \$\endgroup\$
    – Dave Tweed
    Nov 27, 2016 at 17:35
  • 1
    \$\begingroup\$ Ok, but then what's causing the ugly distortion sound? I'm cycling through the same sine wave array with 2 different indexes (each scrolling through at a different frequency), taking the value from the lookup table, adding them with the formula A+B-AM/1024, and then writing to the DAC. I still get the following noise though: soundcloud.com/rutvik-choudhary/dac-output-test-1 \$\endgroup\$
    – rcplusplus
    Nov 27, 2016 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.