# Mixing square waves

I want to mix two square waves of different frequencies together - I'm driving a speaker, and I'd like to play two frequencies at once on it (to whatever extent that is possible). I would just add the two signals together, but then the output signal needs to be capable of three voltage levels (if the original signals can be either 0 or 1, then the output signal could be 0, 1, or 2), but my output signal is only capable of being low or high. What is the best way I can go about this?

• Could you explain for what purpose you want to mix? Commented Oct 26, 2020 at 11:47
• I'm driving a speaker, and I'd like to play two frequencies at once on it (to whatever extent that is possible) Commented Oct 26, 2020 at 15:19
• If you're driving a speaker, you most definitely would not want to restrict yourself to 0 and 1, where does that restriction come from? Commented Oct 26, 2020 at 16:09
• I'd recommend you look into what a class-D amplifier is, then! (in short, you can use that device, but you need to switch it much faster than the frequency of your signals - but that's probably not a problem) Commented Oct 26, 2020 at 16:18
• Thank you!! OR-ing the signals seemed a bit too easy, so I was skeptical at first. I will try out both and report back. Commented Oct 26, 2020 at 16:39

A simple continuous input OR gate will do your job for strict waves of 1/0 amplitude levels.

The following is a matlab simulation of the logic OR functionality with continuous inputs.

N = 1024;
p1 = sin(2*pi*3.5*[0:N-1]/N) > 0  ;
p2 = sin(2*pi*15.5*[0:N-1]/N) > 0  ;

figure,subplot(3,1,1)
plot(p1);title('square wave 1');
axis([1,N,-0.5,1.5])
subplot(3,1,2)
plot(p2);title('square wave 2');
axis([1,N,-0.5,1.5])
subplot(3,1,3)
plot(p1 | p2);title('the OR summed waves');
axis([1,N,-0.5,1.5])


The output is:

• An XOR provides the product, or a Modulo-2 addition, but doesn't accurately provide the Add per the OP's description. (If according to an addition, the OP wants the result to represent both frequencies and not the sum and difference of both frequencies as the above would provide (as well as that of all odd order harmonics, and odd order harmonics of the results given the signals are square waves). Commented Oct 26, 2020 at 14:18
• I didn't use XOR, (though I thought at first) this is just plain OR. It's unconventional but just to get the sum of binary level square waves. It only converts 2 into 1 when both are on... Don't know if this is useful for him as an addition of two waves. As you indicate there are different conventions for mixing binary waves... Commented Oct 26, 2020 at 14:42
• I deleted my answer -- it is clearly simply this as you put and therefore very useful (Once I mapped 0 and 1 to +/-1 where you can get -2, -1, +1, +2 ---and then take the sign, if you do the truth table of that it is the OR gate. I overthunk it! Commented Oct 26, 2020 at 16:24
• @DanBoschen You didn't have to delete your answer. That also provides more sophistacated approach (compared to my naive or) along with simulation results which are quite valuable... Commented Oct 26, 2020 at 18:35
• It was a misleading answer suggesting extra steps were necessary when I then realized thanks to your post that instead of adding 0 and 1 and then dealing with the tie with random distributions (ugh!), simply add -1 and 1 instead and you never get the condition of the tie! And then that reduces to what is simply an OR, just as you stated. So not very naive at all! Commented Oct 26, 2020 at 18:55