I'm searching for a way to parse a signal that mixes multiple square waves of different frequencies.

The intended output is the frequency of each of the mixed square waves.

This answer provides an example of mixing signals, he uses OR gate as an example, but we could change to a more appropriate one if needed (as to highlight more details - as XOR).

Where should I find my answer? or what would be the algorithm(s) to go?


This is a modified MATLAB code that plots the output of OR/XOR:

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

plot(p1);title('square wave 1');
plot(p2);title('square wave 2');
plot(p1 | p2);title('the OR summed waves');
plot(bitxor(p1, p2));title('the XOR summed waves');

Which outputs:

enter image description here

  • 2
    $\begingroup$ What exactly do you know and don't know about the original square waves. Frequency, phase? Is the phase the square waves phase-looked to the sample rate ? Do you have jitter ? $\endgroup$
    – Hilmar
    Commented Feb 8, 2023 at 10:44
  • $\begingroup$ Frequencied in unknown (to be measured), signals aren't necessarily in-phase, there might be no sampling needed (interrupt-based signals). $\endgroup$ Commented Feb 9, 2023 at 10:20

1 Answer 1


To parse the frequencies, as in determine the frequency content of an unknown signal, use the FFT. If the signals were accurately summed, then the result would be the linear combination for each of the square waves where each square wave has a fundamental and odd harmonics that go down as $1/n$ where $n$ is the harmonic number. If the signals are summed with an OR gate, we have the additional complication that the output is the hard saturation of the linear sum. The hard saturation removes the AM but preserves the PM. The PM contains the information of all the fundamental frequencies and thus would be at the strongest bins in the FFT (omitting the DC bin).

If a sum of the inputs is desired, then an OR gate should be used and not an XOR. The OR gate would implement a sum with saturation to square wave at the output while the XOR gate is a time domain multiplier with saturating inputs.

Below is a test condition with a 10 MHz clock and f1= 700 kHz and f2=1.4 MHz, square waves with 50% duty cycle.


Below is the result of a simple OR gate as Fat32 has suggested in the linked post given in the OP, where we see the two dominant bins being consistent with the original frequencies.

OR as sum

Below is the case for the XOR where we get the sum and difference frequencies (consistent with a product, but is not a sum; and what “mixing” is referred to outside of audio design):


  • $\begingroup$ Thanks for your detailed answer, I'd need more time to fully digest its details, but generally: by observing the spectrum of OR/XOR(F1,F2), how to filter the original frequencies? (it's not clear to me - even by visual analisys). $\endgroup$ Commented Feb 9, 2023 at 10:34
  • $\begingroup$ I think you mean f1=700kHz $\endgroup$ Commented Feb 9, 2023 at 10:35
  • $\begingroup$ @HamzaHajeir, yes correct 700kHz. I'll fix that. Is it your choice to use OR or XOR, or are you given either and from that need to determine the frequencies? For the case of OR, we see the frequencies directly in the FFT (note in my example, the two tones at 700kHz and 1.4 MHz are strongest, ignoring the one at DC which is removed by just subtracting the mean before taking the FFT). For the case of the XOR, as I noted this would be the sum and the difference consistent with what we see in that case: 700kHz+1.4MHz = 2.1 MHz, and 1.4MHz-700kHz = 700 kHz, so 700kHz and 2.1MHz as shown. $\endgroup$ Commented Feb 9, 2023 at 17:16
  • $\begingroup$ Nice, so when it's 3 components, I'd check for the top 3 frequencies.. correct? I'm not limited to two choices, I'm checking out what available solutions for the question, so it'd be great if you could suggest/recommend logic operation(s) for such a question! $\endgroup$ Commented Feb 11, 2023 at 15:16
  • $\begingroup$ A note, it seems that the frequencies of XOR are little different, as the strongest two frequencies seems to be (2MHz+2.2MHz) and (0.6MHz+0.8MHz), so the 700kHz and 2.1MHz are their average? $\endgroup$ Commented Feb 11, 2023 at 15:19

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.