2
$\begingroup$

I'm working on a little audio/embedded systems project, and I'm using synthesis to generate waveforms and feeding them into DAC and speakers. Currently, I am able to produce sine waves of individual frequencies, and they sound decent.

Now I'd like multiple frequencies to play together, so that it sounds like a music chord. But the result I'm getting is a low buzz (an octave lower?). For example, I generate samples ticking at 440Hz and 660Hz. I used a tuner app to verify these individual frequencies were produced. But when I combined the two together by adding them and dividing by 2, a low buzz is produced and the tuner reads 220Hz. I suppose the 220Hz is to be expected, but I can only hear the low frequencies, and none of the high 440Hz/660Hz frequencies.

By the way, here is how I'm buffering the signals:

const uint16_t DAC_MAX = (1 << 12) - 1;
const uint16_t AMPLITUDE = DAC_MAX / 2;
uint16_t buffer[BUFFER_SIZE];

// signal.tick() returns [-1.0f, 1.0f].
buffer[i] = (signal1.tick() + signal2.tick()) / 2.0f * amplitude + amplitude;

Any idea what I'm doing wrong?


My sampling frequency is 21000Hz.


Thanks for all the helpful answers and comments. I'll post our follow-up and eventual solution here for posterity.

We tried a second speaker with no results.

Eventually, we switched to a pre-packaged speaker for end-users that came with an audio jack/USB. (We were using some low-end woofers before.) We got rid of the amplifier, since the speakers seem to have amplifiers built-in. We connected the DAC to the audio jack and it produced sounds perfectly (even for multiple tones). The software was not the issue.

I'm not sure if it was an issue with the amplifier or the woofer, but this was how we solved it.

$\endgroup$
5
  • $\begingroup$ What is your sampling frequency? Please edit your question to add Fs. $\endgroup$
    – Juancho
    Commented Oct 28, 2022 at 19:02
  • 1
    $\begingroup$ Note that the sum of those 2 sinusoids is a periodic signal of fundamental frequency 220Hz, so your tuner is picking up this. $\endgroup$
    – Juancho
    Commented Oct 28, 2022 at 19:03
  • 1
    $\begingroup$ Why are you adding amplitude? $\endgroup$
    – Knut Inge
    Commented Oct 28, 2022 at 22:06
  • $\begingroup$ Tim Wescott are absolutely right in their answer regarding the subjective perception of generated signal/sound (indeed belongs to the field of psychoacoustics). Nevertheless, I believe you should also verify the generated signal by using an oscilloscope or recording the signal and performing a simple frequency analysis (using any audio editor like Audacity, nowadays). You should be able to witness the two frequency components (or even compare the time domain signal with an analytical/theoretical one), otherwise you are doing something wrong (apart from adding the amplitude DC term) $\endgroup$
    – ZaellixA
    Commented Oct 29, 2022 at 9:36
  • $\begingroup$ @KnutInge The DAC output is a 12-bit unsigned integer. So I'm basically mapping the sample from $[-1, 1]$ to $[0, 2^{12}-1]$, with amplitude=$(2^{12}-1) / 2$. $\endgroup$
    – TrebledJ
    Commented Oct 29, 2022 at 9:51

3 Answers 3

2
$\begingroup$

but I can only hear the low frequencies, and none of the high 440Hz/660Hz frequencies.

You're basically doing a crude simulation of a bell tuned to A3 (220Hz). Bells sound a spectrum that is missing the fundamental frequency -- yet it's the note at that fundamental that you hear. This is because when you present the human ear with a series of overtones of some frequency that doesn't actually contain that frequency, you still hear it.

I'm not sure that I can say that any sound that repeats once every 220th of a second will sound like an A3, but I suspect that, yes, it will, and certainly most sounds that have elements at $(220 \mathrm{Hz}) n,\ n \in [2, 3, \cdots)$ will sound at A3.

This is just how humans interpret music. The whole subject is called psychoacoustics, if you want to delve deep.

If you want to play a chord, calculate the frequencies for a series of notes -- i.e. C4, E4, G4, and sound those tones together. A sine wave sounds a lot like a flute, so at first you'll be listening to a flute trio -- but in my experience, you'll get a recognizable chord.

$\endgroup$
2
  • 1
    $\begingroup$ Interesting. Thanks for the answer. I heard about psychoacoustics, but never imagined it was this quirky. I guess my question now is how can I play multiple notes like a synth/keyboard would do. Any pointers for this? $\endgroup$
    – TrebledJ
    Commented Oct 28, 2022 at 20:05
  • $\begingroup$ Can you clarify what "sound those sounds together" means? Do you mean additive synthesis, like what I've been doing with two frequencies, but now adding a third note? $\endgroup$
    – TrebledJ
    Commented Oct 29, 2022 at 9:53
1
$\begingroup$

When you add two sine waves that have two different frequencies, $f_1$ and $f_2$, you’ll produce a sine wave whose frequency is the average of $f_1$ and $f_2$. But the amplitude of that resultant sine wave will fluctuate at a frequency of the difference between $f_1$ and $f_2$. See this web page for an explanation of this phenomenon.

$\endgroup$
1
$\begingroup$

I tried fooling around to recreate in Matlab. Not sure that I hear the same as you:

f = [440; 660];
A = 0.5*ones(size(f))';
Fs = 16e3;
T = 1;
t = 0:1/fs:T-1/fs;
x = A*sin(2*pi*f*t);
soundsc(x, fs)

nbits = 8;
x_q = round(x*2^(nbits-1));
x_dq = x_q ./ 2^(nbits-1);
soundsc(x_dq, fs)

To me it just sounds like a Hammond organ tone with the 8’ and 5 1/3’ bars out. Which it pretty much is (except the accuracy of additive synthesis was not all that much in the 1930s), see, for example.

$\endgroup$

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.