Optimal determination of frequencies in the spectral representation of stochastic processes