Audio Classification with an Educational Classifier

Audio Classification Demo

Build a kNN classifier on mean MFCC features for a few clips, and evaluate it with a confusion matrix, to understand the processing pipeline rather than to achieve high performance.

Intermediate

Educational content in the form of a scientific draft that needs expert review before being adopted as an academic reference. No experimental result that was not actually run is shown here.

Prerequisites

  • Python with numpy and librosa
  • Labeled clips with a suitable license, or synthetic signals

Tools

  • audio-classification
  • local-classifier
  • mfcc-explorer

Implementation Steps

  1. Choose a few classes and licensed clips
  2. Split the data without leakage between the two sets
  3. Extract mean MFCCs
  4. Train kNN
  5. Evaluate with accuracy and per-class F1
  6. Plot the confusion matrix and analyze the errors

Code

JavaScript / PythonImplemented and Tested
import numpy as np, librosa
sr = 16000
t = np.arange(sr) / sr
rng = np.random.default_rng(0)
def feat(x): return librosa.feature.mfcc(y=x, sr=sr, n_mfcc=13).mean(axis=1)
def make(kind):
    if kind == 0: return np.sin(2*np.pi*rng.uniform(250, 350)*t)
    return rng.standard_normal(sr) * 0.3
X = np.array([feat(make(k)) for k in (0, 1) * 10]); y = np.array([0, 1] * 10)
Xt = np.array([feat(make(k)) for k in (0, 1) * 5]); yt = np.array([0, 1] * 5)
pred = [y[np.argmin(np.linalg.norm(X - q, axis=1))] for q in Xt]
print('accuracy', np.mean(np.array(pred) == yt))

Expected Results

The classifier is expected to separate tone from noise in the synthetic data, which does not imply similar performance on realistic sounds.

These are educational expectations and not experimental results actually measured.

Project Tests

  • Split the data by original recording to avoid leakage
  • Compare accuracy with a random classifier
  • Report per-class F1 and the confusion matrix

References

  1. Cover, Hart. Nearest neighbor pattern classification. IEEE Transactions on Information Theory (1967). DOI

Audio processing in the experiments takes place inside your browser. Audio, texts, and conversations are not saved on the server. Privacy Policy