Existing background fitting of energy dispersive X-ray spectra (X-ray, electron or proton excited) algorithms (e.g. parabolas or polynomials) are not universally applicable. Various limitations exist tying each algorithm to specific spectral shapes. This project aimed to find a generic algorithm which could reliably fit background independent of X-ray spectra source and form. A frequency differentiated non-linear digital filter ''rolling ball'' was chosen as a promising possibility. The algorithm is based on a square wave with an energy variable width as the structuring function, operated on by a gray scale morphological transformation. A wide range of spectra types have been successfully fitted. This algorithm behaves equivalently to traditional polynomial backgrounds in simple spectra e.g. PIXE aerosol spectra with funny filter, and is considerably more robust for multiple overlapping peaks, rapidly varying backgrounds, and is independent of X-ray energy filter or excitation mode.