{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <span style='color:#A80808'>Objective</span>\n\nThis notebook provides a basic guide for preprocessing audio data for deep learning.","metadata":{"_uuid":"23bb90fd-5632-4a72-be77-fa9e9352d4bc","_cell_guid":"e7fa6a1e-abf6-4482-9f9c-cc84f3591178","jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-02-24T13:30:53.171936Z","iopub.execute_input":"2022-02-24T13:30:53.172369Z","iopub.status.idle":"2022-02-24T13:30:56.004635Z","shell.execute_reply.started":"2022-02-24T13:30:53.172277Z","shell.execute_reply":"2022-02-24T13:30:56.003330Z"}}},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport librosa, librosa.display","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:52:16.340697Z","iopub.execute_input":"2022-02-24T13:52:16.341035Z","iopub.status.idle":"2022-02-24T13:52:16.345674Z","shell.execute_reply.started":"2022-02-24T13:52:16.341003Z","shell.execute_reply":"2022-02-24T13:52:16.344840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style='color:#A80808'>Waveform</span>","metadata":{}},{"cell_type":"code","source":"# Select a random audio file\nfile = '../input/birdclef-2022/train_audio/calqua/XC109664.ogg'\n\n# Get the waveform signal\nsignal, sample_rate = librosa.load(file, sr=22050)\n\n# Show the waveform\nplt.figure(figsize=(15,5))\nlibrosa.display.waveshow(signal, sr=sample_rate, color='red')\nplt.xlabel('Time (s)')\nplt.ylabel('Amplitude')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:45:26.251895Z","iopub.execute_input":"2022-02-24T13:45:26.252213Z","iopub.status.idle":"2022-02-24T13:45:27.175444Z","shell.execute_reply.started":"2022-02-24T13:45:26.252178Z","shell.execute_reply":"2022-02-24T13:45:27.173234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The duration of the audio is\nlen(signal)/sample_rate","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:44:37.071070Z","iopub.execute_input":"2022-02-24T13:44:37.071479Z","iopub.status.idle":"2022-02-24T13:44:37.079038Z","shell.execute_reply.started":"2022-02-24T13:44:37.071440Z","shell.execute_reply":"2022-02-24T13:44:37.078394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style='color:#A80808'>Spectrum</span>","metadata":{}},{"cell_type":"code","source":"# Fast Fourier transform for computing the magnitude in frequency domain\nfft = np.fft.fft(signal)\nmagnitude = np.abs(fft)\nfrequency = np.linspace(0,sample_rate,len(magnitude))\n\n# Show the spectrum, only the half left of the spectrum is shown as it is symetric.\nplt.figure(figsize=(15,3))\nplt.plot(frequency[:int(len(frequency)/2)], magnitude[:int(len(frequency)/2)], color='green')\nplt.xlabel('Frequency')\nplt.ylabel('Magnitude')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T14:54:42.470834Z","iopub.execute_input":"2022-02-24T14:54:42.471454Z","iopub.status.idle":"2022-02-24T14:54:42.951025Z","shell.execute_reply.started":"2022-02-24T14:54:42.471413Z","shell.execute_reply":"2022-02-24T14:54:42.949868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style='color:#A80808'>Spectrogram</span>\n\n[wiki](https://en.wikipedia.org/wiki/Spectrogram)","metadata":{}},{"cell_type":"code","source":"# Short Fourier transform for computing the spectrogram\nn_fft = 2048 # signal window size for stft\nhop_length = 512 # window shifting = distance between two neighbor window centers\nstft = librosa.stft(signal, n_fft=n_fft, hop_length=hop_length)\n\nspectrogram = np.abs(stft)\nlog_spectrogram = librosa.amplitude_to_db(spectrogram)\n\n# Show the spectrogram\nplt.figure(figsize=(15,5))\nlibrosa.display.specshow(log_spectrogram, sr=sample_rate, hop_length=hop_length)\nplt.colorbar()\nplt.xlabel('Time (s)')\nplt.ylabel('Frequency')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T15:20:12.489631Z","iopub.execute_input":"2022-02-24T15:20:12.489991Z","iopub.status.idle":"2022-02-24T15:20:13.928974Z","shell.execute_reply.started":"2022-02-24T15:20:12.489957Z","shell.execute_reply":"2022-02-24T15:20:13.928290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style='color:#A80808'>Mel-frequency cepstral coefficients (MFCCs)</span>\n\n[wiki](https://en.wikipedia.org/wiki/Mel-frequency_cepstrum#:~:text=Mel%2Dfrequency%20cepstral%20coefficients%20(MFCCs,collectively%20make%20up%20an%20MFC.&text=This%20frequency%20warping%20can%20allow,windowed%20excerpt%20of)%20a%20signal.)","metadata":{}},{"cell_type":"code","source":"n_fft = 2048 # signal window size for stft\nhop_length = 512 # window shifting = distance between two neighbor window centers\nmfcc = librosa.feature.mfcc(y=signal, sr=sample_rate, n_mfcc=5)\n\n# Show mfcc\nplt.figure(figsize=(15,5))\nlibrosa.display.specshow(mfcc, sr=sample_rate, hop_length=hop_length)\nplt.colorbar()\nplt.xlabel('Time (s)')\nplt.ylabel('MFCC')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T15:34:13.355657Z","iopub.execute_input":"2022-02-24T15:34:13.356567Z","iopub.status.idle":"2022-02-24T15:34:13.589753Z","shell.execute_reply.started":"2022-02-24T15:34:13.356510Z","shell.execute_reply":"2022-02-24T15:34:13.589166Z"},"trusted":true},"execution_count":null,"outputs":[]}]}