I was trying to make a frequency mixer (only difference frequency) for two square waves (PWM1, PWM2, both 0.5-4KHz range) using ONLY Arduino due.

I'm new to signal processing, and the only way I can think of making a differential frequency is purely mathematical,and only for sin waves:


The cos term is a problem because it involves 90 degree phase shift, so I cannot just use logical operators to express the function.

However during Googling I saw a statement that D flip flop can be used to produce only the difference term in frequency mixing.

That is, if I understand it correctly, attaching an interrupt to the rising edge of PWM1, in the ISP read the PWM2 value and write it to the output pin.

It make sense as the closer the frequencies of PWM1 and PWM2 is, the smaller the frequency of the output wave. However it seems too good to be true and I cannot figure out how this is mathematically equivalent to the difference part of the product of the two signals.

Will I get better accuracy if I use a real D flip flop ?


For 'frequency mixer' I mean the product of two signals. $$2cosAcosB=cos(A+B)+cos(A-B)$$ For 'the difference part of frequency mixer' I mean only the term with differential frequency (A-B).

  • \$\begingroup\$ Give the Google a try again using the term "CORDIC" and see if that helps. \$\endgroup\$ – CapnJJ Jun 18 '18 at 21:27
  • \$\begingroup\$ What is frequency mixer in your terms? What would be the input signals and what is the output? \$\endgroup\$ – Eugene Sh. Jun 18 '18 at 21:27
  • \$\begingroup\$ @EugeneSh. Both PWM1 and PWM2 is in the range of 0.5-4 KHz. \$\endgroup\$ – 7E10FC9A Jun 18 '18 at 21:35
  • \$\begingroup\$ I don't think it will be very accurate because the interrupt latency is not constant. But if the frequencies of the PWM signals are very small, then perhaps... But are you thinking about this the correct way? What are you really trying to achieve? If you make the PWM signals yourself, then surely you know their frequencies and can calculate the difference....? \$\endgroup\$ – PkP Jun 18 '18 at 21:35
  • \$\begingroup\$ @PkP PWM1 and PWM2 are from not-so-stable external clock, the output need to be phase correct. \$\endgroup\$ – 7E10FC9A Jun 18 '18 at 21:37

An XOR gate makes a good mixer, producing the sum and difference of two input squarewaves. If you only want the difference term, then the output must be low pass filtered, so is then in the analogue domain. This method is capable of producing a good difference signal even when the high frequency signals have jitter on them, due to the output low pass filter. If you want a digital output, you must follow the low pass filter with a Schmitt input buffer.

A D-latch taking one signal as D and the other as clock can produce a frequency difference of sorts, but it would be very susceptible to phase jitter on the inputs, when the two input signals are in approximately the same phase, and its setup and hold times are being violated. With clean signals, you may get reasonable transitions. With jittery signals, you would get many random up/down output transitions instead of one clean one at each change of output state.

What I've just written about a D-latch is true if the two input signals are asynchronous. In the specific case of them both being PWM signals generated from the same microcontroller by internal PWM counters, this method of generation could mean that the signals will always meet setup and hold times at the D-latch, which would make it work perfectly.

| improve this answer | |
  • \$\begingroup\$ You are right with its own PWM signal it works fine, but with external clocks (even it's clean) the output always jumping between two frequencies that is close to the correct frequency. I'm only working with below 1KHz signals but the jumping can be as high as 10Hz. Is there a way to get rid of it digitally with MCU? My initial thoughts are since I'm working with low frequency, a MCU might be acceptable. \$\endgroup\$ – 7E10FC9A Jul 16 '18 at 17:40

A D FF alone is not a mixer but a CD4046 PLL uses flip flops and gates with open drain outputs to make a Comp II” phase /freq mixer but has more noise than others but widest capture range

XOR mixer does produce sum and difference frequencies out as pulses so that includes harmonics.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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