Optimized the period detection function for low frequencies.