Skip to content

Instantly share code, notes, and snippets.

@dmastropole
dmastropole / plot_mfccs.py
Last active October 17, 2019 18:43
Plot MFCCs
# Plot MFCCs
plt.imshow(mfccs,
aspect='auto',
origin='lower',
interpolation='none',
cmap=plt.cm.Blues)
plt.title("MFCCs")
plt.xlabel("Frames")
plt.ylabel("MFCCs")
@dmastropole
dmastropole / dct.py
Last active October 17, 2019 18:42
DCT
# Perform a DCT
from essentia.standard import DCT
dct = DCT(inputSize=n_bands, outputSize=13)
mfccs = dct(log_mels)
plt.bar(np.arange(len(mfccs)), mfccs, align='center')
plt.title('Mel-Frequency Cepstrum')
plt.xlabel('Cosines')
plt.ylabel('Coefficients')
@dmastropole
dmastropole / mel_bands.py
Last active October 17, 2019 00:54
Mel Bands
# Compute the mel bands
n_bands = 25
# The "+ 2" will make sense in a bit!
m = np.linspace(mel_formula(f_low), mel_formula(f_high), n_bands + 2)
print(m)
@dmastropole
dmastropole / power_spectrum.py
Last active October 16, 2019 23:50
Power Spectrum
from essentia.standard import Windowing, PowerSpectrum
# Compute the spectrum of a frame
w = Windowing(type='hann')
spectrum = PowerSpectrum(size=1024)
frame = audio[0:1024]
spec = spectrum(w(frame))
# Plot the spectrum of a frame
plt.plot(spec)
@dmastropole
dmastropole / mel_powers.py
Last active October 16, 2019 23:49
Mel Powers
from essentia.standard import MelBands
# Compute the mel band powers
melbands = MelBands(lowFrequencyBound=f_low,
highFrequencyBound=f_high,
inputSize=1024,
numberBands=n_bands,
type='magnitude', # We already computed the power.
sampleRate=44100)
mels = melbands(spec)
@dmastropole
dmastropole / log_powers.py
Last active October 16, 2019 23:48
Log of Mel Band Powers
from essentia.standard import UnaryOperator
# Convert to decibels
log10 = UnaryOperator(type='log10')
log_mels = log10(mels)
# Plot the mel band powers
plt.bar(np.arange(len(log_mels)), log_mels, align='center')
plt.title('Log of the Mel Band Powers')
plt.xlabel('Mel Bands')
@dmastropole
dmastropole / compute_mfccs.py
Created October 15, 2019 00:00
Compute MFCCs
from essentia.standard import MFCC, FrameGenerator
mfcc = MFCC(highFrequencyBound=f_high,
lowFrequencyBound=f_low,
inputSize=1024,
numberBands=n_bands,
numberCoefficients=13,
type='magnitude',
sampleRate=44100)
@dmastropole
dmastropole / mel_filterbanks.py
Last active October 14, 2019 23:52
Mel Filterbanks
for n in range(n_bands):
plt.plot(f[n:n+3], [0, 1, 0]) # Does the "+ 2" make sense now?
plt.title('Mel Filterbanks')
plt.xlabel('Frequency (Hz)')
@dmastropole
dmastropole / frequency_bands.py
Created October 14, 2019 23:41
Frequency Bands
def inverse_mel_formula(m):
return 700 * (np.power(10, m/2595) - 1)
# Convert back to frequency
f = inverse_mel_formula(m)
print(f)
@dmastropole
dmastropole / mel_scale.py
Created October 14, 2019 23:36
Mel Scale
# Define function for computing mels
def mel_formula(f):
return 2595 * np.log(1 + f/700)
# Frequency range
f_low = 40
f_high = 44100 / 2 # Nyquist frequency
f = np.linspace(f_low, f_high)
# Plot relationship between frequency and mels