{"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":"# 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 torch\nimport librosa\nimport soundfile as sf\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\n'''","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-30T15:36:50.975645Z","iopub.execute_input":"2022-04-30T15:36:50.976012Z","iopub.status.idle":"2022-04-30T15:36:54.503077Z","shell.execute_reply.started":"2022-04-30T15:36:50.975917Z","shell.execute_reply":"2022-04-30T15:36:54.501944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport ast\nimport random\nimport numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nfrom tqdm import tqdm\nimport torchaudio\nimport IPython.display as ipd\nfrom collections import Counter\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.metrics import f1_score\n\nimport torch\nimport torch.nn as nn\nfrom torch.optim import Adam\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import models\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:36:54.506355Z","iopub.execute_input":"2022-04-30T15:36:54.507048Z","iopub.status.idle":"2022-04-30T15:36:54.838083Z","shell.execute_reply.started":"2022-04-30T15:36:54.507005Z","shell.execute_reply":"2022-04-30T15:36:54.836952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"''''\nfiles = []\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        files.append(os.path.join(dirname, filename))\n'''\n","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:36:54.839728Z","iopub.execute_input":"2022-04-30T15:36:54.839952Z","iopub.status.idle":"2022-04-30T15:36:54.846225Z","shell.execute_reply.started":"2022-04-30T15:36:54.839922Z","shell.execute_reply":"2022-04-30T15:36:54.845438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\naudio_1 = \"../input/birdclef-2022/train_audio/afrsil1/XC209513.ogg\"\naudio_2 = \"../input/birdclef-2022/train_audio/afrsil1/XC317039.ogg\"\n\naudio1, sr1 = librosa.load(audio_1, sr= 32000, mono=True)\naudio2, sr2 = librosa.load(audio_2, sr= 32000, mono=True)\nprint(audio1)\nprint(audio2)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:36:54.848671Z","iopub.execute_input":"2022-04-30T15:36:54.849209Z","iopub.status.idle":"2022-04-30T15:36:55.115416Z","shell.execute_reply.started":"2022-04-30T15:36:54.849176Z","shell.execute_reply":"2022-04-30T15:36:55.114263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy import signal\ncorr = signal.correlate(audio1, audio2)*100","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:36:55.116986Z","iopub.execute_input":"2022-04-30T15:36:55.117367Z","iopub.status.idle":"2022-04-30T15:36:55.352685Z","shell.execute_reply.started":"2022-04-30T15:36:55.117318Z","shell.execute_reply":"2022-04-30T15:36:55.351513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(corr)","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:36:55.353864Z","iopub.execute_input":"2022-04-30T15:36:55.354111Z","iopub.status.idle":"2022-04-30T15:36:55.360336Z","shell.execute_reply.started":"2022-04-30T15:36:55.354071Z","shell.execute_reply":"2022-04-30T15:36:55.359286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clock = np.arange(64, len(audio2), 128)\nfig, (ax_orig, ax_noise, ax_corr) = plt.subplots(3, 1, sharex=True)\nax_orig.plot(audio2)\nax_orig.set_title('Audio2')\nax_noise.plot(audio1)\nax_noise.set_title('Audio1')\nax_corr.plot(corr)\nax_corr.set_title('Cross-correlated with rectangular pulse')\nax_orig.margins(0, 0.1)\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:36:55.361729Z","iopub.execute_input":"2022-04-30T15:36:55.361982Z","iopub.status.idle":"2022-04-30T15:36:56.641824Z","shell.execute_reply.started":"2022-04-30T15:36:55.361949Z","shell.execute_reply":"2022-04-30T15:36:56.640865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = []\nimport os\nfor dirname, _, filenames in os.walk('../input/birdclef-2022/train_audio/afrsil1/'):\n    for filename in filenames:\n        files.append(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:36:56.643216Z","iopub.execute_input":"2022-04-30T15:36:56.643543Z","iopub.status.idle":"2022-04-30T15:36:56.656179Z","shell.execute_reply.started":"2022-04-30T15:36:56.643496Z","shell.execute_reply":"2022-04-30T15:36:56.655401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:36:56.657473Z","iopub.execute_input":"2022-04-30T15:36:56.657994Z","iopub.status.idle":"2022-04-30T15:36:56.664324Z","shell.execute_reply.started":"2022-04-30T15:36:56.657942Z","shell.execute_reply":"2022-04-30T15:36:56.663232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"avg = [0]*64000","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:36:56.667578Z","iopub.execute_input":"2022-04-30T15:36:56.667799Z","iopub.status.idle":"2022-04-30T15:36:56.676415Z","shell.execute_reply.started":"2022-04-30T15:36:56.667771Z","shell.execute_reply":"2022-04-30T15:36:56.675402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in files:\n    audio, sr = librosa.load(i, sr= 64000, mono=True)\n    print(audio)\n    avg+=audio[0:64000]","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:36:56.677804Z","iopub.execute_input":"2022-04-30T15:36:56.678044Z","iopub.status.idle":"2022-04-30T15:37:28.04295Z","shell.execute_reply.started":"2022-04-30T15:36:56.678012Z","shell.execute_reply":"2022-04-30T15:37:28.041957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"avg","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:37:28.044242Z","iopub.execute_input":"2022-04-30T15:37:28.044463Z","iopub.status.idle":"2022-04-30T15:37:28.051624Z","shell.execute_reply.started":"2022-04-30T15:37:28.044433Z","shell.execute_reply":"2022-04-30T15:37:28.050647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax_orig, ax_noise, ax_corr) = plt.subplots(3, 1, sharex=True)\nax_orig.plot(audio2[0:64000])\nax_orig.set_title('Audio2')\nax_noise.plot(audio1[0:64000])\nax_noise.set_title('Audio1')\nax_corr.plot(avg)\nax_corr.set_title('Average')\nax_orig.margins(0, 0.1)\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:37:28.053067Z","iopub.execute_input":"2022-04-30T15:37:28.053521Z","iopub.status.idle":"2022-04-30T15:37:28.546045Z","shell.execute_reply.started":"2022-04-30T15:37:28.053491Z","shell.execute_reply":"2022-04-30T15:37:28.545109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"myfile = \"../input/birdclef-2022/train_audio/afrsil1/XC125458.ogg\"","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:37:28.54723Z","iopub.execute_input":"2022-04-30T15:37:28.547443Z","iopub.status.idle":"2022-04-30T15:37:28.552084Z","shell.execute_reply.started":"2022-04-30T15:37:28.547414Z","shell.execute_reply":"2022-04-30T15:37:28.550878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy.io import wavfile as wav\nfrom scipy.fft import rfft\naudio, sr = librosa.load(myfile, sr= 32000, mono=True)\nfft_out  = rfft(audio)\nfft_out = fft_out[0:12000]\nplt.plot(fft_out[0:12000])\nplt.show","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:37:28.553278Z","iopub.execute_input":"2022-04-30T15:37:28.553578Z","iopub.status.idle":"2022-04-30T15:37:28.934939Z","shell.execute_reply.started":"2022-04-30T15:37:28.55355Z","shell.execute_reply":"2022-04-30T15:37:28.934062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fft_out","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:54:17.923358Z","iopub.execute_input":"2022-04-30T15:54:17.92371Z","iopub.status.idle":"2022-04-30T15:54:17.931966Z","shell.execute_reply.started":"2022-04-30T15:54:17.923669Z","shell.execute_reply":"2022-04-30T15:54:17.931284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(audio)\nplt.show","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:37:28.936058Z","iopub.execute_input":"2022-04-30T15:37:28.936292Z","iopub.status.idle":"2022-04-30T15:37:29.281927Z","shell.execute_reply.started":"2022-04-30T15:37:28.936264Z","shell.execute_reply":"2022-04-30T15:37:29.281255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clips_arr = []\nplt.figure(figsize=(100,50))\nfor i in range(0,11):\n    fft_out1  = rfft(audio[i*32000:(i+1)*32000])\n    clips_arr.append(fft_out1)\n    plt.subplot(3,4,i+1)\n    plt.plot(fft_out1[0:1000])\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:38:45.045073Z","iopub.execute_input":"2022-04-30T15:38:45.045384Z","iopub.status.idle":"2022-04-30T15:38:47.686654Z","shell.execute_reply.started":"2022-04-30T15:38:45.045353Z","shell.execute_reply":"2022-04-30T15:38:47.685632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clips_arr","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:38:56.940834Z","iopub.execute_input":"2022-04-30T15:38:56.94111Z","iopub.status.idle":"2022-04-30T15:38:56.952631Z","shell.execute_reply.started":"2022-04-30T15:38:56.94108Z","shell.execute_reply":"2022-04-30T15:38:56.951957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:46:42.566406Z","iopub.execute_input":"2022-04-30T15:46:42.567263Z","iopub.status.idle":"2022-04-30T15:46:42.574354Z","shell.execute_reply.started":"2022-04-30T15:46:42.567227Z","shell.execute_reply":"2022-04-30T15:46:42.573295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy import stats\nbins = stats.binned_statistic(np.array(fft_out).astype(int), np.array(fft_out).astype(int), 'count', bins=10000,range = [(0,400)])","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:03:10.425792Z","iopub.execute_input":"2022-04-30T16:03:10.426152Z","iopub.status.idle":"2022-04-30T16:03:10.43451Z","shell.execute_reply.started":"2022-04-30T16:03:10.4261Z","shell.execute_reply":"2022-04-30T16:03:10.43317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bins","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:03:12.228963Z","iopub.execute_input":"2022-04-30T16:03:12.229259Z","iopub.status.idle":"2022-04-30T16:03:12.236698Z","shell.execute_reply.started":"2022-04-30T16:03:12.229228Z","shell.execute_reply":"2022-04-30T16:03:12.235648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(bins[0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:03:15.047933Z","iopub.execute_input":"2022-04-30T16:03:15.048626Z","iopub.status.idle":"2022-04-30T16:03:15.263055Z","shell.execute_reply.started":"2022-04-30T16:03:15.048582Z","shell.execute_reply":"2022-04-30T16:03:15.262192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = []\nfor i in range(-200,200):\n    temp.append(i)","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:07:22.669027Z","iopub.execute_input":"2022-04-30T16:07:22.669359Z","iopub.status.idle":"2022-04-30T16:07:22.674699Z","shell.execute_reply.started":"2022-04-30T16:07:22.669326Z","shell.execute_reply":"2022-04-30T16:07:22.673607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = np.array(temp)/2","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:07:24.516106Z","iopub.execute_input":"2022-04-30T16:07:24.516445Z","iopub.status.idle":"2022-04-30T16:07:24.520967Z","shell.execute_reply.started":"2022-04-30T16:07:24.516412Z","shell.execute_reply":"2022-04-30T16:07:24.520312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(fft_out, temp)","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:07:26.374324Z","iopub.execute_input":"2022-04-30T16:07:26.374629Z","iopub.status.idle":"2022-04-30T16:07:27.583419Z","shell.execute_reply.started":"2022-04-30T16:07:26.37459Z","shell.execute_reply":"2022-04-30T16:07:27.582315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 500\n# sample spacing\nT = 0.1\nx = np.linspace(0.0, (N-1)*T, N)\ny = 5*np.sin(x) + np.cos(2*np.pi*x) \nW= fft_out(audio.size, d=x[1]-x[0])\n\ncut_f_signal = f_signal.copy()\ncut_f_signal[(W>0.6)] = 0 ","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:12:00.076107Z","iopub.execute_input":"2022-04-30T16:12:00.076561Z","iopub.status.idle":"2022-04-30T16:12:00.101419Z","shell.execute_reply.started":"2022-04-30T16:12:00.076529Z","shell.execute_reply":"2022-04-30T16:12:00.100435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-04-30T15:37:32.047823Z","iopub.status.idle":"2022-04-30T15:37:32.048173Z","shell.execute_reply.started":"2022-04-30T15:37:32.047967Z","shell.execute_reply":"2022-04-30T15:37:32.047985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}