Objective To accurately detect ECG P wave,a P wave detection algorithm based on fitting function in the optimal interval is proposed and evaluated in this paper,for resolving very difficult problems.Such as differences of positions and figures in ECG P wave as well as easily suffered diversified interferences.Methods In this algorithm the quadratic function to fit the P wave with least square method in every interval was used,which was shifted in local range.Then the optimal fitting interval of P wave was found by comparing the error of fitting.Finally,the characteristic points of P wave were obtained with the fitting function to fit P wave in the optimal interval.Results The performance of this algorithm tested with the records of the MIT-BIH database was effective and accurate.Conclusion This algorithm on the wide range of heart rate variation and small P wave of ECG P wave detection has good effect.Also it has some capabilities of anti-interference and quite low dismissal probability in particular.