{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom IPython.display import Audio,display\nimport librosa","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"### it still feels amazing to me, the variety of sounds we hear can be each represented by a single time series"},{"metadata":{"trusted":true},"cell_type":"code","source":"sr    = 48000 # sample rate\n\n# do re mi fa sol la ti do, my MUSIC 117 works!\ndo    = 261.6\nseq   = np.exp([np.log(do) + np.log(2)/12 * x for x in np.cumsum([0,2,2,1,2,2,2,1])])\nwav   = 4e-1 * np.concatenate([np.sin( 2 * np.pi/sr * s * np.arange(sr)) for s in seq])\n\nplt.figure(figsize=(15,5))\nplt.plot(wav,'.-')                              \nplt.axvline(sr,color='r',ls='-')\nplt.xlim(int(sr*0.99),int(sr*1.01))\nplt.title('wave plot at the transition between \"do\" and \"re\"')\nplt.xlabel('sample')\nplt.ylabel('pressure')\nplt.grid(True)\n\ndisplay(Audio( wav ,rate=sr,normalize=False))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### By a pretrained human brain, many species' characteristics can be learnt by less than 20 clips."},{"metadata":{"trusted":true},"cell_type":"code","source":"train_tp = pd.read_csv('/kaggle/input/rfcx-species-audio-detection/train_fp.csv')\ntrain_tp.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"for sid,df in train_tp.groupby('species_id'):\n    print('='*10 + f'species_id {sid}' + '='*10)\n    agg = []\n    for index,row in df.sample(20).iterrows():\n        wav, sr = librosa.load(f'/kaggle/input/rfcx-species-audio-detection/train/{row[\"recording_id\"]}.flac', sr=None)\n        clip    = wav[int(row[\"t_min\"]*sr):int(row[\"t_max\"]*sr)]\n        agg.append(clip / np.max(np.abs(clip)) * 2)        \n        agg.append(np.sin( 2 * np.pi/sr * 8 * do * np.arange(int(sr/10)))) # beep\n    display(Audio(np.concatenate(agg),rate=sr))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}