A frequency-dependent p-adaptive technique for spectral methods