TITLE: How can I find the value of this [pathological] function?
QUESTION [5 upvotes]: A few months ago, while attempting to create a parameterization of the Hilbert curve, I discovered an interesting function, given by the summation...
$$f(x)=\sum_{n=1}^\infty \frac{\text{sgn}\left(\cos(nx)\right)}{n}$$
...where $\text{sgn}$ is the signum function...
$$\text{sgn}(x)=\begin{cases}1&x>0\\0&x=0\\-1&x<0\end{cases}$$
The function is noteworthy for being nowhere differentiable and nowhere continuous. I have found it nigh impossible to evaluate the function for any $x$ save integer multiples of $2\pi$ (where the sum diverges).
The graph of the function (shown below) seems to be a self-similar fractal, but I don't know enough about fractals to verify this. Even if I did, I'm not sure what I would do with this information.

Anyway, how can I find the value of $f(x)$ for arbitrary $x$?

REPLY [3 votes]: This is more like an extended comment. From the Fourier series
$$ \operatorname{sign}(\cos(x)) = \frac{\pi}{4}\sum_{k=0}^{\infty} \frac{(-1)^k}{2k+1} \cos((2k+1)x), $$
we heuristically compute that
\begin{align*}
f(x)
&= \frac{4}{\pi}\sum_{n=1}^{\infty}\sum_{k=0}^{\infty} \frac{(-1)^k}{n(2k+1)}\cos(n(2k+1)x) \\
&= \frac{1}{\pi} \sum_{l=1}^{\infty} \frac{r_2(l)}{l} \cos(lx) \\
&= \frac{1}{\pi} \sum_{\mathrm{k} \in \mathbb{Z}^2 \setminus\{0\}} \frac{\cos(|\mathrm{k}|x)}{|\mathrm{k}|},
\end{align*}
where $r_2(\cdot)$ is the sum of squares functions and the second step follows by rearranging the sum according to the value of $l = n(2k+1)$, although establishing the validity of such manipulation would be a non-trivial task.
The following is the plot of of the partial sum $\frac{1}{\pi} \sum_{l=1}^{1000} \frac{r_2(l)}{l} \cos(lx)$ evaluated at points of the form $x = \frac{k \pi}{1025}$ for $|k| \leq 1025$, replicating OP's figure.