a device for treating sleep apnea by discriminating between successive sleep stages of a patient includes a generator configured to produce stimulation pulses , a stimulator that receives the stimulation pulses produced by the generator and delivers stimulation to the patient , a sensor configured to measure a biological parameter of the patient , and a controller . the controller is configured to determine a state of the patient based on the biological parameter , perform a sleep analysis based on the state of the patient , activate the generator to trigger production of the stimulation pulses , determine a variation of the biological parameter subsequent to the production of the stimulation pulses , determine a response of the patient to the stimulation pulses according to the variation of the biological parameter , and determine a sleep stage of the patient based on the response .