In this paper, the sinusoidal error compensation method based on particle swarm optimization (PSO) is researched to reduce the subdivision precision loss caused by sinusoidal error in a grating moiré signal. On the basis of the principle of arctangent subdivision of a grating moiré signal, the subdivision error caused by sinusoidal error is quantitatively analysed. In view of the sinusoidal error compensation method, the signal compensation method involves complex calculations and occupies many resources; therefore, a sinusoidal error angle compensation method is proposed. The principle of the sinusoidal error angle compensation method based on the PSO algorithm is explained in detail. Aiming at the large calculation amount of the PSO algorithm, the appropriate PSO parameters are selected experimentally, and the complexity of the PSO algorithm is minimized while ensuring the fitting accuracy. The parameters of the signal waveform equation are solved using the PSO algorithm on the field-programmable gate array (FPGA) platform. According to the parameters solved, a lookup table for sinusoidal error compensation is designed. The grating system platform is built to verify the effect of the compensation scheme on the FPGA platform. The results show that the compensation method can effectively reduce the sinusoidal error component in the signal, and the subdivision error is reduced from 0.95'' to 0.56'' in the time domain. Consequently, the zero-order, first-order, second-order and third-order components are considerably suppressed. The compensation scheme can realize the sinusoidal error compensation of the grating moiré signal and effectively improve the measurement accuracy of the grating encoder.