Detecting inhale and exhale

Discussion in 'Module Connector (MC)' started by Shane, Oct 4, 2018.

  1. Shane

    Shane New Member


    In the XeThruExplorer (and X2M200 data sheet), I noticed you can set the LED control to Inhale, in which case the LED remains off except when the sensor has a good lock on inhale and exhale. It then turns blue when a person is inhaling and yellow when exhaling.

    I’d like to replicate this behavior using the python ModuleConnector, except instead of controlling an LED, I’d like to simply print “exhaling” or “inhaling” to the screen. I’ve provided my first attempt at how I might do this below.

    Two questions:
    1. Does anyone know the actual algorithm/calculation that the XeThru uses to decide when to change the LED to blue/yellow when the LED is in Inhale mode?
    2. If not, is my “first attempt” below generally on the right track or way off? Any recommendations on how to improve it, or suggestions for alternative algorithms?

    I still need to work out the details, but here’s my first attempt at a high-level algorithm:

    A) Getting a good lock on inhale and exhale: Keep a history of movement measurements (from the respiration data) collected when the sensor state is “Breathing”. If there’s a regular pattern of the measurements increasing and decreasing, then we have a good lock. E.g. increasing, on average, for about X frames, then decreasing, on average, for about X frames, and this pattern repeats a few times.

    B) Detecting inhale and exhale: Once we have a good lock, as soon as the movement measurements have been increasing, on average, for Y frames, then the target person is inhaling. As soon as the movement measurements have been decreasing, on average, for Y frames, then the target is exhaling.

    Additional Info:

    Hardware: XeThru X2M200
    Software: Python ModuleConnector v1.5.3
    Configuration: Respiration profile

  2. Charlie Shao

    Charlie Shao Moderator Staff Member

    Hi Shane,
    1)I checked the X2M200 datasheet, here is the definition of LED Full mode for breathing state:
    Breathing: Green on/off flashing 50% duty cycle. Period = RPM (no breathing pattern, RPM =15
    means LED flashes 15 times/minute)
    so there is no "inhale" and "exhale" time, just 50% duty cycle at respiration frequency.
    2) Your second option sounds more reasonable, you need to extract "inhale" and "exhale" information from BREATHING PATTERN, which is basically the respiration movement.
  3. Shane

    Shane New Member

    Thanks for your reply. We are using the respiration movement to detect “exhaling”, but what we have so far is a very simple approximation and we’d like to improve its accuracy.

    Here’s a python snippet that’s similar to the actual code we’re using to detect exhaling:

    last_movement = 0.0

    while True:
    Not shown here: it seems the XeThru sends data faster than we
    can read it, so we’re calling x2m200.clear(“resp_status”) every
    N frames so we’re regularly reading fresh data.
    rdata = x2m200.get_respiration_data('resp_status')
    if rdata.sensor_state == XTS_VAL_RESP_STATE_BREATHING:
    delta = float(rdata.movement) - float(last_movement)
    last_movement = float(rdata.movement)

    if rdata.movement > THRESHOLD and delta < 0:

    In other words: Anytime movement is above a certain threshold, we assume we're nearing the peak of the inhale and are about to exhale. So the moment the movement measurement begins to decrease, we assume this is the beginning of the exhale.

    Even though this is a simple/inaccurate approximation, it's seemingly able to detect exhale’s under limited conditions. But it lags behind the actual exhale by about half a second.

    We’d like to implement something more accurate and robust, but aren’t sure how. We tried implementing the algorithm described in B) in my original post, but the above calculation actually worked better.

    Any suggestions would be greatly appreciated.