MFCC
Python
librosa
python_speech_features
tensorflow.signal

MFCC Python completely different result from librosa vs python_speech_features vs tensorflow.signal

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Different MFCC values from librosa, python_speech_features, and tensorflow.signal are normal when defaults do not match. Each library uses different assumptions for frame size, hop length, mel scale, normalization, and logarithm behavior. To compare results fairly, align every parameter first, then measure remaining numeric differences.

Why the Numbers Differ

MFCC extraction is a pipeline, and small changes at any stage propagate to final coefficients. The common sources of mismatch are:

  • Sample rate assumptions.
  • Pre-emphasis enabled in one library and disabled in another.
  • Window type and frame boundary handling.
  • Mel filter design and frequency range.
  • DCT type and coefficient normalization.
  • Log floor epsilon used before DCT.

python_speech_features often enables pre-emphasis by default, while many librosa workflows do not. librosa defaults are music-oriented and can differ from speech recipes. tensorflow.signal is low-level and expects explicit parameter choices, which is powerful but easy to misconfigure.

Build a Controlled Comparison

Start with one waveform, one sample rate, and shared parameter values. The script below computes MFCCs with aligned settings and prints shape and rough magnitude statistics.

python
1import numpy as np
2import librosa
3import tensorflow as tf
4from python_speech_features import mfcc as psf_mfcc
5
6# 1 second test tone + noise
7sr = 16000
8t = np.linspace(0, 1, sr, endpoint=False)
9y = (0.2 * np.sin(2 * np.pi * 440 * t) + 0.01 * np.random.randn(sr)).astype(np.float32)
10
11# Shared parameters
12n_fft = 400
13hop = 160
14n_mels = 26
15n_mfcc = 13
16fmin, fmax = 0, 8000
17
18# librosa MFCC
19lib_mfcc = librosa.feature.mfcc(
20    y=y,
21    sr=sr,
22    n_mfcc=n_mfcc,
23    n_fft=n_fft,
24    hop_length=hop,
25    n_mels=n_mels,
26    fmin=fmin,
27    fmax=fmax,
28    htk=True
29)
30
31# python_speech_features MFCC
32psf = psf_mfcc(
33    signal=y,
34    samplerate=sr,
35    winlen=n_fft / sr,
36    winstep=hop / sr,
37    numcep=n_mfcc,
38    nfilt=n_mels,
39    nfft=n_fft,
40    lowfreq=fmin,
41    highfreq=fmax,
42    preemph=0.0,
43    ceplifter=0,
44    appendEnergy=False
45).T
46
47# tensorflow.signal MFCC
48wave = tf.convert_to_tensor(y)
49stft = tf.signal.stft(wave, frame_length=n_fft, frame_step=hop, fft_length=n_fft,
50                      window_fn=tf.signal.hann_window)
51spec = tf.abs(stft) ** 2
52
53mel_matrix = tf.signal.linear_to_mel_weight_matrix(
54    num_mel_bins=n_mels,
55    num_spectrogram_bins=n_fft // 2 + 1,
56    sample_rate=sr,
57    lower_edge_hertz=fmin,
58    upper_edge_hertz=fmax,
59)
60mel_spec = tf.matmul(spec, mel_matrix)
61log_mel = tf.math.log(mel_spec + 1e-6)
62tf_mfcc = tf.signal.mfccs_from_log_mel_spectrograms(log_mel)[..., :n_mfcc].numpy().T
63
64for name, m in [("librosa", lib_mfcc), ("python_speech_features", psf), ("tensorflow", tf_mfcc)]:
65    print(name, "shape:", m.shape, "mean:", float(np.mean(m)), "std:", float(np.std(m)))

This will not produce bit-identical arrays, but it should reduce the mismatch from huge to manageable.

Normalize Comparison Before Judging Quality

Even with aligned settings, frame counts can differ by one frame due to padding behavior. Trim to common shape before computing distance metrics.

python
1import numpy as np
2
3
4def align(a, b):
5    rows = min(a.shape[0], b.shape[0])
6    cols = min(a.shape[1], b.shape[1])
7    return a[:rows, :cols], b[:rows, :cols]
8
9
10a, b = align(lib_mfcc, tf_mfcc)
11mae = np.mean(np.abs(a - b))
12print("MAE librosa vs tensorflow:", float(mae))

If error remains large, check whether coefficient zero includes log-energy in one implementation but not others. That single convention can dominate perceived differences.

Pick a Library Based on Workflow

Use librosa when quick analysis and broad audio tooling matter. Use python_speech_features for classic speech pipelines and lightweight dependency footprint. Use tensorflow.signal when you need graph execution, model integration, or GPU-friendly preprocessing.

Consistency usually matters more than a specific library choice. In production, freeze parameters and package versions, then store a small reference waveform with expected MFCC stats for regression checks.

Common Pitfalls

  • Comparing defaults across libraries and expecting similar MFCC arrays.
  • Forgetting that pre-emphasis or liftering is enabled in only one path.
  • Mixing mel scale conventions and then blaming numerical precision.
  • Ignoring frame padding differences when comparing matrix shapes.
  • Changing package versions without regenerating baseline feature tests.

Summary

  • Large MFCC mismatches are usually parameter mismatches, not library bugs.
  • Align sample rate, FFT settings, mel bins, frequency range, DCT policy, and log epsilon.
  • Compare aligned arrays with explicit shape trimming and a numeric distance metric.
  • Choose one library per pipeline and freeze configuration for reproducibility.
  • Keep a regression waveform and expected statistics to catch silent drift.

Course illustration
Course illustration

All Rights Reserved.