The interpolation error of an optical encoder is largely determined by the imperfections in the electrical signals, such as amplitude error, phaseshift error, DC error and higher harmonic distortion. In this paper, an optimized optical encoder based on generalized grating imaging is presented. We first describe the basic principle and configuration of the optical encoder. To achieve four electrical signals with equal amplitudes, equal DC contents and accurate 90° electrically phaseshift, a photodiodes array is adopted as the photodetector. The photodiodes array offers the advantage of singlefield scanning. Then, to solve the remaining problem of higher harmonic distortion, a slitwidthmodulated index grating is proposed to suppress the dominant third order and fifth order harmonics simultaneously, and the cell's width of the photodiodes array is optimized to further reduce the fifth order harmonic. Experiments have been carried out to verify the theoretical results. The feasibility and effectiveness of the proposed optimizing methods have been confirmed from the experimental results.