I'm writing a program that requires finding $$\text{argmax}_\theta\sum_{k=1}^na_k\cos(k\theta+b_k),$$ where $a_k$ and $b_k$ can be any real numbers. How can I do this efficiently?
2 Answers
This can be done efficiently using an eigenvalue calculation. You can transform this to a finite Fourier series by using $a_k \cos(k \theta + b_k) = a_k \cos(b_k) \cos(k \theta) - a_k \sin(b_k) \sin(k \theta)$. Then you can find all critical points, by finding the roots of the derivative. Then, one can use this answer to find all the roots (see also this paper). Finally, you just need to calculate the values at all these points, and pick the maximum.

- 21
I've decided to take the FFT, find the maximum element, and use the result as a starting point for local optimization. This gives the right answer as long as the high-frequency elements are relatively small (otherwise I can just increase the resolution of the FFT), and the runtime is $O(n\log n)$.

- 11
-
Could you please explain a bit more on how you proceed. – M.X Jul 25 '16 at 13:36
-
1@MehdiXoylu sure. Let me rewrite my formula as the sum from k=0 to N-1 of aₖcos(kθ+bₖ). Using Euler's formula this equals the real part of the sum from k=0 to N-1 of aₖe^(ibₖ)e^(kθ). This is the DFT formula with fₖ=aₖe^(ibₖ) and n=-Nθ/(2π). Now you can compute my formula by computing the real part of Fₙ. The FFT lets you compute Fₙ efficiently for many values at once. – realguy Jul 27 '16 at 05:00