5.4. Phase#

The examples we’ve seen so far have all used cosine waves, but what if we had used a sine wave instead of a cosine for our signal \(\blue{x}\)?

Continuing the example in the previous section, we’ll use \(\red{m=3}\) to generate a sine wave at an analysis frequency with \(f_s=20\) and \(N=40\):

\[\blue{x[n] = \sin\left(2\pi \cdot \frac{3}{40} \cdot n \right)}.\]
illustration of zero-similarity between analysis frequencies

Fig. 5.11 A sine wave \(\blue{x[n]}\) at an analysis frequency results in similarities \(\purple{S=0}\) for all reference signals \(\red{y_m}\).#

Fig. 5.11 is not an error — all similarity scores here are 0, including the comparison to \(\red{m=3}\).

To make sense of this, we’ll first convert the \(\sin\) wave into a standard cosine form by using Equation (1.5):

\[\begin{align*} \blue{x[n]} &= \sin\left(2\pi \cdot \frac{3}{N} \cdot n\right) & \text{by definition of } x\\ &= \cos\left(\frac{\pi}{2} - 2\pi \cdot \frac{3}{N} \cdot n\right) & \text{by the conversion rule}\\ &= \cos\left(2\pi \cdot \frac{3}{N} \cdot n - \frac{\pi}{2}\right) & \cos(\theta) = \cos(-\theta). \end{align*}\]

Now that we have \(\blue{x}\) in cosine form, we can apply the reasoning from above to calculate each similarity score:

\[\begin{align*} \purple{S(x, y_m)} &= \sum_{n=0}^{N-1} \blue{\cos\left(2\pi \cdot \frac{3}{N} \cdot n - \frac{\pi}{2}\right)} \cdot \red{\cos \left(2\pi\cdot\frac{m}{N} \cdot n \right)}\\ &= \frac{1}{2} \sum_{n=0}^{N-1} \left(\blue{\cos\left(2\pi \cdot \frac{3 + m}{N} \cdot n - \frac{\pi}{2}\right)} + \red{\cos \left(2\pi\cdot\frac{3-m}{N} \cdot n - \frac{\pi}{2}\right)} \right) \end{align*}\]

This looks almost identical to our first example, except that we now have phase offsets of \(-\pi/2\) in both terms of the summation. If \(\red{m \neq 3}\), this phase difference will not matter: summing over all samples \(n=0\dots N-1\) will still produce a total of \(\purple{S=0}\).

However, if \(\red{m=3}\), the first term (frequency index \(\blue{3+m}\)) will again cancel to 0 when summed, but the second term (\(\red{3-m=0}\)) will simplify to

\[\red{\cos\left(2\pi \cdot \frac{0}{N}\cdot n - \frac{\pi}{2} \right)} = \cos\left(-\frac{\pi}{2} \right) = \cos\left(\frac{\pi}{2} \right) = 0. \]

So, rather than getting a contribution of \(\cos(0) = 1\) for each term in the summation like our first example, we instead get a contribution of 0, and the total summation results in \(\purple{S=0}\).

This is a huge problem.

Remember that our goal is to represent the frequency content of a signal \(\blue{x}\) by comparing it against a collection of reference signals of known frequencies. But what we’ve just shown is that a signal can have exactly the same frequency as one of our reference signals, and still produce a score of 0.

This example of a sine wave is in some ways the worst-case scenario. In a bit more generality, we can consider a signal \(\blue{x}\) with an analysis frequency index \(\red{m \neq 0}\) and arbitrary phase offset \(\phi\):

\[\blue{x[n] = \cos\left(2\pi \cdot \frac{m}{N} \cdot n + \phi\right)}\]

and by substituting \(\phi\) for \(-\pi/2\) in the above derivation, we generally have

(5.8)#\[\begin{split}\purple{S(x, y_m)} = \begin{cases} \frac{N}{2} \cdot \cos\phi &\text{if } m \neq \frac{N}{2}\\ N \cdot \cos\phi & \text{if } m = \frac{N}{2}. \end{cases}\end{split}\]