{"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":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport matplotlib.pyplot as plt\nfrom IPython.display import Audio, Video\nimport torch\nimport torchaudio\nfrom torchaudio.io import StreamReader\nimport torchaudio.functional as F\nimport torchaudio.transforms as T\nimport librosa","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-16T14:29:36.996725Z","iopub.execute_input":"2023-03-16T14:29:36.997111Z","iopub.status.idle":"2023-03-16T14:29:37.003439Z","shell.execute_reply.started":"2023-03-16T14:29:36.997079Z","shell.execute_reply":"2023-03-16T14:29:37.001990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"/kaggle/input/birdclef-2023/sample_submission.csv\")\ntrain_metadata = pd.read_csv(\"/kaggle/input/birdclef-2023/train_metadata.csv\")\ntrain_df = pd.read_csv(\"/kaggle/input/birdclef-2023/eBird_Taxonomy_v2021.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:29:37.008563Z","iopub.execute_input":"2023-03-16T14:29:37.008955Z","iopub.status.idle":"2023-03-16T14:29:37.118002Z","shell.execute_reply.started":"2023-03-16T14:29:37.008925Z","shell.execute_reply":"2023-03-16T14:29:37.116120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:29:37.121173Z","iopub.execute_input":"2023-03-16T14:29:37.121575Z","iopub.status.idle":"2023-03-16T14:29:37.139513Z","shell.execute_reply.started":"2023-03-16T14:29:37.121541Z","shell.execute_reply":"2023-03-16T14:29:37.136952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:29:37.141173Z","iopub.execute_input":"2023-03-16T14:29:37.141520Z","iopub.status.idle":"2023-03-16T14:29:37.163794Z","shell.execute_reply.started":"2023-03-16T14:29:37.141489Z","shell.execute_reply":"2023-03-16T14:29:37.161988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:29:37.167848Z","iopub.execute_input":"2023-03-16T14:29:37.168247Z","iopub.status.idle":"2023-03-16T14:29:37.190144Z","shell.execute_reply.started":"2023-03-16T14:29:37.168211Z","shell.execute_reply":"2023-03-16T14:29:37.188136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_train_dataframe(df):\n    df = pd.DataFrame()\n    df['primary_label'] = train_metadata['primary_label']\n    df['filename'] = train_metadata['filename']\n    return df\n\ntraining_dataframe = create_train_dataframe(train_metadata)\ntraining_dataframe.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:29:37.191861Z","iopub.execute_input":"2023-03-16T14:29:37.192288Z","iopub.status.idle":"2023-03-16T14:29:37.211717Z","shell.execute_reply.started":"2023-03-16T14:29:37.192254Z","shell.execute_reply":"2023-03-16T14:29:37.210293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"waveform, sample_rate = torchaudio.load('/kaggle/input/birdclef-2023/train_audio/yetgre1/XC247367.ogg')","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:29:37.213325Z","iopub.execute_input":"2023-03-16T14:29:37.213673Z","iopub.status.idle":"2023-03-16T14:29:37.245819Z","shell.execute_reply.started":"2023-03-16T14:29:37.213641Z","shell.execute_reply":"2023-03-16T14:29:37.244165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## What are Waveforms, sample_rate and Spectograms \n\n    - waveform : The generic term waveform means a graphical representation of the shape and form of a signal moving in a gaseous, liquid, or solid medium. For sound, the term describes a depiction of the pattern of sound pressure variation (or amplitude) in the time domain.\n    \n    - SampleRate : Sample rate is the number of samples per second that are taken of a waveform to create a discete digital signal. The higher the sample rate, the more snapshots you capture of the audio signal. The audio sample rate is measured in kilohertz (kHz) and it determines the range of frequencies captured in digital audio.\n    \n## What are spectograms and why do we prefer melspectrogram for deeplearning\n\n    - Spectograms: A spectrogram is a visual way of representing the signal strength, or “loudness”, of a signal over time at various frequencies present in a particular waveform. Not only can one see whether there is more or less energy at, for example, 2 Hz vs 10 Hz, but one can also see how energy levels vary over time.\n    \n    \n    -MelSpectograms: A Mel Spectrogram makes two important changes relative to a regular Spectrogram that plots Frequency vs Time. It uses the Mel Scale instead of Frequency on the y-axis.\n    It uses the Decibel Scale instead of Amplitude to indicate colors. For deep learning models, we usually use this rather than a simple Spectrogram.\n\n","metadata":{}},{"cell_type":"code","source":"def plot_waveform(waveform, sr, title=\"Waveform\"):\n    waveform = waveform.numpy()\n\n    num_channels, num_frames = waveform.shape\n    time_axis = torch.arange(0, num_frames) / sr\n\n    figure, axes = plt.subplots(num_channels, 1)\n    axes.plot(time_axis, waveform[0], linewidth=1)\n    axes.grid(True)\n    figure.suptitle(title)\n    plt.show(block=False)\n\n\ndef plot_spectrogram(specgram, title=None, ylabel=\"freq_bin\"):\n    fig, axs = plt.subplots(1, 1)\n    axs.set_title(title or \"Spectrogram (db)\")\n    axs.set_ylabel(ylabel)\n    axs.set_xlabel(\"frame\")\n    im = axs.imshow(librosa.power_to_db(specgram), origin=\"lower\", aspect=\"auto\")\n    fig.colorbar(im, ax=axs)\n    plt.show(block=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:37:37.387984Z","iopub.execute_input":"2023-03-16T14:37:37.389182Z","iopub.status.idle":"2023-03-16T14:37:37.399138Z","shell.execute_reply.started":"2023-03-16T14:37:37.389126Z","shell.execute_reply":"2023-03-16T14:37:37.397105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_waveform(waveform, sample_rate)\nAudio(\"/kaggle/input/birdclef-2023/train_audio/yetgre1/XC247367.ogg\")","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:37:38.873363Z","iopub.execute_input":"2023-03-16T14:37:38.873782Z","iopub.status.idle":"2023-03-16T14:37:44.596132Z","shell.execute_reply.started":"2023-03-16T14:37:38.873746Z","shell.execute_reply":"2023-03-16T14:37:44.594330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_fft = 1024\nwin_length = None\nhop_length = 512\n\n# Define transform\nspectrogram = T.Spectrogram(\n    n_fft=n_fft,\n    win_length=win_length,\n    hop_length=hop_length,\n    center=True,\n    pad_mode=\"reflect\",\n    power=2.0,\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:36:20.467820Z","iopub.execute_input":"2023-03-16T14:36:20.468304Z","iopub.status.idle":"2023-03-16T14:36:20.495757Z","shell.execute_reply.started":"2023-03-16T14:36:20.468257Z","shell.execute_reply":"2023-03-16T14:36:20.494831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec = spectrogram(waveform)\nplot_spectrogram(spec[0], title=\"torchaudio spectogram\")\n","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:43:57.466386Z","iopub.execute_input":"2023-03-16T14:43:57.466899Z","iopub.status.idle":"2023-03-16T14:43:57.781645Z","shell.execute_reply.started":"2023-03-16T14:43:57.466841Z","shell.execute_reply":"2023-03-16T14:43:57.780661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_fft = 1024\nwin_length = None\nhop_length = 512\nn_mels = 128\n\nmel_spectrogram = T.MelSpectrogram(\n    sample_rate=sample_rate,\n    n_fft=n_fft,\n    win_length=win_length,\n    hop_length=hop_length,\n    center=True,\n    pad_mode=\"reflect\",\n    power=2.0,\n    norm=\"slaney\",\n    onesided=True,\n    n_mels=n_mels,\n    mel_scale=\"htk\",\n)\n\nmelspec = mel_spectrogram(waveform)","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:38:35.939499Z","iopub.execute_input":"2023-03-16T14:38:35.939889Z","iopub.status.idle":"2023-03-16T14:38:35.969398Z","shell.execute_reply.started":"2023-03-16T14:38:35.939855Z","shell.execute_reply":"2023-03-16T14:38:35.968486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_spectrogram(melspec[0], title=\"MelSpectrogram - torchaudio\", ylabel=\"mel freq\")\n","metadata":{"execution":{"iopub.status.busy":"2023-03-16T14:38:44.423468Z","iopub.execute_input":"2023-03-16T14:38:44.423876Z","iopub.status.idle":"2023-03-16T14:38:44.750941Z","shell.execute_reply.started":"2023-03-16T14:38:44.423834Z","shell.execute_reply":"2023-03-16T14:38:44.749485Z"},"trusted":true},"execution_count":null,"outputs":[]}]}