Learn · 01 of 02
How it works
From a heart sound to a decision, in two halves.
Hardware · The circuit
From two microphones to the PCM1808 input.
Circuit design and analysis by Patrick De Leo.
Design and analysis · simulated, awaiting parts
FIG. 1 · The whole front end · microphones to the PCM1808 input
Scrolls sideways
Stage 1 · Input stage
Microphones and input coupling
Each microphone's output is a voltage on a node (VMNC and VMH) that RL ties to the supply +Vs. The capacitor CL passes the signal on to nodes A and D, where Rin goes to ground.
CL, RL and Rin together form a first-order high-pass filter. A high-pass filter passes frequencies above its cutoff and weakens those below it. The analysis derives the cutoff fc and sets it to 10 Hz, which fixes Rin.
FIG. 2 · Input stage · circuit
As designed
Design targets
- Zout = ro || RL, ro >> RL, so Zout ~ RL
- 1 kΩ p.1
- Vs (DC)
- 4.5 to 10 V p.1
- Input coupling cutoff fc
- 10 Hz p.2
Components
| Component | Value | Role in the circuit | Page |
|---|---|---|---|
| S1.RL_NCRL (MNC) | 1 kΩ | Load resistor from the MNC capsule output (node VMNC) to +Vs | p.1 |
| S1.RL_HRL (MH) | 1 kΩ | Load resistor from the MH capsule output (node VMH) to +Vs | p.1 |
| S1.CL_NCCL (MNC) | 1 µF | Coupling capacitor from node VMNC to node A | p.1 |
| S1.CL_HCL (MH) | 1 µF | Coupling capacitor from node VMH to node D | p.1 |
| S1.Rin_NCRin (MNC) | 14915.49 Ω (about 15 kΩ) | Resistor from node A to ground; fc = 10 Hz | p.2 |
| S1.Rin_HRin (MH) | 14915.49 Ω (about 15 kΩ) | Resistor from node D to ground; fc = 10 Hz | p.2 |
Parts
- S1.M_NCElectret microphone, voltage VMNC
- CMB-6544PF p.1
- S1.M_HElectret microphone, voltage VMH
- CMB-6544PF p.1
Derivation
On a narrow screen, scroll an equation sideways to see all of it.
The page is headed “Circuits – Phonocardiogram”. Microphone: CMB-6544PF.
voltage divider
Since this is a HPF, we take the limit to find max power.
The dB is half power and is power @ .
FIG. 3 · input coupling · gain against frequency
1–2000 Hz
Point at the chart, or focus it and use the arrow keys, to read the gain at one frequency.
Stage 2 · Instrumentation amplifier
Instrumentation amplifier
R1, R2 and R3 set the gain of the first pair, Av1 = 1 + 2R/R1, which the analysis sets to 201. M3, with R4 = R5 = R6 = R7, subtracts Vo1 from Vo2, so the output is Vo = (1 + 2R/R1)(VC − VB).
FIG. 4 · Instrumentation amplifier · circuit
As designed
Design targets
- Av1 = 1 + 2R/R1
- 201 V/V p.3
Components
| Component | Value | Role in the circuit | Page |
|---|---|---|---|
| S2.R1R1 | 100 Ω | Gain resistor between nodes B and C | p.3 |
| S2.R2R2 | 10 kΩ | Feedback resistor, node B to Vo1 (R = R2 = R3) | p.3 |
| S2.R3R3 | 10 kΩ | Feedback resistor, node C to Vo2 (R = R2 = R3) | p.3 |
| S2.R4R4 | R4 = R5 = R6 = R7 | Vo1 to the inverting input of M3 | p.4 |
| S2.R5R5 | R4 = R5 = R6 = R7 | Vo2 to the non-inverting input of M3 | p.4 |
| S2.R6R6 | R4 = R5 = R6 = R7 | Feedback resistor of M3, output to the inverting input | p.4 |
| S2.R7R7 | R4 = R5 = R6 = R7 | Non-inverting input of M3 to ground | p.4 |
Parts
- S2.M1In-amp input op-amp, + input at node A (labelled M1 on p.4)
- OPA2141 or OPA4141 p.9
- S2.M2In-amp input op-amp, + input at node D (labelled M2 on p.4)
- OPA2141 or OPA4141 p.9
- S2.M3In-amp difference amplifier (labelled M3 on p.4)
- OPA2141 or OPA4141 p.9
Derivation
On a narrow screen, scroll an equation sideways to see all of it.
Let and . .
For
If :
FIG. 5 · instrumentation amplifier · gain against frequency
1–2000 Hz
Point at the chart, or focus it and use the arrow keys, to read the gain at one frequency.
Stage 3a · High-pass filter
Sallen-Key high-pass
The analysis sets the corner f0 to 20 Hz and Q to 0.707, takes C1 = C2 = 1 µF, and solves for R1 and R2.
FIG. 6 · High-pass filter · circuit
As designed
Design targets
- High-pass f0
- 20 Hz p.5
- High-pass Q (Butterworth)
- 0.707 p.5
Components
| Component | Value | Role in the circuit | Page |
|---|---|---|---|
| S3a.C1C1 | 1 µF | Input capacitor, Vi to node A | p.5 |
| S3a.C2C2 | 1 µF | Capacitor from node A to node B | p.5 |
| S3a.R1R1 | 5.628 kΩ | Feedback resistor, node A to Vo | p.5 |
| S3a.R2R2 | 11.252 kΩ | Resistor from node B to ground | p.5 |
Parts
- S3a.U1High-pass Sallen-Key op-amp
- OPA4141 or OPA4197 p.9
Derivation
On a narrow screen, scroll an equation sideways to see all of it.
Let &
Assume:
FIG. 7 · high-pass filter · gain against frequency
1–2000 Hz
Point at the chart, or focus it and use the arrow keys, to read the gain at one frequency.
Stage 3b · Low-pass filter
Sallen-Key low-pass
A low-pass filter passes frequencies below its corner and weakens those above it. With R1 = R2 and C1 = C2/2, the analysis sets f0 to 500 Hz and solves for R.
FIG. 8 · Low-pass filter · circuit
As designed
Design targets
- Low-pass f0
- 500 Hz p.6
- Low-pass Q = 1/sqrt(2)
- 0.7071 p.6
Components
| Component | Value | Role in the circuit | Page |
|---|---|---|---|
| S3b.R1R1 | 22.508 kΩ | Series resistor, Vi to node A (R1 = R2) | p.6 |
| S3b.R2R2 | 22.508 kΩ | Series resistor, node A to node B (R1 = R2) | p.6 |
| S3b.C1C1 | 10 nF | Capacitor from node B to ground | p.6 |
| S3b.C2C2 | 20 nF | Feedback capacitor, node A to Vo | p.6 |
Parts
- S3b.U1Low-pass Sallen-Key op-amp
- OPA4141 or OPA4197 p.9
Derivation
On a narrow screen, scroll an equation sideways to see all of it.
Let &
Assuming
FIG. 9 · low-pass filter · gain against frequency
1–2000 Hz
Point at the chart, or focus it and use the arrow keys, to read the gain at one frequency.
Stage 4 · Notch filter
Notch filter
A notch filter weakens a narrow range of frequencies around its centre and passes the rest. The analysis gives its resistor and capacitor values, with f = 60 Hz, Q = 1 and a gain of 1.
FIG. 10 · Notch filter · circuit
As designed
Design targets
- Notch frequency f
- 60 Hz p.7
- Notch Q
- 1 p.7
- Notch gain
- 1 V/V p.7
Components
| Component | Value | Role in the circuit | Page |
|---|---|---|---|
| S4.R1R1 | 12.1 kΩ | Input node to the R1/R2 junction | p.7 |
| S4.R2R2 | 12.1 kΩ | R1/R2 junction to ground | p.7 |
| S4.R3R3 | 10 kΩ | Input node to the inverting input of the second op-amp | p.7 |
| S4.R4R4 | 24.3 kΩ | Across the first op-amp, inverting input to output | p.7 |
| S4.R5R5 | 10 kΩ | Feedback resistor of the second op-amp | p.7 |
| S4.R6R6 | 10 kΩ | First op-amp output to the inverting input of the second op-amp | p.7 |
| S4.C1C1 | 0.22 µF | C1/C2 junction to the inverting input of the first op-amp | p.7 |
| S4.C2C2 | 0.22 µF | C1/C2 junction to the output of the first op-amp | p.7 |
Parts
- S4.U1Notch, first op-amp
- OPA4141 or OPA4197 p.9
- S4.U2Notch, second op-amp
- OPA4141 or OPA4197 p.9
Derivation
On a narrow screen, scroll an equation sideways to see all of it.
FIG. 11 · notch filter · gain against frequency
1–2000 Hz
Point at the chart, or focus it and use the arrow keys, to read the gain at one frequency.
Stage 5 · Variable gain and ADC
Variable gain and the ADC input
With Rin = 1 kΩ and the potentiometer Rf between 0 and 100 kΩ, the gain Av = 1 + Rf/Rin runs from 1 to 101. CADC couples the output to the PCM1808 analog input, and RBias ties that input to the PCM1808's Vref.
| Rf | Av (V/V) | Gain of this stage (dB) |
|---|---|---|
| 0 Ω | 1 | 0.0 |
| 10 kΩ | 11 | 20.8 |
| 50 kΩ | 51 | 34.2 |
| 100 kΩ | 101 | 40.1 |
FIG. 12 · Variable gain and ADC · circuit
As designed
Design targets
- Av = 1 + Rf/Rin
- 1 to 101 V/V p.8
- Rf
- 0 to 100 kΩ p.8
Components
| Component | Value | Role in the circuit | Page |
|---|---|---|---|
| S5.RinRin | 1 kΩ | Node A (inverting input) to ground | p.8 |
| S5.RfRf | 0 to 100 kΩ | Feedback potentiometer, output to node A; Rf = [0, 100k] | p.8 |
| ADC.C_ADCCADC | 1 µF | Coupling capacitor from the variable gain output to the PCM1808 analog input | p.8, p.9 |
| ADC.R_BiasRBias | 100 kΩ | Resistor from the PCM1808 analog input to the PCM1808 Vref | p.8, p.9 |
Parts
- S5.U1Variable gain op-amp
- OPA2141 or OPA4141 p.9
- ADC.PCM1808ADC; analog input pin and Vref
- PCM1808 p.8, p.9
Derivation
On a narrow screen, scroll an equation sideways to see all of it.
Let
ADC
PCM1808.
FIG. 13 · ADC coupling · gain against frequency
1–2000 Hz
Point at the chart, or focus it and use the arrow keys, to read the gain at one frequency.
Whole chain · Response
The gain of the whole chain.
FIG. 14 · Whole chain · gain against frequency
1–2000 Hz
Point at the chart, or focus it and use the arrow keys, to read the gain at one frequency.
Show the numbers as a table
| Frequency | Input coupling | Instrumentation amplifier | High-pass | Low-pass | Notch | ADC coupling | Whole chain, Av = 1 | Whole chain, Av = 11 | Whole chain, Av = 51 | Whole chain, Av = 101 |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 Hz | -20.6 | 46.1 | -52.0 | 0.0 | 0.0 | -5.5 | -32.1 | -11.2 | 2.1 | 8.0 |
| 5 Hz | -7.6 | 46.1 | -24.1 | 0.0 | 0.0 | -0.4 | 14.0 | 34.8 | 48.1 | 54.0 |
| 10 Hz | -3.6 | 46.1 | -12.3 | 0.0 | -0.1 | -0.1 | 29.9 | 50.8 | 64.1 | 70.0 |
| 20 Hz | -1.5 | 46.1 | -3.0 | 0.0 | -0.6 | 0.0 | 40.9 | 61.7 | 75.1 | 81.0 |
| 60 Hz | -0.7 | 46.1 | -0.1 | 0.0 | -33.4 | 0.0 | 11.9 | 32.7 | 46.0 | 52.0 |
| 100 Hz | -0.6 | 46.1 | 0.0 | 0.0 | -2.7 | 0.0 | 42.8 | 63.6 | 76.9 | 82.8 |
| 500 Hz | -0.6 | 46.1 | 0.0 | -3.0 | -0.1 | 0.0 | 42.4 | 63.3 | 76.6 | 82.5 |
| 600 Hz | -0.6 | 46.1 | 0.0 | -4.9 | 0.0 | 0.0 | 40.6 | 61.4 | 74.7 | 80.7 |
| 1000 Hz | -0.6 | 46.1 | 0.0 | -12.3 | 0.0 | 0.0 | 33.2 | 54.0 | 67.3 | 73.3 |
| 2000 Hz | -0.6 | 46.1 | 0.0 | -24.1 | 0.0 | 0.0 | 21.4 | 42.3 | 55.6 | 61.5 |
Parts · Components and power
Components and power.
Parts by stage (analysis p.9):
| Stage | Part | Function | |
|---|---|---|---|
| Stage 2 | OPA2141 or OPA4141 | In Amp | |
| Stage 3 | OPA4141 or OPA4197 | BP Filter | |
| Stage 4 | Same as Stage 3 | T-Notch Filter | |
| Stage 5 | Same as Stage 2 | Variable Gain |
ADC: PCM1808.
To produce the inverted rail add low noise charge pump inverter: TC7660H or LM27762.
Component values as given in the analysis:
| Component | Value | Page |
|---|---|---|
| Stage 1 · Input stage | ||
| S1.RL_NCRL (MNC) | 1 kΩ | p.1 |
| S1.RL_HRL (MH) | 1 kΩ | p.1 |
| S1.CL_NCCL (MNC) | 1 µF | p.1 |
| S1.CL_HCL (MH) | 1 µF | p.1 |
| S1.Rin_NCRin (MNC) | 14915.49 Ω (about 15 kΩ) | p.2 |
| S1.Rin_HRin (MH) | 14915.49 Ω (about 15 kΩ) | p.2 |
| Stage 2 · Instrumentation amplifier | ||
| S2.R1R1 | 100 Ω | p.3 |
| S2.R2R2 | 10 kΩ | p.3 |
| S2.R3R3 | 10 kΩ | p.3 |
| S2.R4R4 | R4 = R5 = R6 = R7 | p.4 |
| S2.R5R5 | R4 = R5 = R6 = R7 | p.4 |
| S2.R6R6 | R4 = R5 = R6 = R7 | p.4 |
| S2.R7R7 | R4 = R5 = R6 = R7 | p.4 |
| Stage 3 · Band-pass filter (high-pass and low-pass Sallen-Key) | ||
| S3a.C1C1 | 1 µF | p.5 |
| S3a.C2C2 | 1 µF | p.5 |
| S3a.R1R1 | 5.628 kΩ | p.5 |
| S3a.R2R2 | 11.252 kΩ | p.5 |
| S3b.R1R1 | 22.508 kΩ | p.6 |
| S3b.R2R2 | 22.508 kΩ | p.6 |
| S3b.C1C1 | 10 nF | p.6 |
| S3b.C2C2 | 20 nF | p.6 |
| Stage 4 · Notch filter | ||
| S4.R1R1 | 12.1 kΩ | p.7 |
| S4.R2R2 | 12.1 kΩ | p.7 |
| S4.R3R3 | 10 kΩ | p.7 |
| S4.R4R4 | 24.3 kΩ | p.7 |
| S4.R5R5 | 10 kΩ | p.7 |
| S4.R6R6 | 10 kΩ | p.7 |
| S4.C1C1 | 0.22 µF | p.7 |
| S4.C2C2 | 0.22 µF | p.7 |
| Stage 5 · Variable gain stage | ||
| S5.RinRin | 1 kΩ | p.8 |
| S5.RfRf | 0 to 100 kΩ | p.8 |
| Stage ADC · ADC interface | ||
| ADC.C_ADCCADC | 1 µF | p.8, p.9 |
| ADC.R_BiasRBias | 100 kΩ | p.8, p.9 |
01 · The software at a glance
From samples to a decision.
Software design and implementation by Luca Mazzo.
FIG. 1 · The software, step by step
Runs on the Pi
- 01Recordinga file now, the microphone later
- 025 s windowsseveral heartbeats each
- 03Keep 20–500 Hzthe heart-sound band
- 04MFCC featuresa summary of the spectrum
- 05Classifiera random forest (scikit-learn)
- 06Present or absentwith a confidence
- Answer
- murmur present or absent
- Segmentation
- implicit: 5 s windows, no S1/S2 finder
- Features
- MFCC, mean and standard deviation per window
- Model
- random forest, scikit-learn
- Runs on
- the Pi's CPU, no cloud, no network calls
02 · Acquisition
Samples in: a file or the ADC.
Everything starts as a stream of samples. In review mode the software reads a recording from a .wav file (WavFileSource). In live mode it reads blocks of samples from aBlockSource. On the Pi, AlsaCaptureSource (pcg/capture.py) reads the PCM1808 through the Pi's I2S input with arecord at 48 kHz and decimates it to 4 kHz. FileReplaySource replays .wav files in real time, so the live display can also run with no hardware attached.
The recordings are from the CirCor DigiScope dataset, stored as .wav files at 4 kHz. The capture source and the file replay implement the same BlockSource interface, so nothing above it changes. If the ADC is missing or delivers no signal, the display says so and nothing is classified.
File replay · runs today
Capture from the ADC · software written and tested, not yet run against the wired ADC
03 · Windows, band-limit, normalise
Five-second windows, one band, one loudness.
The samples are cut into 5 s windows, a new window every 2.5 s: long enough to hold several heartbeats. There is no S1/S2 finder. The model sees the repeating pattern of beats inside the window, so nobody has to find the heart sounds for it first.
Each window is then band-limited to 20–500 Hz and divided by its own
FIG. 2 · 9979_TV.wav · tricuspid · one 5 s window
Point at the plot, or focus it and use the arrow keys (Shift for 100 ms steps).
04 · MFCC features
A window becomes a short list of numbers.
From each window the software computes
A window becomes 26 numbers: for each of the 13 coefficients, its mean and its standard deviation over the window. The frames are 32 ms long, 16 ms apart. One piece of code, pcg/features.py, makes these numbers for training, for the offline baseline and for the live view.
05 · The classifier
Numbers in, a vote share out.
A random forest (scikit-learn) takes each window's 26 numbers and decides. Every tree in the forest votes "murmur" or "no murmur", and the share of trees voting murmur is the window's score; the confidence shown is the share that agrees with the chosen label. A recording's decision is the mean of its windows' values compared with one threshold, 0.32, fixed from the training data and never tuned for the live result.
It was chosen by cross-validation grouped by child, so a child never appears on both sides of a split. A random forest runs cheaply on the Pi and is easy to explain: the trees vote and the votes are counted. How it was trained and how well it works are on the Data & AI page.
06 · The HMI layers
Small layers, fixed interfaces.
FIG. 3 · The software layers
Data flows downward
Sources
pcg/sources.py, pcg/stream.py, pcg/capture.py
Where samples come from: a .wav file, a file replay, or the PCM1808 ADC through AlsaCaptureSource, all behind the same BlockSource interface.
Features
pcg/features.py
The only feature path: windows, band-limit to 20–500 Hz, divide by loudness, MFCCs, then the mean and standard deviation of each per window.
Model
pcg/model.py, pcg/classify.py
A trained random forest, whose share of trees voting murmur is the window's score (confidence is the share agreeing with the chosen label), and the rule that turns window results into one decision per recording.
JSON API
hmi/app.py
A small Flask server on the Pi. It calls the layers above and sends the results as JSON; it does no signal maths of its own.
Controller and renderer
hmi/static/hmi.js, hmi/static/plot.js
The browser side: the controller fetches and keeps the page state, the renderer only draws the waveform on a canvas.
On the Raspberry Pi
The instrument's own display. The Flask server also serves the monitor page, so it works with no internet connection. In the browser a controller (hmi.js) fetches a window of samples and fills in the readouts, and the renderer (plot.js) draws the waveform on a canvas. plot.js has not changed since the midterm.
This public site
A static site built from the same exported data, drawing its traces with the same unchanged plot.js. Its Live monitor link opens the Pi's own display at live.eecsyorku.app; when the Pi is off or cannot be reached, a page at /pi-offline says so plainly.
A swappable source
Review mode reads a file. Live mode reads blocks of samples from a BlockSource. AlsaCaptureSource reads the PCM1808 on the Pi, and FileReplaySource replays .wav files in real time for demos. Both implement the same interface, so nothing above them changes.
One feature path
The same feature code serves training, the offline baseline and the live view. That keeps the comparison fair: the only thing that differs is where the audio comes from.
07 · The experiment
Same files, same model, same threshold: live against offline.
Offline baseline · software, from training
The reference number
The model's score on the held-out files, worked out in software during training. It is a number, not something the sound passes through, so it shows what the model can do on these files before any hardware is involved. Any drop in the live result comes from what the sound passes through on the way.
Through the hardware · live
The headline result
Each held-out file is played, one at a time, through a loudspeaker in a 3D-printed coupler and a cast silicone layer, picked up by the microphone, passed through the analog front end and digitised (the circuit is on the Hardware tab), then read by the Raspberry Pi and classified live. The result is compared with the offline baseline on the same files, to show how much accuracy survives the real chain. It needs the hardware, so it is not measured yet.
Simulated bandpass
A software model
A software model of a 20–500 Hz bandpass filter applied to the same files. It shows how much of any loss a filter's nominal response alone could explain. It is a model, not a measurement, and never a result through the hardware.
What is measured
- Accuracy
- The share of recordings the classifier got right.
- Sensitivity
- Of the recordings with a murmur, the share it flagged as murmur present.
- Specificity
- Of the recordings without a murmur, the share it cleared as absent.
- ROC AUC
- How well the model's score ranks murmur recordings above normal ones, whatever threshold is used. 1 is perfect and 0.5 is chance.
To tell whether a change between two results on the same files is real and not luck, the files are paired and each class is tested separately with an exact
Offline baseline · software, from training
The model's score on the held-out files.
- ROC AUC1 is perfect · 0.5 is chance
- 0.724160 recordings
- SensitivityMurmurs flagged
- 0.7195% CI 0.61–0.80 · 57 of 80
- SpecificityNo-murmur recordings cleared
- 0.6895% CI 0.57–0.77 · 54 of 80
- AccuracyShare of recordings classified right
- 0.6995% CI 0.62–0.76 · 111 of 160
| Predicted murmur | Predicted absent | |
|---|---|---|
| Murmur, 80 | 57found | 23missed |
| Absent, 80 | 26false alarm | 54cleared |
Through the hardware · live
The same files, played and classified live.
Not measured
Needs the speaker, coupler, silicone, microphone, front end and ADC.
A third run, simulated bandpass, applies a software filter to the files. It is not the hardware, so it is always labelled simulated and never merged into this comparison. Its rows are on the Data & AI page.
08 · Where to look in the code
Where to look in the code.
- pcg/sources.py, pcg/stream.py
- Where samples come from: WavFileSource, the BlockSource interface, FileReplaySource and the LiveMonitor.
- pcg/capture.py
- AlsaCaptureSource: reads the PCM1808 through the Pi's I2S input with arecord and decimates it to the model's 4 kHz.
- pcg/features.py
- The one feature path: windows, band-limit, normalise, MFCCs and their mean and standard deviation per window.
- pcg/model.py, pcg/classify.py, pcg/train.py
- The classifier, what its answer looks like, and how it is trained.
- pcg/evaluate.py
- Scores the model on the bench set, offline and through the hardware, and compares the two.
- pcg/simulate_chain.py
- The simulated bandpass: a software model of the filter.
- hmi/app.py
- The Flask server on the Pi and its JSON routes.
- hmi/static/plot.js
- The renderer: draws one window of samples. Unchanged since the midterm.
- hmi/export_web.py
- Writes the data files this site is built from.
09 · What the colours mean
Every colour means one thing, everywhere.
Teal trace
The file's own signal, and the offline baseline: the model's score in software, from training.
Slate trace
Not heard yet: the part of a recording that playback has not reached.
Murmur absent
An outlined teal circle, always with the words "Murmur absent".
Murmur present
A coral triangle with the words "Murmur present". A 45 degree hatch marks the murmur bands the dataset labels in time.
Violet, dashed
Not measured, planned, awaiting parts or an example: never a number.
Amber
Through the hardware, live: always with the words "Through the hardware" or a hardware name.