1
\$\begingroup\$

I have an idea for an Arudino project which is essentially a piezo buzzer music machine that I will be able to program sheet music into. It would consist of individual 555 oscillator circuits at different frequencies and the arduino would control whether they (the specific note) is active or not. I've looked around to find similar projects but everyone I find only uses one or two piezo buzzers with the arduino tone(); library.

It got me thinking about multiple frequencies on one speaker/buzzer and how that works. If I had two square waves wouldn't it cause distortion by either the signals stacking when both are high or cancellation when one is high and the other is low?

I've searched a lot trying to find an answer to this question but nothing has been straight forward. It also has me questioning whether my project is a good idea or not because I wish to play a plethora of sounds.

\$\endgroup\$
10
  • \$\begingroup\$ Superposition, see also Fourier synthesis \$\endgroup\$ Commented Jul 24, 2017 at 16:16
  • \$\begingroup\$ If you play two sine waves with opposite phase, they will cancel out. Does it count as "distortion"? \$\endgroup\$
    – Eugene Sh.
    Commented Jul 24, 2017 at 16:18
  • \$\begingroup\$ @EugeneSh. that is an interesting question... \$\endgroup\$
    – Trevor_G
    Commented Jul 24, 2017 at 16:22
  • 1
    \$\begingroup\$ @Trevor I would say that a superposition of waves is not a distortion, but the way things are working naturally. If you have a drum an guitar, their "waves" are just adding together in your ear. and the combined sound is not a "distortion". \$\endgroup\$
    – Eugene Sh.
    Commented Jul 24, 2017 at 16:25
  • 1
    \$\begingroup\$ @Trevor Yes, I see your point. If we have two "instruments" working in opposite phase, we can get different sounds by putting them apart and moving the observer. But if playing from a single spot we won't hear anything. I guess that is what the sound engineers are for :) \$\endgroup\$
    – Eugene Sh.
    Commented Jul 24, 2017 at 16:33

3 Answers 3

1
\$\begingroup\$

This is really a lengthy comment as apposed to a answer to the question.

What you are trying to do is a bad idea if the point is the result. If you're doing it for fun, the challenge, or learning, that's fine, but don't expect good results.

It's going to be very difficult and tedious to keep all those analog oscillators from the 1970s in tune. Ditch the evil 666 555 timers and do it all digitally. Not only will the result be better, but it will be much easier too.

Add all the signals you want to combine digitally, then write the sum to a D/A and have that eventually drive a loudspeaker. Write a fast lookup-based sine routine, then use it for each of the tones. The frequencies are controlled by how much the indexes into the table are advanced each sample. Use maybe 16 fraction bits below each index to get plenty of frequency resolution.

Make the sine table a power of 2 in size. That means the indexes (angle arguments to the sine functions) automatically wrap to 0 when incremented past 360°.

Even brute force 10-bit sine lookup will yield far better results than a bunch of 666 timers.

This will be easier on a processor more meant for such things than whatever comes in a arduino. Something like a Microchip dsPIC can easily do this.

Let's see how the numbers work out. A dsPIC is inherently a 16 bit machine, so let's use two words for each angle. The high word will be the table index directly, and the low word fraction bits for more resolution. A EP series dsPIC can run at 70 MIPs. Let's say you want 40 kHz sample rate. That means you get 1750 instructions per sample!

For each contributing tone, you grab the high word of the angle, use it to look up the sine value from a table, add that into the accumulator, then bump the index by adding its 32 bit increment to it. let's say this takes 25 instructions per tone, including all the initialization, loop, and termination checking overhead. It should take less than that, but even with that this method can support 70 tones.

That's way better and easier than trying to keep 70 analog oscillators tuned.

\$\endgroup\$
1
\$\begingroup\$

Same as it works in air - as long as the medium is linear, two or more added signals, whether electrical or acoustic, will not interfere with each other, the resulting more complex waveform will appear to the ear or any measuring device as "frequency x at amplitude y and frequency a at amplitude b".

As soon as anything becomes nonlinear - speaker cone at end of excursion, amplifier driven out of either linear range or slew rate reserve* ... the resulting waveform will have the two or more original frequencies at a lower amplitude plus additional frequencies (usually multiples of the original frequencies).

A "linear filter" behaviour of anything in the chain (bandpass, lowpass, highpass...) will change the relative amplitudes but not introduce new frequencies.

*This can happen if there is a high-amplitude, inaudibly high pitched signal in a very wideband audio chain. Amplifiers (and speakers) have a passband (linear filter behaviour, independent of absolute amplitudes - this can't "distort" anything unless you reach -> ), excursion limits (output voltage can't go higher or lower than x, and the high pitch signal consumes that limit leaving not enough voltage for the other, and slew rate limits (how quickly the output voltage, or cone position, can change per second - this is amplitude dependent and different from the passband! Here too, the high pitch signal can consume this resource resulting in distortion of the other signals).

\$\endgroup\$
0
\$\begingroup\$

Obviously two or more signals do affect each other, the superposition answer or word is all you need to look up here. Watch a speaker in action, if you have a really low frequency the speaker moves relatively slow and a high and it moves fast, but you can generate both and tell that both are there, so how is that possible?

They must be affecting each other and they are exactly the way you would expect if you added the two together, you would say have a slow sine wave that wiggles with a faster sine wave, just do that math and graph it, there you go if the speaker is mechanically capable that is what is happening (with some delay, etc). This is how the mp3 compression works, in theory with enough going on or say a really loud low frequency can our ear pick up some quieter other frequencies going on at the same time? Nope, can we figure this and filter those out, yep, does this save overall digital bandwidth? Yep...

There is no reason for all the work you are doing, external timers. Just one processor of some sort and a dac, everything else is digital just add up the channels and divide by some number N to keep them from clipping, and or each channel has a weight or amplification applied to it. With an arduino or I would say any microcontroller no reason to use floating point stick with fixed. The mcu clock is good enough, ideally a crystal for accuracy. Mix your channels digitally (add and divide by some N) and out a DAC to an amp to the speaker. Want to prototype this? Write some software on your development computer that generates a few waves, combines them, feed them raw into audacity or some program that can take them or figure out the wave file format (somewhat trivial) or other raw format and then just play it on your computer, it is no more complicated than that. You can have a pre-computed look up table per sound that you want, for each sample period, combine the enabled sounds together and feed that to the DAC.

\$\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.