Numerical simulation and inversion imaging are essential in geophysics exploration. Fourier transform plays a vital role in geophysical numerical simulation and inversion imaging, especially in solving partial differential equations. This paper proposes an arbitrary sampling Fourier transform algorithm (AS-FT) based on quadratic interpolation of shape function. Its core idea is to discretize the Fourier transform integral into the sum of finite element integrals. The quadratic shape function represents the function change in each element, and then all element integrals are calculated and accumulated. In this way, the semi-analytical solution of the Fourier oscillation operator in each integral interval can be obtained, and the Fourier transform coefficient can be calculated in advance, so the algorithm has high calculation accuracy and efficiency. Based on the one-dimensional (1D) transform, the two-dimensional (2D) transform is realized by integrating the 1D Fourier transform twice, and the three-dimensional (3D) transform is realized by integrating the 1D Fourier transform three times. The algorithm can sample flexibly according to the distribution of integrated values. The correctness and efficiency of the algorithm are verified by Fourier transform pairs. The AS-FT algorithm is applied to the numerical simulation of magnetic anomalies. The accuracy and efficiency are compared with the standard Fast Fourier transform (standard-FFT) and Gauss Fast Fourier transform (Gauss-FFT). It shows that the AS-FT algorithm has no edge effects and has a higher computational speed. The AS-FT algorithm has good adaptability to continuous medium, weak magnetic catastrophe medium, and strong magnetic catastrophe medium. It can achieve the same as or even higher accuracy than Gauss-FFT through fewer sampling points. The AS-FT algorithm provides a new means for partial differential equation solution in geophysics. It successfully solves the boundary problems, which makes it an efficient and high-precision Fourier transform approach with promising applications. Therefore, the AS-FT algorithm has excellent advantages in solving partial differential equations, providing a new means for solving geophysical forward and inverse problems.