Skip to content

Instantly share code, notes, and snippets.

@RicherMans
Created January 13, 2020 14:02
Show Gist options
  • Save RicherMans/dc1b50dd8043cee5872e0b7584f6202f to your computer and use it in GitHub Desktop.
Save RicherMans/dc1b50dd8043cee5872e0b7584f6202f to your computer and use it in GitHub Desktop.
LFCC implementation in librosa
import numpy as np
import scipy
import matplotlib.pyplot as plt
import librosa
def lin(sr, n_fft, n_filter=128, fmin=0.0, fmax=None, dtype=np.float32):
if fmax is None:
fmax = float(sr) / 2
# Initialize the weights
n_filter = int(n_filter)
weights = np.zeros((n_filter, int(1 + n_fft // 2)), dtype=dtype)
# Center freqs of each FFT bin
fftfreqs = librosa.fft_frequencies(sr=sr, n_fft=n_fft)
# 'Center freqs' of liner bands - uniformly spaced between limits
linear_f = np.linspace(fmin, fmax, n_filter + 2)
fdiff = np.diff(linear_f)
ramps = np.subtract.outer(linear_f, fftfreqs)
for i in range(n_filter):
# lower and upper slopes for all bins
lower = -ramps[i] / fdiff[i]
upper = ramps[i + 2] / fdiff[i + 1]
# .. then intersect them with each other and zero
weights[i] = np.maximum(0, np.minimum(lower, upper))
return weights
def linear_spec(y=None,
sr=22050,
n_fft=2048,
hop_length=512,
win_length=None,
window='hann',
center=True,
pad_mode='reflect',
power=2.0,
**kwargs):
S = np.abs(
librosa.core.stft(y=y,
n_fft=n_fft,
hop_length=hop_length,
win_length=win_length,
window=window,
center=center,
pad_mode=pad_mode))**power
filter = lin(sr=sr, n_fft=n_fft, **kwargs)
return np.dot(filter, S)
def lfcc(y=None,
sr=22050,
S=None,
n_lfcc=20,
dct_type=2,
norm='ortho',
**kwargs):
if S is None:
S = librosa.power_to_db(linear_spec(y=y, sr=sr, **kwargs))
M = scipy.fftpack.dct(S, axis=0, type=dct_type, norm=norm)[:n_lfcc]
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment