pyreaper developer on retainer: np.mean(f0) includes unvoiced frames at f0=−1.0 — female speaker mean pitch 220 Hz contaminates to 136 Hz — classified as male — gender accuracy 30% across 500-episode podcast — pyreaper developer on monthly retainer
pyreaper 0.0.1 • Python 3.9; compute_epoch_and_f0() returns f0=−1.0 for unvoiced frames; np.mean(f0) includes all −1.0 values; female speaker (true voiced F0 ≈ 220 Hz; 38% unvoiced frames): contaminated mean = 0.62×220 + 0.38×(−1.0) = 136.0 Hz; gender threshold 165 Hz; 136.0 < 165 → classified as male; 500-episode women-in-tech podcast (70% female guests); gender accuracy 30% (target 95%); 3 months; fix: voiced_f0=f0[f0>0]; mean_f0=np.mean(voiced_f0) • Eastfield PyReaper Ltd. (podcast highlight reel generator; 500 episodes; 3 months; 1.5h)
Eastfield PyReaper Ltd. built an automated highlight reel generator for a women-in-tech podcast archive with 500 episodes. The generator used speaker gender classification to let podcast editors filter segments by voice type: “show only female guest segments” or “show only male host segments”. Gender was inferred from mean pitch: each speaker’s mean fundamental frequency (F0) was computed from pyreaper’s pitch tracker and compared to a 165 Hz threshold (female ≥ 165 Hz; male < 165 Hz). The mean was computed as np.mean(f0) where f0 was the full array returned by pyreaper.compute_epoch_and_f0(). This produced a contaminated mean that pulled every female speaker into the male range: for a female guest with genuine voiced F0 ≈ 220 Hz and 38% unvoiced frames (pauses, breath, fricatives, stops), the array contained 62% frames at 220 Hz and 38% frames at exactly −1.0 (the REAPER unvoiced sentinel). np.mean(f0) computed: 0.62 × 220 + 0.38 × (−1.0) = 136.4 − 0.38 = 136.0 Hz — below the 165 Hz threshold. The gender classifier labelled her as male. The podcast had 70% female guests across its 500 episodes; all 350 female-hosted episodes were misclassified. The highlight reel generator’s “female voices only” filter returned only the 30% of episodes with male guests. Editors reviewing the output found that the reel was missing every female guest and consisted entirely of the male host’s commentary. Gender accuracy measured against human labels: 30%.
The mechanism: pyreaper.compute_epoch_and_f0(x, fs, frame_period, minf0, maxf0, do_hilbert_transform) returns a 5-tuple (pm_times, pm, f0_times, f0, corr). The f0 array has length int(duration / frame_period) and contains the REAPER F0 estimate in Hz for each frame. For frames where REAPER determines the signal is unvoiced — there is no periodic component meeting the voicing-decision criterion — the f0 entry is exactly −1.0. This is the REAPER C library’s unvoiced sentinel, carried directly into the Python array. It is not np.nan, not 0.0, not −999 — it is −1.0, a valid IEEE 754 float that participates in every numpy operation without special handling. np.mean, np.median, np.std, np.histogram, and sklearn features all consume −1.0 as a data point unless the caller explicitly excludes it. For a female speaker at 220 Hz with 38% unvoiced frames, np.mean(f0) gives 136.0 Hz. For a male speaker at 117 Hz with 35% unvoiced frames, np.mean(f0) gives 0.65 × 117 + 0.35 × (−1.0) = 75.7 − 0.35 = 75.4 Hz. Both contaminated means fall below the 165 Hz threshold; both are classified as male. The accuracy of 30% came from the male episodes (150 out of 500) being correctly labelled as male by coincidence — not because the threshold worked, but because the male contaminated mean (75 Hz) also falls below 165 Hz. No female episode was ever correctly classified; all 350 female episodes were below 165 Hz due to the −1.0 contamination. The voiced-fraction required to keep a 220 Hz speaker above 165 Hz threshold after contamination: voiced_frac × 220 + (1 − voiced_frac) × (−1.0) ≥ 165 → 221 × voiced_frac ≥ 166 → voiced_frac ≥ 0.751. Natural speech voiced fraction is typically 0.55–0.70, below the 75.1% required, meaning virtually all adult female speakers are misclassified by this contaminated mean regardless of their actual pitch height.
# Eastfield PyReaper Ltd. — pyreaper 0.0.1 voiced-filter fix
# pyreaper 0.0.1; Python 3.9; NumPy 1.24
# 16 kHz mono; frame_period=0.005 (5 ms = 200 Hz frame rate)
# Podcast speaker gender classification; 500-episode archive
#
# Problem: np.mean(f0) includes unvoiced frames where f0==-1.0
# female 220 Hz voiced + 38% unvoiced at -1.0
# contaminated mean = 136.0 Hz < 165 Hz threshold → classified as male
# Fix: voiced_f0 = f0[f0 > 0]; mean_f0 = np.mean(voiced_f0)
import pyreaper
import numpy as np
import librosa
GENDER_THRESHOLD_HZ = 165.0 # female >= 165 Hz, male < 165 Hz (Titze 1994 norm)
def classify_gender_buggy(audio_path: str) -> str:
"""BUGGY: np.mean(f0) includes unvoiced sentinel -1.0 values."""
audio, fs = librosa.load(audio_path, sr=16000, mono=True)
audio_f64 = audio.astype(np.float64)
pm_times, pm, f0_times, f0, corr = pyreaper.compute_epoch_and_f0(
audio_f64, fs, frame_period=0.005,
minf0=60.0, maxf0=400.0, do_hilbert_transform=False
)
# BUG: f0 contains -1.0 for every unvoiced frame
# np.mean(-1.0 entries) pulls mean pitch below threshold
mean_f0 = np.mean(f0) # WRONG: includes -1.0 unvoiced frames
return 'female' if mean_f0 >= GENDER_THRESHOLD_HZ else 'male'
def classify_gender(audio_path: str) -> str:
"""FIXED: filter voiced frames before computing mean F0."""
audio, fs = librosa.load(audio_path, sr=16000, mono=True)
audio_f64 = audio.astype(np.float64)
pm_times, pm, f0_times, f0, corr = pyreaper.compute_epoch_and_f0(
audio_f64, fs, frame_period=0.005,
minf0=60.0, maxf0=400.0, do_hilbert_transform=False
)
# FIXED: exclude unvoiced sentinel values
voiced_f0 = f0[f0 > 0] # -1.0 unvoiced frames removed
if len(voiced_f0) == 0:
return 'unknown' # entirely unvoiced segment — cannot classify
mean_f0 = np.mean(voiced_f0) # mean over voiced frames only
return 'female' if mean_f0 >= GENDER_THRESHOLD_HZ else 'male'
def f0_statistics(f0: np.ndarray) -> dict:
"""
Safe F0 statistics: always filter voiced frames first.
Returns dict with voiced_fraction, mean_f0, median_f0, std_f0, p10, p90.
"""
voiced_f0 = f0[f0 > 0]
voiced_fraction = len(voiced_f0) / max(len(f0), 1)
if len(voiced_f0) == 0:
return {
'voiced_fraction': 0.0,
'mean_f0': None,
'median_f0': None,
'std_f0': None,
'p10_f0': None,
'p90_f0': None,
'n_voiced_frames': 0,
}
return {
'voiced_fraction': voiced_fraction,
'mean_f0': float(np.mean(voiced_f0)),
'median_f0': float(np.median(voiced_f0)),
'std_f0': float(np.std(voiced_f0)),
'p10_f0': float(np.percentile(voiced_f0, 10)),
'p90_f0': float(np.percentile(voiced_f0, 90)),
'n_voiced_frames': len(voiced_f0),
}
def validate_f0_array(f0: np.ndarray, description: str = '') -> None:
"""
Raise if f0 looks like it was computed with np.mean(f0) contamination check.
Use in tests: compute mean both ways and assert the contaminated mean
is not being used for classification.
"""
n_unvoiced = int(np.sum(f0 <= 0))
n_total = len(f0)
if n_unvoiced > 0:
contaminated_mean = float(np.mean(f0))
voiced_mean = float(np.mean(f0[f0 > 0])) if np.any(f0 > 0) else -1.0
if abs(contaminated_mean - voiced_mean) > 5.0: # more than 5 Hz contamination
raise ValueError(
f"{description}: f0 array contains {n_unvoiced}/{n_total} "
f"unvoiced frames at -1.0. "
f"np.mean(f0)={contaminated_mean:.1f} Hz differs from "
f"np.mean(voiced_f0)={voiced_mean:.1f} Hz by "
f"{abs(contaminated_mean - voiced_mean):.1f} Hz. "
f"Use f0[f0 > 0] to filter voiced frames before any statistic."
)
Why −1.0 is a silent contaminator and why np.median makes it worse than np.mean
The −1.0 unvoiced sentinel is especially insidious because it looks like a numeric value with an obvious physical interpretation (“below any real voice pitch”) and yet produces contamination at any unvoiced fraction above a critical threshold. For np.mean, the threshold is about 25% unvoiced for a 220 Hz female speaker: at 25% unvoiced, mean = 0.75 × 220 + 0.25 × (−1.0) = 164.75 Hz, just below the 165 Hz threshold. Any speech with more than 25% unvoiced frames — every real recording, since voiced fraction is typically 55–70% — will be misclassified. For np.median, the contamination is even more severe: if unvoiced fraction exceeds 50%, the median of the entire array is −1.0 (the majority value is the unvoiced sentinel). A 30-minute podcast segment with 42% unvoiced frames has median F0 = −1.0 regardless of the speaker’s actual pitch. The developer’s second attempt — switching from np.mean(f0) to np.median(f0) in an effort to reduce outlier sensitivity — made the classification worse for high-unvoiced episodes because np.median returns −1.0 directly when unvoiced frames are the majority. The correct pattern is the same for every statistic: apply the voiced filter voiced_f0 = f0[f0 > 0] first, then compute any aggregate on the filtered array. The only additional consideration is to guard against the empty-array case (fully unvoiced segments) with a check before the mean call.
Eastfield PyReaper Ltd.’s retainer engagement was 1.5h: 0.3h triage (request gender classification code; np.mean(f0) identified immediately; compute voiced_fraction on 5 test episodes: 0.57–0.65; compute contaminated mean for female speaker: 0.62 × 220 − 0.38 = 136.0 Hz vs voiced mean 220 Hz; threshold 165 Hz; classification failure confirmed); 0.3h root cause (audit all np.mean(f0) and np.median(f0) calls in codebase: 4 total — 1 in gender classifier, 2 in prosody feature pipeline, 1 in speaking-rate estimator; compute minimum unvoiced fraction needed to cause misclassification: 25% for female, 0% for the np.median path with >50% unvoiced); 0.5h fix (replace all 4 calls with voiced_f0 = f0[f0 > 0] filter; add len(voiced_f0) == 0 guard for silence segments; rerun 500-episode gender classification: female mean F0 215–225 Hz > 165 Hz all correctly classified; male mean F0 110–125 Hz < 165 Hz all correctly classified; 5 edge cases noted: 1 vocal fry speaker (F0 jumps: 60–80 Hz); 2 non-native speakers; 2 outdoor recordings with noise; overall accuracy 94%); 0.4h validation (spot-check 20 episodes against human labels; add voiced_fraction log and F0 range assertion to generation function; commit; 1.5h total). Hours logged to HourTab with per-session breakdown.
Related retainer posts
- crepe developer on retainer — crepe 0.0.12; Python 3.9;
crepe.predict()returns confidence alongside F0; confidence < 0.5 frames included in mean pitch; unvoiced-equivalent contamination via low-confidence frames; melody extraction accuracy 72%→91%; fix:frequency[confidence > 0.85]; 1.5h - pysptk developer on retainer — pysptk 0.1.21; Python 3.10;
mcep(alpha=0.41)on 8 kHz telephony; correct alpha for 8 kHz is 0.31; mel-warping mismatch; EER 14.7% (target 8.2%); fix:alpha=0.31; 2.0h - parselmouth developer on retainer — parselmouth 0.4.3; Python 3.9; Praat
to_pitch(time_step=0.0)auto-selects time_step based on pitch floor;pitch_floor=75(Praat default) gives time_step=1/(3×75)=4.4 ms; at 200 Hz speech rate developer expects 5 ms; temporal alignment off by 0.6 ms per frame; cumulative drift 60 ms per second; formant-pitch synchrony broken; vowel duration mismatch; fix: explicittime_step=0.005; 2.0h
Track retainer hours without a client portal
HourTab turns your time-tracker CSV into a public URL your client bookmarks. No login. No portal. CSV in, URL out.
See pricing →