this method cames from a manuscript published on Med. Phys. 33 (10), Oct 2006. peak and valley detection in free-breathing respiratory waveforms.