# Why does dot product return zero at non-harmonic frequency

If I understand correctly, dot product between two sinusoids should return zero if they are orthogonal. Since dot product is at the heart of the DFT, we can only clearly compare the frequencies that are multiples of the frequency from the equation: (sample frequency / number of samples). If our sinusoid is not a multiple of this frequency, we get spectral leakage. This all seems fine, but I can't understand why does the dot product return zeros at some non-harmonic frequencies as seen in the third example. Let's suppose we have the following:

Sampling frequency = 512 Hz Number of samples = 512

Our fundamental frequency is then: 512 Hz/512 = 1 Hz, so our DFT bins would represent frequencies from 0, 1, 2, ..., N - 1.

Example 1:

sin1 = real sinusoid with frequency 5 Hz

sin2 = real sinusoid with frequency 5 Hz

Dot product with these two real sinusoids returns 256 as expected.

Example 2:

sin1 = real sinusoid with frequency 5 Hz

sin2 = real sinusoid with frequency 6 Hz

Dot product returns zero as expected.

Example 3:

sin1 = real sinusoid with frequency 5 Hz

sin2 = real sinusoid with frequency 5.5 Hz

Dot returns zero, but I don't understand why. Why do we get zero here instead of a non-zero number that would represent spectral leakage (as the second sinusoid is not a multiple of a fundamental frequency)?

Example 4:

sin1 = real sinusoid with frequency 5 Hz

sin2 = real sinusoid with frequency 5.75 Hz

We get non-zero value, so this works as expected as we get spectral leakage.

Is my understanding wrong?

Note that when you compute the DFT, you compute the dot product with a sine term and with a cosine term. In your third example, even though the dot product with the sine term is zero, the dot product with the cosine term isn't, so you do get spectral leakage, as you expected. The dot product with the sine term is zero because you have a whole number of periods plus one half period inside the DFT frame, so the periodized version of the signal is even and, consequently, the imaginary part of its DFT (the dot product with the sine term) vanishes.

Dot returns zero, but I don't understand why. Why do we get zero here instead of a non-zero number

Luck mostly. It's only zero because you chose the right phase difference. If you add any non-trivial phase to either one of the sine waves, you would get a non zero result. That's NOT the case for 5Hz and 6 Hz, where the dot product is zero regardless of the phase of either one.

Let's look at it a different way: Multiplying two sine waves results in a signal that contains the sum and difference frequencies. So if you have the product of 5 Hz and 6 Hz you get 1 Hz and 11 Hz in the result. These sum/difference frequencies have integer number of periods in your observation window, i.e. they sum to zero individually. So the sum over the 1 Hz sine wave is zero and the sum over the 11 Hz sine wave is zero, regardless of which phase they have.

If you multiply 5 Hz and 5.5 Hz you get 0.5 Hz and 10.5 Hz. These do NOT have integer numbers of periods so they do NOT sum to zero. So in general this will indeed be non zero. However, there is always one phase difference where the two components cancel and that's exactly what happens here. The dot product as a function of phase difference can be positive and negative and it has to go through zero somewhere

You can ALSO make the dot product between 5Hz and 5.75Hz disappear, you just need to add a phase of $$-\pi /4$$ to the 5.75Hz sine wave.

• "It's only zero because you chose the right phase difference." which is why you should be very careful doing math by experiment -- particularly when you can just learn analysis, and do it by proof. – TimWescott Aug 7 '20 at 18:41
• @TimWescott I say throw caution to the wind when doing math by experiment if that is your druthers. Then go learn the theory for the things you can't figure out or understand. Learning the theory should always be followed by numerical confirmation, and playing with numbers should be followed by learning (deriving and/or researching) the theory. Some number playing will lead to others and some theory will lead to others. YMMV. There isn't always one application to every math tool either. – Cedron Dawg Aug 7 '20 at 19:45
• I didn't say don't do it -- just be careful. And if you can do it by analysis then you can get the whole answer at once, instead of a -- possibly misleading, as in this case -- point answer. Sometimes you just have to do math by experiment -- but you should be aware of the limitations. – TimWescott Aug 7 '20 at 22:11

If you want to learn about the DFT standalone you need to ditch the Hz.

DFT exercise in the book Understanding digital signal processing 3 Ed

Any two Sine or Cosine waves multiplied together are the same as the sum or the difference of two others. It's just a matter of getting the phases line up.

$$\cos(X)\cos(Y) = \frac{1}{2} \left( \cos(X+Y) + \cos(X-Y) \right)$$

Add $$\pi/2$$s to taste to $$X$$ or $$Y$$.

Spectral leakage occurs when the thing doesn't have a whole number of repeats in the frame.