{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.11"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":91844,"databundleVersionId":11361821,"sourceType":"competition"}],"dockerImageVersionId":31011,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n#import os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n   # for filename in filenames:\n     #   print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:22.928070Z","iopub.execute_input":"2025-05-04T05:16:22.928457Z","iopub.status.idle":"2025-05-04T05:16:24.449438Z","shell.execute_reply.started":"2025-05-04T05:16:22.928437Z","shell.execute_reply":"2025-05-04T05:16:24.448585Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='top'></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style=\"background-color:#682F3F;font-family:newtimeroman;color:#FFF9ED;font-size:150%;text-align:center;border-radius:10px 10px;\">IMPORT OF LIBRARIES</p>   ","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pylab as plt\nimport seaborn as sns\n\nfrom glob import glob\n\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:24.450234Z","iopub.execute_input":"2025-05-04T05:16:24.450594Z","iopub.status.idle":"2025-05-04T05:16:26.009683Z","shell.execute_reply.started":"2025-05-04T05:16:24.450570Z","shell.execute_reply":"2025-05-04T05:16:26.008875Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from itertools import cycle\n\nsns.set_theme(style=\"white\", palette=None)\ncolor_pal=plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"]\ncolor_cycle=cycle(plt.rcParams[\"axes.prop_cycle\"].by_key()[\"color\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:26.011340Z","iopub.execute_input":"2025-05-04T05:16:26.011648Z","iopub.status.idle":"2025-05-04T05:16:26.018770Z","shell.execute_reply.started":"2025-05-04T05:16:26.011621Z","shell.execute_reply":"2025-05-04T05:16:26.018112Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='top'></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style=\"background-color:#682F3F;font-family:newtimeroman;color:#FFF9ED;font-size:150%;text-align:center;border-radius:10px 10px;\">IMPORT OF DATA</p>   ","metadata":{}},{"cell_type":"code","source":"audio_files=glob('/kaggle/input/birdclef-2025/train_audio/*/*.ogg')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:26.019270Z","iopub.execute_input":"2025-05-04T05:16:26.019478Z","iopub.status.idle":"2025-05-04T05:16:27.232600Z","shell.execute_reply.started":"2025-05-04T05:16:26.019458Z","shell.execute_reply":"2025-05-04T05:16:27.231694Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='top'></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style=\"background-color:#682F3F;font-family:newtimeroman;color:#FFF9ED;font-size:150%;text-align:center;border-radius:10px 10px;\">PREPROCESSING THE DATA</p>   ","metadata":{}},{"cell_type":"code","source":"# Play audio file\nipd.Audio(audio_files[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:27.233638Z","iopub.execute_input":"2025-05-04T05:16:27.233975Z","iopub.status.idle":"2025-05-04T05:16:27.326336Z","shell.execute_reply.started":"2025-05-04T05:16:27.233949Z","shell.execute_reply":"2025-05-04T05:16:27.325559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y,sr=librosa.load(audio_files[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:27.327301Z","iopub.execute_input":"2025-05-04T05:16:27.327571Z","iopub.status.idle":"2025-05-04T05:16:39.034376Z","shell.execute_reply.started":"2025-05-04T05:16:27.327547Z","shell.execute_reply":"2025-05-04T05:16:39.033801Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f'y: {y[10]}')\nprint(f'shape y: {y.shape}')\nprint(f'sr: {sr}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:39.035203Z","iopub.execute_input":"2025-05-04T05:16:39.035502Z","iopub.status.idle":"2025-05-04T05:16:39.040270Z","shell.execute_reply.started":"2025-05-04T05:16:39.035485Z","shell.execute_reply":"2025-05-04T05:16:39.039544Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.Series(y).plot(figsize=(10,5), lw=1,title='Raw Audio Example',\n                                       color=color_pal[0])\n                  \nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:39.040999Z","iopub.execute_input":"2025-05-04T05:16:39.041265Z","iopub.status.idle":"2025-05-04T05:16:39.517352Z","shell.execute_reply.started":"2025-05-04T05:16:39.041241Z","shell.execute_reply":"2025-05-04T05:16:39.516678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_trimmed, _ =librosa.effects.trim(y,top_db=20)\npd.Series(y_trimmed).plot(figsize=(10,5), lw=1,title='Raw Audio Trimmed Example',\n                                       color=color_pal[1])\n                  \nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:39.519193Z","iopub.execute_input":"2025-05-04T05:16:39.519406Z","iopub.status.idle":"2025-05-04T05:16:41.730867Z","shell.execute_reply.started":"2025-05-04T05:16:39.519389Z","shell.execute_reply":"2025-05-04T05:16:41.730147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.Series(y[400000:400100]).plot(figsize=(10,5), lw=1,title='Raw Audio Zoomed In Example',\n                                       color=color_pal[2])\n                  \nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:41.731679Z","iopub.execute_input":"2025-05-04T05:16:41.732122Z","iopub.status.idle":"2025-05-04T05:16:41.936885Z","shell.execute_reply.started":"2025-05-04T05:16:41.732100Z","shell.execute_reply":"2025-05-04T05:16:41.936201Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='top'></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style=\"background-color:#682F3F;font-family:newtimeroman;color:#FFF9ED;font-size:150%;text-align:center;border-radius:10px 10px;\">PLOTTING IN SPECTROGRAM</p> ","metadata":{}},{"cell_type":"code","source":"D = librosa.stft(y)\ns_db=librosa.amplitude_to_db(np.abs(D), ref=np.max)\ns_db.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:41.937517Z","iopub.execute_input":"2025-05-04T05:16:41.937726Z","iopub.status.idle":"2025-05-04T05:16:42.005149Z","shell.execute_reply.started":"2025-05-04T05:16:41.937710Z","shell.execute_reply":"2025-05-04T05:16:42.004434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot the trasformed audio data\nfig,ax = plt.subplots(figsize = (10,5))\nimg= librosa.display.specshow(s_db,\n                              x_axis='time',\n                              y_axis='log',\n                              ax=ax)\nax.set_title('Spectogram Example',fontsize=20)\nfig.colorbar(img,ax=ax,format=f'%0.2f')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:42.005830Z","iopub.execute_input":"2025-05-04T05:16:42.006028Z","iopub.status.idle":"2025-05-04T05:16:43.092421Z","shell.execute_reply.started":"2025-05-04T05:16:42.006012Z","shell.execute_reply":"2025-05-04T05:16:43.091728Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<a id='top'></a>\n<div class=\"list-group\" id=\"list-tab\" role=\"tablist\">\n<p style=\"background-color:#682F3F;font-family:newtimeroman;color:#FFF9ED;font-size:150%;text-align:center;border-radius:10px 10px;\">PLOTTING IN MEL SPECTROGRAM</p> ","metadata":{}},{"cell_type":"code","source":"S = librosa.feature.melspectrogram(y=y,sr=sr,n_mels=128,)\ns_db_mel=librosa.amplitude_to_db(S, ref=np.max)\n\nS.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:43.093224Z","iopub.execute_input":"2025-05-04T05:16:43.093522Z","iopub.status.idle":"2025-05-04T05:16:43.138454Z","shell.execute_reply.started":"2025-05-04T05:16:43.093499Z","shell.execute_reply":"2025-05-04T05:16:43.137872Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot the mel spectrogram\nfig,ax = plt.subplots(figsize = (15,5))\nimg= librosa.display.specshow(s_db_mel,\n                              x_axis='time',\n                              y_axis='log',\n                              ax=ax)\nax.set_title('Mel Spectogram Example',fontsize=20)\nfig.colorbar(img,ax=ax,format=f'%0.2f')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:43.139216Z","iopub.execute_input":"2025-05-04T05:16:43.139468Z","iopub.status.idle":"2025-05-04T05:16:43.534140Z","shell.execute_reply.started":"2025-05-04T05:16:43.139445Z","shell.execute_reply":"2025-05-04T05:16:43.533217Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"s_db_mel","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T05:16:43.534960Z","iopub.execute_input":"2025-05-04T05:16:43.535208Z","iopub.status.idle":"2025-05-04T05:16:43.540959Z","shell.execute_reply.started":"2025-05-04T05:16:43.535190Z","shell.execute_reply":"2025-05-04T05:16:43.540304Z"}},"outputs":[],"execution_count":null}]}