When a tamping machine is tamping track ballasts under railway, it is necessary to determine the tamping positions in advance. This study proposes a peak detection algorithm to locate rail spikes with magnetic sensor signals, and then determine the tamping positions. In this algorithm, we have performed the downsampling, sliding window, threshold classification, and secondary peak search to complete peak detection. Especially, we discuss how a sliding window length, a downsampling frequency, a secondary-search-window length, and other parameters affect the performance of the algorithm. In experiments, we use a group of real magnetic sensor signals to evaluate the algorithm. Compared with traditional methods, the proposed algorithm can reduce the false positives and misses of peak detection to 0, while the maximum location error will not more than 1 cm.