<jupyter_start><jupyter_text>592B, Class 7.1 (10/12). Spectral domain methods and representations: the cepstrum<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import scipy.io.wavfile as wavfile 
import scipy.signal as signal
from scipy import fftpack
import librosa

from ipywidgets import interactive
from IPython.display import Audio, display<jupyter_output><empty_output><jupyter_text>The cepstrumFor this section, we will be using [python_speech_features](https://github.com/jameslyons/python_speech_features) by [James Lyon](https://maxwell.ict.griffith.edu.au/spl/staff/j_lyons/home.htm). Install it using:```pip install python_speech_features```We can examine the code at the [github repository](https://github.com/jameslyons/python_speech_features) and via the [documentation](https://python-speech-features.readthedocs.io/en/latest/).James Lyon also has some nice write-ups on the [cepstrum](http://www.practicalcryptography.com/miscellaneous/machine-learning/tutorial-cepstrum-and-lpccs/) and [MFCCs](http://practicalcryptography.com/miscellaneous/machine-learning/guide-mel-frequency-cepstral-coefficients-mfccs/).<jupyter_code>import python_speech_features as sf
#from python_speech_features import mfcc
#from python_speech_features import logfbank<jupyter_output><empty_output><jupyter_text>We'll also be working with the Beijing Mandarin sound clip, `beijing_m5_17_c.wav`:<jupyter_code>#fs,sig = wavfile.read("beijing_m5_17_c.wav")

sig,fs = librosa.load("beijing_m5_17_c.wav")

display(Audio(data=sig, rate=fs))

# check bit depth
print("Bit depth can be determined from numpy array data type: ", sig.dtype)
print("Amplitude range from ", min(sig), " to ", max(sig))

# Get signal length
dur_sig = len(sig) *1/fs # duration of signal in seconds 
print("The signal is ", dur_sig, " seconds long.")<jupyter_output><empty_output><jupyter_text>We can plot a quick spectrogram with `librosa`:<jupyter_code>sgram = librosa.stft(sig)
librosa.display.specshow(sgram)  # Error??<jupyter_output><empty_output><jupyter_text>What we'll be doing now is working through the tutorials using the [github code repository code for `python_speech_features.base.mfcc`](https://python-speech-features.readthedocs.io/en/latest/). This function calls a ton of other sub-functions we'll need to understand in order to figure out how the computation is proceeding. The basic steps are described in the tutorial [here](http://practicalcryptography.com/miscellaneous/machine-learning/guide-mel-frequency-cepstral-coefficients-mfccs/). Let's work through these. But first a quick whirlwind overview to see where we are going.We can extract MCFF and filter bank features as follows:<jupyter_code># Extract MFCC features
mfcc_feat = sf.mfcc(sig, fs)

# Extract filter bank features
fbank_feat = sf.logfbank(sig, fs)

# Print some of fbank features
#print(fbank_feat[1:3,:])

print(fbank_feat.shape)
print(mfcc_feat.shape)<jupyter_output>WARNING:root:frame length (551) is greater than FFT size (512), frame will be truncated. Increase NFFT to avoid.
WARNING:root:frame length (551) is greater than FFT size (512), frame will be truncated. Increase NFFT to avoid.<jupyter_text>What do these look like? We can visualize them as follows:<jupyter_code>plt.figure()
plt.title("MFCC features")
plt.pcolormesh(np.transpose(mfcc_feat))
plt.xlabel("Time samples")
plt.ylabel("MFCCs")
plt.colorbar()

plt.figure()
plt.title("Filterbank features")
plt.pcolormesh(np.transpose(fbank_feat))
plt.xlabel("Time samples")
plt.ylabel("Filterbank")
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Step 0. Pick extract of sound to analyzeLet's pick some part of the audio file to analyze. We also can normalize the amplitude with respect to the bit depth.<jupyter_code>bit_depth = 16
sig = sig/((2**bit_depth)/2) # normalize signal amplitude for 16-bit

plt.figure()
t = np.arange(0, dur_sig, 1/fs) 
plt.plot(t,sig)
plt.xlabel("Time (s)")
plt.ylabel("Normalized amplitude")<jupyter_output><empty_output><jupyter_text>We can visually inspect the waveform and see which part of the waveform we want to analyze and then play that extract.<jupyter_code>sig_extract = sig[int(0.1 * fs):int(0.85 * fs)]  

display(Audio(data=sig_extract, rate=fs))


dur_sig_extract = len(sig_extract) *1/fs 

plt.figure()
t = np.arange(0, dur_sig_extract, 1/fs) 
plt.plot(t,sig_extract)
plt.xlabel("Time (s)")
plt.ylabel("Normalized amplitude")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11183618361836184, 'fraction_numerical': 0.011251125112511251, 'mean_word_length': 6.521150592216582, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 5, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '473293', 'license': 'MIT', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1476, 'n_words': 390, 'path': 'class_7-1/592B-F21-class_7-1.ipynb', 'repo_name': 'ling592b-f21/592b-f21-class', 'size': '4444'}