{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":19596,"databundleVersionId":1292430,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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)\nimport librosa\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\nimport os\nfor 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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-26T20:16:33.454600Z","iopub.execute_input":"2023-11-26T20:16:33.454957Z","iopub.status.idle":"2023-11-26T20:16:44.221515Z","shell.execute_reply.started":"2023-11-26T20:16:33.454927Z","shell.execute_reply":"2023-11-26T20:16:44.220209Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_path = \"/kaggle/input/birdsong-recognition/train_audio\"\n\npaths = [\"/kaggle/input/birdsong-recognition/train_audio/aldfly\",\n\"/kaggle/input/birdsong-recognition/train_audio/amegfi\",\n\"/kaggle/input/birdsong-recognition/train_audio/banswa\",\n\"/kaggle/input/birdsong-recognition/train_audio/bewwre\",\n\"/kaggle/input/birdsong-recognition/train_audio/bkhgro\"]\n\nextracted_file_paths = [os.listdir(i) for i in paths]\nextracted_files = []\nfor i in range(len(paths)):\n    extracted_files.append([paths[i]+'/'+f for f in extracted_file_paths[i]])  ","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:18:28.824292Z","iopub.execute_input":"2023-11-26T20:18:28.824657Z","iopub.status.idle":"2023-11-26T20:18:28.837650Z","shell.execute_reply.started":"2023-11-26T20:18:28.824625Z","shell.execute_reply":"2023-11-26T20:18:28.836273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_files = []\nfor i in extracted_files:\n    temp_loaded = []\n    for j in i:\n        y, sr = librosa.load(j, sr=11025)\n        temp_loaded.append(y)\n    loaded_files.append(temp_loaded)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:18:31.073521Z","iopub.execute_input":"2023-11-26T20:18:31.073838Z","iopub.status.idle":"2023-11-26T20:19:39.776981Z","shell.execute_reply.started":"2023-11-26T20:18:31.073815Z","shell.execute_reply":"2023-11-26T20:19:39.776281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig, ax = plt.subplots(nrows=1, sharex=True)\nlibrosa.display.waveshow(loaded_files[0][0], sr=sr)\nnp.array(y).shape","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:19:40.591633Z","iopub.execute_input":"2023-11-26T20:19:40.591979Z","iopub.status.idle":"2023-11-26T20:19:40.972624Z","shell.execute_reply.started":"2023-11-26T20:19:40.591957Z","shell.execute_reply":"2023-11-26T20:19:40.971502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(loaded_files[0][0])","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:45:04.168095Z","iopub.execute_input":"2023-11-26T20:45:04.168442Z","iopub.status.idle":"2023-11-26T20:45:04.175808Z","shell.execute_reply.started":"2023-11-26T20:45:04.168412Z","shell.execute_reply":"2023-11-26T20:45:04.174642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\n# https://ceur-ws.org/Vol-2125/paper_140.pdf\nsegment_length = 11025*3\nsr = 11025\ndef segmentation(audio):\n    number_of_segments = math.floor(len(audio)/segment_length)\n    segmented_audio = [audio[i*sr:(i+1)*sr] for i in range(number_of_segments)]\n    kept_segments = []\n    average_intensity = 1/1000000\n    for i in segmented_audio:\n        if sum([abs(n) for n in i]) > average_intensity*3*sr:\n           kept_segments.append(i) \n    return(kept_segments)\n\nall_samples = []\nfor category in loaded_files:\n    category_samples = []\n    for long_sample in category:\n        resampled = segmentation(long_sample)\n        for re in resampled:\n            category_samples.append(re)\n    all_samples.append(category_samples)\n\nall_samples","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:21:23.427605Z","iopub.execute_input":"2023-11-26T20:21:23.428011Z","iopub.status.idle":"2023-11-26T20:21:48.983727Z","shell.execute_reply.started":"2023-11-26T20:21:23.427981Z","shell.execute_reply":"2023-11-26T20:21:48.982100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fft_samples = []\nfor category in all_samples:\n    S = [np.abs(librosa.stft(y)) for y in category]\n    fft_samples.append(S)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:21:48.985773Z","iopub.execute_input":"2023-11-26T20:21:48.986403Z","iopub.status.idle":"2023-11-26T20:21:59.640528Z","shell.execute_reply.started":"2023-11-26T20:21:48.986375Z","shell.execute_reply":"2023-11-26T20:21:59.639136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(fft_samples[0][0])\nprint(fft_samples[0][0].shape)\nprint(all_samples[0][0])\nprint(len(all_samples[0][0]))\nprint(\"shape of fourier transform:\", fft_samples[0][1].shape)\nprint(\"shape of before ft:\",len(all_samples[0][1]))","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:21:59.642192Z","iopub.execute_input":"2023-11-26T20:21:59.642670Z","iopub.status.idle":"2023-11-26T20:21:59.652322Z","shell.execute_reply.started":"2023-11-26T20:21:59.642629Z","shell.execute_reply":"2023-11-26T20:21:59.651027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[0][1],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[0][11],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[0][21],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[0][31],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[0][100],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:22:01.786355Z","iopub.execute_input":"2023-11-26T20:22:01.787698Z","iopub.status.idle":"2023-11-26T20:22:03.746585Z","shell.execute_reply.started":"2023-11-26T20:22:01.787653Z","shell.execute_reply":"2023-11-26T20:22:03.744890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[1][1],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[1][11],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[1][21],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[1][31],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:22:03.748766Z","iopub.execute_input":"2023-11-26T20:22:03.749260Z","iopub.status.idle":"2023-11-26T20:22:05.299990Z","shell.execute_reply.started":"2023-11-26T20:22:03.749227Z","shell.execute_reply":"2023-11-26T20:22:05.298885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[2][1],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[2][11],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[2][21],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[2][31],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")","metadata":{"execution":{"iopub.status.busy":"2023-11-26T17:31:50.857056Z","iopub.execute_input":"2023-11-26T17:31:50.857460Z","iopub.status.idle":"2023-11-26T17:31:52.829854Z","shell.execute_reply.started":"2023-11-26T17:31:50.857430Z","shell.execute_reply":"2023-11-26T17:31:52.828700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[3][1],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[3][2],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[3][3],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")\n\nfig, ax = plt.subplots()\nimg = librosa.display.specshow(librosa.amplitude_to_db(fft_samples[3][4],\n                                                       ref=np.max),\n                               y_axis='log', x_axis='time', ax=ax)\nax.set_title('Power spectrogram')\nfig.colorbar(img, ax=ax, format=\"%+2.0f dB\")","metadata":{"execution":{"iopub.status.busy":"2023-11-26T17:32:36.837715Z","iopub.execute_input":"2023-11-26T17:32:36.838124Z","iopub.status.idle":"2023-11-26T17:32:38.898962Z","shell.execute_reply.started":"2023-11-26T17:32:36.838094Z","shell.execute_reply":"2023-11-26T17:32:38.897605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.fft import fft, ifft, fftfreq\nscipy_fft_samples = []\nfor category in all_samples:\n    S = [fft(y) for y in category]\n    scipy_fft_samples.append(S)","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:22:05.301295Z","iopub.execute_input":"2023-11-26T20:22:05.301554Z","iopub.status.idle":"2023-11-26T20:22:07.388933Z","shell.execute_reply.started":"2023-11-26T20:22:05.301532Z","shell.execute_reply":"2023-11-26T20:22:07.387524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(scipy_fft_samples[0][0])\nprint(scipy_fft_samples[0][0].shape)\nprint(all_samples[0][0])\nprint(len(all_samples[0][0]))\nprint(\"shape of fourier transform:\", scipy_fft_samples[0][1].shape)\nprint(\"shape of before ft:\",len(all_samples[0][1]))","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:22:07.390148Z","iopub.execute_input":"2023-11-26T20:22:07.390458Z","iopub.status.idle":"2023-11-26T20:22:07.399496Z","shell.execute_reply.started":"2023-11-26T20:22:07.390430Z","shell.execute_reply":"2023-11-26T20:22:07.397869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 600\n# sample spacing\nT = 1.0 / 800.0\nxf = fftfreq(N, T)[:N//2]\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[0][0][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[0][15][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[0][25][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[0][40][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[0][60][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[0][25][0:N//2]))\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:22:07.400727Z","iopub.execute_input":"2023-11-26T20:22:07.401114Z","iopub.status.idle":"2023-11-26T20:22:08.403364Z","shell.execute_reply.started":"2023-11-26T20:22:07.401081Z","shell.execute_reply":"2023-11-26T20:22:08.402173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[1][0][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[1][15][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[1][25][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[0][65][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[0][87][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[0][12][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[0][120][0:N//2]))\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:22:35.618198Z","iopub.execute_input":"2023-11-26T20:22:35.618591Z","iopub.status.idle":"2023-11-26T20:22:36.883102Z","shell.execute_reply.started":"2023-11-26T20:22:35.618561Z","shell.execute_reply":"2023-11-26T20:22:36.881631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[2][0][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[2][15][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[2][25][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[2][45][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[2][86][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[2][91][0:N//2]))\nplt.grid()\nplt.show()\n\nplt.plot(xf, 2.0/N * np.abs(scipy_fft_samples[2][120][0:N//2]))\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-26T20:22:55.811166Z","iopub.execute_input":"2023-11-26T20:22:55.811546Z","iopub.status.idle":"2023-11-26T20:22:57.137198Z","shell.execute_reply.started":"2023-11-26T20:22:55.811517Z","shell.execute_reply":"2023-11-26T20:22:57.136140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}