Skip to content
Brian Chang
All projects

Case study · ML + iOS

Coughie

An iOS app that monitors respiratory health while you sleep by detecting and classifying coughs.

TL;DR

Doctors rely on what patients tell them, but nobody can describe coughs that happen while they're asleep. As ML Engineer and iOS Developer, I built Coughie: an iPhone app that listens overnight, filters out background noise, and classifies each cough as dry or wet, all on the device.

Role
ML Engineer, iOS Developer
Platform
iOS
Team
5 people
Duration
1 month
  • SwiftUI
  • AVFoundation
  • SoundAnalysis
  • Core ML
  • ActivityKit
  • HealthKit

Problem

A diagnosis doesn't come from the physical examination alone. Doctors also lean on anamnesis, the patient's own account of their symptoms. The trouble is that patients often struggle to describe what they've experienced, and coughing at night is the hardest case of all. The patient is asleep, so they can't say how often they coughed or what it sounded like.

Research

We interviewed several doctors to understand how they reach a diagnosis and where that process breaks down.

  • Anamnesis matters as much as the examination. What the patient reports shapes how the doctor reads everything else.
  • Patients are unreliable narrators of their own symptoms, not on purpose, but because they don't notice or don't remember.
  • Night coughs are the blind spot. They happen while the patient is asleep, so that part of the story simply never reaches the doctor.

Insight

  • The doctor needs a record, not a recollection. If the phone can listen while the patient sleeps, the anamnesis no longer depends on memory.
  • Count and type both matter. How often someone coughs, and whether the coughs are dry or wet, tell the doctor different things.

Decisions & trade-offs

Detect a cough before classifying it

Context
Our first plan was simple: feed the audio straight into a model that labels each cough as dry or wet. But Coughie listens to a live stream all night, and most of what it hears isn't a cough at all.
Options
  • Run the dry/wet classifier directly on the audio stream
  • First filter for cough sounds, then classify only the coughs (chosen)
Why

A dry/wet classifier always answers dry or wet, even when the sound is someone talking, a door closing, or a snore. On a stream, that turns every noise into a fake cough. So we added a step in front: Apple's built-in sound classifier checks whether a sound is a cough and not speech, and only then does our own model decide whether it's dry or wet.

A noise gate that adapts to the room

Context
Bedrooms differ: some are near-silent, others have a fan, AC, or traffic. A fixed loudness threshold either misses quiet coughs in a silent room or keeps triggering the models in a noisy one.
Options
  • A fixed loudness threshold
  • A threshold that learns the room's background noise (chosen)
Why

Coughie keeps learning the room's noise floor from the quiet moments in the stream, and only sounds clearly louder than that background reach the models. In a silent room the gate drops so soft coughs still count. In a noisy room it rises, so the models aren't called over and over for sounds that are just the room. That keeps detection focused on coughs and saves battery over a whole night.

Solution

Coughie runs a three-stage pipeline on the iPhone, so bedroom audio never leaves the device:

  1. Adaptive noise gate. Audio is cut into short windows, and quiet ones are used to keep updating the room's background level. Only windows louder than that pass.
  2. Cough filter. Apple's SoundAnalysis classifier screens what's left, keeping coughs and dropping speech.
  3. Dry or wet. Our own CNN reads a log-mel spectrogram of the sound and labels the cough dry or wet. Windows close together are merged into one cough event, and a short clip is saved so it can be played back.

Around the pipeline, Live Activities and the Dynamic Island show that monitoring is running through the night. In the morning, the app shows a summary of the night and a timeline of when coughs peaked. Cough data is written to Apple Health and can be exported as a PDF report to bring to the doctor.

Monitor screen with a session timer, live acoustic feed, and a list of recent detections
Monitoring overnight: a live acoustic feed and each detection as it happens.
Morning summary showing 12 total coughs, 8 dry and 4 wet
The morning summary: how many coughs, and how many were dry or wet.
Session analysis with a nightly activity chart, key insights, and a list of detected events
When the coughs peaked during the night, with every event listed.

Results

  • A working on-device pipeline that goes from a live microphone stream to dry and wet cough events.
  • The detection step and adaptive gate keep the classifier from firing on background noise.
  • Cough records sync to Apple Health and export as a PDF report for the doctor.

Reflection

Coughie taught me that a model is only as good as the system around it. On paper, a dry-versus-wet classifier was the whole product. In a real bedroom, most of the work was deciding what the model should never see: silence, talking, a fan, the room itself. Designing the gate and the cough filter mattered as much as training the classifier. Starting from the doctors also kept us honest about what we were building. The goal wasn't a clever model, but a reliable record of the nights patients can't describe themselves.