Sums of sines and cosines
Introduction
In signal processing applications you will inevitably stumble across a summation of cosines (or sines), where the argument of the cosines form an arithmetic progression. Specifically, let's consider summations of the form:
$$S_N(\theta) = \sum_{n=0}^{N-1} \cos(n \theta)$$
This looks rather intractable a first, but with a couple of tricks involving complex exponentials and some algebra, it is possible to derive a nice closed form expression. We will do precisely that in this post.
Analysis
The key here is to use Euler's formula: $$e^{jx} = \cos(x) + j \sin(x)$$, which can be used to rewrite $$cos(x)$$ as
$$cos(x) = {1 \over 2}\left( e^{jx} + e^{-jx} \right)$$. Here $$j = \sqrt{-1}$$.
Let's rewrite $$S_N(\theta)$$ using the above rule:
$$S_N(\theta) = {1 \over 2}\sum_{n=0}^{N-1} (e^{jn \theta} + e^{-jn \theta}) = {1 \over 2}\underbrace{\sum_{n=0}^{N-1} e^{jn \theta}}_{S^+_N(\theta)} + {1 \over 2}\underbrace{\sum_{n=0}^{N-1} e^{-jn \theta}}_{S^-_N(\theta)}$$
We have split $$S_N(\theta)$$ into two geometric sums. Let's simplify $$S^+_N(\theta)$$ first.
$$S^{+}_N(\theta) = \sum_{n=0}^{N-1} e^{j n \theta}$$
$$ = {{1 - e^{jN \theta}} \over {1-e^{j \theta}}}$$ (using the standard formula for summation of a geometric series)
$$ = {{e^{jN \theta /2}(e^{-jN \theta /2}-e^{jN \theta /2})} \over {e^{j \theta /2}(e^{-j \theta /2}-e^{j \theta /2})}} $$ (factorizing the numerator and denominator)
Invoking Euler's formula again, we can write $$\sin(x)$$ as:
$$\sin(x) = {1 \over 2j}\left( e^{jx} - e^{-jx} \right)$$
Applying this to the expression for $$S^{+}_N(\theta)$$ gives
$$ S^{+}_N(\theta) = e^{j (N-1) \theta/2}{\sin (N \theta/2) \over \sin (\theta /2)}$$
With very similar analysis, we can rewrite $$S^-_N(\theta)$$ as
$$ S^{-}_N(\theta) = e^{-j (N-1) \theta/2}{\sin (N \theta/2) \over \sin (\theta /2)}$$
Now, we can combine the expressions for $$S^+_N(\theta)$$ and $$S^-_N(\theta)$$ to get
$$S_N(\theta) = {\sin (N \theta/2) \over \sin (\theta /2)} \cdot {1 \over 2}(e^{j (N-1) \theta/2} + e^{-j (N-1) \theta/2})$$
Using Euler's formula one more time gives
$$S_N(\theta) = {\sin (N \theta/2) \over \sin (\theta /2)} \cos \left( {(N-1)\theta \over 2}\right)$$, a nice closed form expression!
Extensions
A slight variant of the above analysis can be used to show that
$$\tilde{S}_N(\theta) = \sum_{n=0}^{N-1} \sin(n \theta) = {\sin (N \theta/2) \over \sin (\theta /2)} \sin \left( {(N-1)\theta \over 2}\right)$$
The same analysis can also be used to compute the sum $$\sum_{n=0}^{N-1} \cos(n \theta + \phi)$$, albeit at the expense of more cumbersome algebra.
Finally, the analysis can be extended to compute the summations $$\sum_{n=0}^{N-1} n \cos(n \theta)$$ and $$\sum_{n=0}^{N-1} n \sin(n \theta)$$. Simply differentiating the expressions for $$S_N(\theta)$$ and $$\tilde{S}_N(\theta)$$ gives:
$${\partial \over \partial \theta} S_N(\theta) = -\sum_{n=0}^{N-1} n \sin(n \theta) = {\partial \over \partial \theta} \left[ {\sin (N \theta/2) \over \sin (\theta /2)} \cos \left( {(N-1)\theta \over 2}\right) \right]$$
$${\partial \over \partial \theta} \tilde{S}_N(\theta) = \sum_{n=0}^{N-1} n \cos(n \theta) = {\partial \over \partial \theta} \left[ {\sin (N \theta/2) \over \sin (\theta /2)} \sin \left( {(N-1)\theta \over 2}\right) \right]$$
We are not going to go through the algebra here, but the above approach gives a clear path to the computation of linearly weighted sums of cosines and sines.
Here to share a few nuggets of my math wisdom with the world and to learn from you!