A noise elimination method based on an improved particle swarm algorithm is applied to direct absorption spectroscopy. The algorithm combines the theory of spectral line shape to calculate a fitness function according to the original spectra. Comparing the particles and the fitness function to calculate the updating direction, and position of particles, the iterative update finally finds the optimal solution. The algorithm is applied to direct absorption spectroscopy to measure methane; compared with the signal without algorithm processing, the signal-to-noise ratio (SNR) is improved by 4.17 times, and the minimum detection limit in the experiment is 15.3 ppb. R2 = 0.9999 is calculated in the calibration experiment, and the error is less than 0.1 ppm in the repeatability experiment of constant methane at 2 ppm concentration.