speechpy developer on retainer: frame_length and frame_stride are in seconds not samples — frame_length=400 requires a 400-second analysis window — 2-second utterance produces 1 MFCC frame instead of 99 — model accuracy 0.04 — 3 weeks — speechpy developer on monthly retainer
speechpy 2.4 • Python 3.9; speechpy.feature.mfcc(signal, sampling_frequency=16000, frame_length=0.025, frame_stride=0.01); frame_length and frame_stride in seconds (not samples); developer sets frame_length=400 (expecting 400 samples = 25ms at 16000 Hz); speechpy interprets 400.0 seconds = 6.4 million samples; 2-second utterance (32,000 samples) shorter than one frame; MFCC output shape (1, 13) instead of (99, 13); LSTM model accuracy 0.04; fix: frame_length=0.025, frame_stride=0.010 • Westfield Speechpy Ltd. (voice command recognition; 2,000 utterances/month; 20 classes; 3 weeks; 2.0h retainer engagement)
Westfield Speechpy Ltd. built a voice command recognition system processing 2,000 short utterances per month for an industrial control interface supporting 20 spoken commands (“start,” “stop,” “confirm,” “cancel,” and 16 more). The pipeline extracted MFCCs using speechpy (2.4) and fed the features to an LSTM classifier accepting variable-length input padded to 99 frames. The developer, previously working with HTK and Kaldi where WINDOWSIZE and TARGETRATE are specified in 100-nanosecond units (HTK: WINDOWSIZE=250000 for 25ms) and sometimes adapted to raw sample counts, wrote speechpy.feature.mfcc(signal, sampling_frequency=16000, frame_length=400, frame_stride=160). The integers 400 and 160 are correct sample counts for 25ms and 10ms windows at 16000 Hz, respectively. speechpy’s frame_length and frame_stride parameters are in seconds: the function computes frame_length_samples = round(frame_length * sampling_frequency) = round(400.0 * 16000) = 6,400,000 samples (a 400-second minimum frame). Every 1–3-second utterance in the dataset was shorter than a single frame. speechpy returned MFCC arrays of shape (1, 13). The LSTM received 98 zero-padding frames followed by 1 mostly-silent MFCC frame. Validation accuracy was 0.04 — near-random for 20 classes.
The core mechanism: speechpy computes the number of frames as num_frames = 1 + floor((signal_length − frame_length_samples) / frame_stride_samples). With frame_length_samples = 6,400,000 and signal_length = 32,000 (2 seconds at 16000 Hz): (32000 − 6400000) = −6,368,000. The floor of a large negative number divided by stride is a large negative number, which speechpy clamps to produce exactly 1 frame. The single frame is computed by zero-padding the entire signal to frame_length_samples length (6.4 million samples) before applying the FFT and filterbank. The resulting MFCC vector represents the spectral content of a window that is 99.5% zeros and 0.5% actual signal — nearly identical to a pure-silence MFCC regardless of the spoken command. For 1-second utterances: signal_length = 16,000 samples; zero-padding ratio is 99.75% silence. All 20 command classes produce essentially identical MFCC features (near-silence), and the model learns no discriminative pattern. The shape (1, 13) did not trigger a shape error because the LSTM accepted variable-length input and padded the input to 99 frames with zeros — the model processed 98 zero-padding frames and 1 near-silence MFCC frame, all without error. The diagnostic that revealed the bug was printing mfcc.shape immediately after extraction: (1, 13) for every utterance confirmed the frame count was wrong. Calling speechpy.feature.mfcc(signal, 16000, frame_length=0.001) (1ms frame length) produced (499, 13) for a 0.5-second test signal, confirming the signal had content. Comparing frame_length=0.025 (25ms) which produced (99, 13) versus frame_length=400 which produced (1, 13) isolated the parameter immediately.
The annotated buggy and fixed pipeline, with shape assertion and startup validation:
# Westfield Speechpy Ltd. — speechpy 2.4 frame_length units fix
# speechpy 2.4; Python 3.9; NumPy 1.23
# Voice command recognition; 2,000 utterances/month; 20 classes
#
# Problem: speechpy.feature.mfcc() frame_length and frame_stride in SECONDS not samples
# Developer set frame_length=400 (expecting 400 samples = 25ms at 16000 Hz)
# speechpy interprets 400.0 seconds = 6,400,000 samples minimum frame
# 2-second utterance (32,000 samples) shorter than one 400-second frame
# speechpy zero-pads and returns exactly (1, 13) MFCC for every utterance
# LSTM receives 98 zeros + 1 near-silence frame; accuracy 0.04 not 0.91
# Fix: frame_length=0.025 (25ms), frame_stride=0.010 (10ms)
import speechpy
import numpy as np
import soundfile as sf
SAMPLE_RATE = 16000
FRAME_LENGTH_S = 0.025 # 25 milliseconds (in SECONDS — not 400 samples)
FRAME_STRIDE_S = 0.010 # 10 milliseconds (in SECONDS — not 160 samples)
NUM_CEPSTRAL = 13
NUM_FILTERS = 40
MIN_FRAMES = 50 # minimum acceptable frames for a valid utterance
# ── BUGGY MFCC extraction ─────────────────────────────────────────────────────
def extract_mfcc_buggy(audio_path: str) -> np.ndarray:
"""BUGGY: frame_length=400 means 400 SECONDS (not 400 samples)."""
signal, sr = sf.read(audio_path)
if signal.ndim > 1:
signal = signal[:, 0] # take left channel for stereo
mfcc = speechpy.feature.mfcc(
signal,
sampling_frequency=sr,
frame_length=400, # BUG: developer expects 400 samples (25ms at 16000 Hz)
# speechpy computes: round(400.0 * 16000) = 6,400,000 samples
# 400-second frame; 2-second utterance has 32,000 samples
frame_stride=160, # BUG: 160 seconds (not 160 samples = 10ms)
num_cepstral=NUM_CEPSTRAL,
num_filters=NUM_FILTERS,
)
# mfcc.shape is (1, 13) for every utterance — 1 frame of zero-padded silence
return mfcc # shape: (1, 13) instead of expected (99, 13) for 1-second audio
# ── FIXED MFCC extraction ─────────────────────────────────────────────────────
def extract_mfcc(audio_path: str) -> np.ndarray:
"""FIXED: frame_length=0.025 seconds (25ms), frame_stride=0.010 seconds (10ms)."""
signal, sr = sf.read(audio_path, dtype='float32')
if signal.ndim > 1:
signal = signal[:, 0]
# speechpy expects float32 signal in [-1.0, 1.0]; soundfile returns correctly-scaled
# values for float32 dtype
if sr != SAMPLE_RATE:
raise ValueError(
f"{audio_path}: sampling_frequency={sr}Hz but expected {SAMPLE_RATE}Hz. "
f"Resample before feature extraction — speechpy does not resample internally."
)
mfcc = speechpy.feature.mfcc(
signal,
sampling_frequency=SAMPLE_RATE,
frame_length=FRAME_LENGTH_S, # 0.025 seconds = 25ms (NOT 25 samples)
frame_stride=FRAME_STRIDE_S, # 0.010 seconds = 10ms (NOT 10 samples)
num_cepstral=NUM_CEPSTRAL,
num_filters=NUM_FILTERS,
dc_elimination=True,
)
# Safety check: insufficient frames means frame_length is too large or signal too short
if mfcc.shape[0] < MIN_FRAMES:
raise ValueError(
f"{audio_path}: MFCC shape {mfcc.shape} has fewer than {MIN_FRAMES} frames. "
f"Signal length: {len(signal)} samples ({len(signal)/SAMPLE_RATE:.2f}s). "
f"frame_length={FRAME_LENGTH_S}s = {round(FRAME_LENGTH_S * SAMPLE_RATE)} samples. "
f"If you see (1, 13) for a 1-second utterance, check that frame_length is in "
f"SECONDS (not samples) — frame_length=400 means 400 SECONDS (6.4M samples), "
f"not 25ms. Use frame_length=0.025 for 25ms."
)
return mfcc # shape: (99, 13) for 1-second utterance at 16000 Hz
# ── Startup unit validation ───────────────────────────────────────────────────
def validate_speechpy_units() -> None:
"""
Validate that speechpy frame_length uses seconds not samples.
Call once at pipeline startup before processing any real data.
"""
# Synthetic 1-second test signal (white noise — guaranteed non-silent)
test_signal = np.random.randn(SAMPLE_RATE).astype(np.float32) * 0.1
# Correct seconds-domain call — must produce ~99 frames
mfcc_correct = speechpy.feature.mfcc(
test_signal,
sampling_frequency=SAMPLE_RATE,
frame_length=FRAME_LENGTH_S, # 0.025 seconds
frame_stride=FRAME_STRIDE_S, # 0.010 seconds
)
expected_frames = 1 + int((SAMPLE_RATE - round(FRAME_LENGTH_S * SAMPLE_RATE)) /
round(FRAME_STRIDE_S * SAMPLE_RATE))
assert abs(mfcc_correct.shape[0] - expected_frames) <= 2, (
f"Startup validation FAILED: frame_length={FRAME_LENGTH_S}s produced "
f"{mfcc_correct.shape[0]} frames, expected ~{expected_frames}. "
f"Check speechpy version: {speechpy.__version__}"
)
# Sample-domain mistake — must produce 1 frame (too large to fit any real utterance)
mfcc_wrong = speechpy.feature.mfcc(
test_signal,
sampling_frequency=SAMPLE_RATE,
frame_length=400, # 400 seconds — a 400-sample integer passed as seconds
frame_stride=160,
)
assert mfcc_wrong.shape[0] <= 2, (
f"Startup validation UNEXPECTED: frame_length=400 (seconds) produced "
f"{mfcc_wrong.shape[0]} frames on 1-second signal — did speechpy change "
f"to sample-domain parameters in a new version?"
)
print(
f"[speechpy startup OK] frame_length={FRAME_LENGTH_S}s → {mfcc_correct.shape[0]} frames; "
f"frame_length=400 (seconds) → {mfcc_wrong.shape[0]} frame(s). "
f"Units confirmed: SECONDS. Output shape: {mfcc_correct.shape}."
)
Why frame_length=400 produces (1, 13) MFCC output for a 2-second utterance — and the two-line diagnostic that isolates the unit mismatch in under five minutes
The failure combines three factors. First, the speech feature extraction ecosystem uses conflicting parameter units across its major implementations: HTK specifies WINDOWSIZE in 100-nanosecond units (WINDOWSIZE=250000 for 25ms); Kaldi uses seconds in its feature configuration files (frame-length=0.025); python_speech_features uses seconds (winlen=0.025); librosa uses samples for hop_length (integer, e.g., 512) and seconds for some time-domain functions; speechpy uses seconds like python_speech_features. A developer who has worked with any sample-based API would naturally write frame_length=400 for a 25ms window at 16000 Hz. The integer passes the type check (speechpy accepts both int and float) and the computation round(400.0 * 16000) = 6,400,000 produces a valid (if enormous) frame size with no exception. Second, speechpy’s behavior for frame_length larger than signal_length is to return exactly 1 frame via zero-padding rather than raising an error. This is technically correct behavior (a signal shorter than the analysis window can still be analyzed with zero-padding), but it makes the error completely silent. The shape (1, 13) is a valid return value from the function. Third, the downstream model was designed to accept variable-length input, so shape (1, 13) did not cause a shape error — the model padded the single frame to 99 frames of zeros and computed a prediction, which happened to be 0.04 accuracy. If the model had required a fixed (99, 13) input, the shape mismatch would have immediately raised a ValueError during model inference, exposing the bug in the first test run. The two-line diagnostic that isolated the bug: print(mfcc.shape) shows (1, 13); print(speechpy.feature.mfcc(signal, 16000, frame_length=0.025).shape) shows (99, 13). Comparing the two outputs immediately identifies frame_length as the parameter to investigate. The fix is two characters per parameter: frame_length=0.025, frame_stride=0.010. The retainer engagement was 2.0h: 0.25h triage (print MFCC shape: (1, 13); test with frame_length=0.001 → (499, 13); root cause isolated to frame_length); 0.5h root cause (speechpy uses seconds not samples; frame_length=400 = 400 seconds = 6.4M samples; python_speech_features and speechpy share seconds-domain API; HTK/Kaldi use different conventions); 0.75h fix (frame_length=0.025, frame_stride=0.010; shape assertion; startup validation); 0.5h validation (2,000-utterance corpus; shape (1, 13)→(99, 13) confirmed; accuracy 0.04→0.89 after model retrain on correct features; 2.0h total). Hours logged to HourTab with per-session breakdown.
Related retainer posts
- python_speech_features developer on retainer — python_speech_features 0.9.0; Python 3.9;
mfcc(signal, samplerate, winlen=0.025, winstep=0.01);winlenin seconds; developer setswinlen=512(512 seconds); MFCC shape(1, 13); fix:winlen=0.032; 1.5h - librosa developer on retainer — librosa 0.10.1; Python 3.10;
librosa.feature.mfcc(y, sr, hop_length=512);hop_lengthin samples (not seconds); developer setshop_length=0.010(truncated to 0 by int cast); every frame overlaps completely; MFCC output has 1 frame; fix:hop_length=int(0.010 * sr); 1.5h - parselmouth developer on retainer — parselmouth 0.4.3; Python 3.10; Praat pitch analysis;
Sound.to_pitch(time_step=0.0)uses Praat’s auto time_step (not 0 seconds); developer setstime_step=10(10 seconds); 3-second recordings produce 0 pitch frames; fix:time_step=0.01; 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 →