{"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)\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\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":"2021-05-26T07:09:04.479387Z","iopub.execute_input":"2021-05-26T07:09:04.479762Z","iopub.status.idle":"2021-05-26T07:09:11.418362Z","shell.execute_reply.started":"2021-05-26T07:09:04.479731Z","shell.execute_reply":"2021-05-26T07:09:11.417412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport matplotlib.image as mpimg\nfrom matplotlib.offsetbox import AnnotationBbox, OffsetImage\n\n# Map 1 library\nimport plotly.express as px\n\n# Map 2 libraries\nimport descartes\nimport geopandas as gpd\nfrom shapely.geometry import Point, Polygon\n\n# Librosa Libraries\nimport librosa\nimport librosa.display\nimport IPython.display as ipd","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:06:44.854224Z","iopub.execute_input":"2021-05-26T07:06:44.854625Z","iopub.status.idle":"2021-05-26T07:06:49.565226Z","shell.execute_reply.started":"2021-05-26T07:06:44.854594Z","shell.execute_reply":"2021-05-26T07:06:49.564183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import data\ntrain_csv = pd.read_csv(\"../input/birdsong-recognition/train.csv\")\ntest_csv = pd.read_csv(\"../input/birdsong-recognition/test.csv\")\n\n# Create some time features\ntrain_csv['year'] = train_csv['date'].apply(lambda x: x.split('-')[0])\ntrain_csv['month'] = train_csv['date'].apply(lambda x: x.split('-')[1])\ntrain_csv['day_of_month'] = train_csv['date'].apply(lambda x: x.split('-')[2])\n\nprint(\"There are {:,} unique bird species in the dataset.\".format(len(train_csv['species'].unique())))","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:12:31.739676Z","iopub.execute_input":"2021-05-26T07:12:31.740189Z","iopub.status.idle":"2021-05-26T07:12:32.327068Z","shell.execute_reply.started":"2021-05-26T07:12:31.740123Z","shell.execute_reply":"2021-05-26T07:12:32.325866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing the libraries that we will use to analyze the audio data\nimport numpy as np \nimport pandas as pd \nimport os","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:13:34.939961Z","iopub.execute_input":"2021-05-26T07:13:34.940434Z","iopub.status.idle":"2021-05-26T07:13:34.945934Z","shell.execute_reply.started":"2021-05-26T07:13:34.940399Z","shell.execute_reply":"2021-05-26T07:13:34.944583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Importing Librosa Libraries\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\n\nimport sklearn\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:13:51.659488Z","iopub.execute_input":"2021-05-26T07:13:51.659938Z","iopub.status.idle":"2021-05-26T07:13:51.666337Z","shell.execute_reply.started":"2021-05-26T07:13:51.659898Z","shell.execute_reply":"2021-05-26T07:13:51.664841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loading an audio file\naudio_sample_point = '/kaggle/input/birdsong-recognition/train_audio/nutwoo/XC462016.mp3'\nx , sr = librosa.load(audio_sample_point)\nprint(type(x), type(sr))\nprint(x.shape, sr)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:14:05.544723Z","iopub.execute_input":"2021-05-26T07:14:05.545112Z","iopub.status.idle":"2021-05-26T07:14:10.486206Z","shell.execute_reply.started":"2021-05-26T07:14:05.545081Z","shell.execute_reply":"2021-05-26T07:14:10.485268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Resampling the audio to 44.1 kHz\nlibrosa.load(audio_sample_point, sr=44100)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:14:27.939749Z","iopub.execute_input":"2021-05-26T07:14:27.940164Z","iopub.status.idle":"2021-05-26T07:14:28.242963Z","shell.execute_reply.started":"2021-05-26T07:14:27.940116Z","shell.execute_reply":"2021-05-26T07:14:28.241995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(audio_sample_point)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:14:40.434767Z","iopub.execute_input":"2021-05-26T07:14:40.435199Z","iopub.status.idle":"2021-05-26T07:14:40.494383Z","shell.execute_reply.started":"2021-05-26T07:14:40.435136Z","shell.execute_reply":"2021-05-26T07:14:40.493332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualizing the time waveform of audio_sample_data\n%matplotlib inline\nimport matplotlib.pyplot as plt\nplt.figure(figsize=(16, 5))\nlibrosa.display.waveplot(x, sr=sr)","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:14:55.850336Z","iopub.execute_input":"2021-05-26T07:14:55.850785Z","iopub.status.idle":"2021-05-26T07:14:56.266056Z","shell.execute_reply.started":"2021-05-26T07:14:55.850750Z","shell.execute_reply":"2021-05-26T07:14:56.264939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Converting to spectogram\nSample = librosa.stft(x)\nSampledb = librosa.amplitude_to_db(abs(Sample))\nplt.figure(figsize=(16, 5))\nlibrosa.display.specshow(Sampledb, sr=sr, x_axis='time', y_axis='hz')\nplt.colorbar()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:15:07.500016Z","iopub.execute_input":"2021-05-26T07:15:07.500391Z","iopub.status.idle":"2021-05-26T07:15:09.830775Z","shell.execute_reply.started":"2021-05-26T07:15:07.500359Z","shell.execute_reply":"2021-05-26T07:15:09.829860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Since most of the activity is taking place at the bottom of the Spectogram,\n#I am changing the frequency scale to log scale\nlibrosa.display.specshow(Sampledb, sr=sr, x_axis='time', y_axis='log')\nplt.colorbar()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:15:26.559284Z","iopub.execute_input":"2021-05-26T07:15:26.559862Z","iopub.status.idle":"2021-05-26T07:15:29.054011Z","shell.execute_reply.started":"2021-05-26T07:15:26.559825Z","shell.execute_reply":"2021-05-26T07:15:29.053078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# I am creating a sample audio signal\nsr = 22050 # sample rate (defaut)\nT = 5.0    # seconds\nt = np.linspace(0, T, int(T*sr), endpoint=False) # defining time variable for plotting\nx = 0.5*np.sin(2*np.pi*220*t)  # pure sine wave at 220 Hz\n#Playing the audio\nipd.Audio(x, rate=sr) # load a NumPy array\n#Saving the audio\n# I am using soundfile because Librosa has removed output function from its latest version\nimport soundfile as sf\nsf.write('tone_sample.wav', np.random.randn(10, 2), 44100, 'PCM_24')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:15:43.614696Z","iopub.execute_input":"2021-05-26T07:15:43.615043Z","iopub.status.idle":"2021-05-26T07:15:43.636503Z","shell.execute_reply.started":"2021-05-26T07:15:43.615013Z","shell.execute_reply":"2021-05-26T07:15:43.635282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn\nspectral_centroids = librosa.feature.spectral_centroid(x, sr=sr)[0] #returns the numbers of frame as number of columns in the array\nspectral_centroids.shape\n# Computing the time variable for visualization\nplt.figure(figsize=(12, 4))\nframes = range(len(spectral_centroids))\nt = librosa.frames_to_time(frames)\n# Normalising the spectral centroid for visualisation\ndef normalize(x, axis=0):\n    return sklearn.preprocessing.minmax_scale(x, axis=axis)\n#Plotting the Spectral Centroid along the waveform\nlibrosa.display.waveplot(x, sr=sr, alpha=0.4)\nplt.plot(t, normalize(spectral_centroids), color='b')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:15:59.149634Z","iopub.execute_input":"2021-05-26T07:15:59.150207Z","iopub.status.idle":"2021-05-26T07:15:59.375240Z","shell.execute_reply.started":"2021-05-26T07:15:59.150138Z","shell.execute_reply":"2021-05-26T07:15:59.374203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# computing the spectral rolloff for each frame \nspectral_rolloff = librosa.feature.spectral_rolloff(x+0.01, sr=sr)[0]\nplt.figure(figsize=(12, 4))\nlibrosa.display.waveplot(x, sr=sr, alpha=0.4)\nplt.plot(t, normalize(spectral_rolloff), color='r')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:16:10.519797Z","iopub.execute_input":"2021-05-26T07:16:10.520414Z","iopub.status.idle":"2021-05-26T07:16:10.738335Z","shell.execute_reply.started":"2021-05-26T07:16:10.520358Z","shell.execute_reply":"2021-05-26T07:16:10.737265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Computing the spectral bandwidth (between the two verticsl red lines in the frame)\nspectral_bandwidth_2 = librosa.feature.spectral_bandwidth(x+0.01, sr=sr)[0]\nspectral_bandwidth_3 = librosa.feature.spectral_bandwidth(x+0.01, sr=sr, p=3)[0]\nspectral_bandwidth_4 = librosa.feature.spectral_bandwidth(x+0.01, sr=sr, p=4)[0]\nplt.figure(figsize=(15, 9))\nlibrosa.display.waveplot(x, sr=sr, alpha=0.4)\nplt.plot(t, normalize(spectral_bandwidth_2), color='r')\nplt.plot(t, normalize(spectral_bandwidth_3), color='g')\nplt.plot(t, normalize(spectral_bandwidth_4), color='y')\nplt.legend(('p = 2', 'p = 3', 'p = 4'))","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:16:24.745806Z","iopub.execute_input":"2021-05-26T07:16:24.746229Z","iopub.status.idle":"2021-05-26T07:16:25.125491Z","shell.execute_reply.started":"2021-05-26T07:16:24.746196Z","shell.execute_reply":"2021-05-26T07:16:25.124415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x, sr = librosa.load(audio_sample_point)\n#Plot the signal:\nplt.figure(figsize=(14, 5))\nlibrosa.display.waveplot(x, sr=sr)\n# Zooming in\nn0 = 9000\nn1 = 9100\nplt.figure(figsize=(14, 5))\nplt.plot(x[n0:n1])\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:16:38.745017Z","iopub.execute_input":"2021-05-26T07:16:38.745584Z","iopub.status.idle":"2021-05-26T07:16:42.863251Z","shell.execute_reply.started":"2021-05-26T07:16:38.745549Z","shell.execute_reply":"2021-05-26T07:16:42.862180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting MFCCs\nfs=10\nmfccs = librosa.feature.mfcc(x, sr=fs)\nprint(mfccs.shape)\n(20, 97)\n#Displaying  the MFCCs:\nplt.figure(figsize=(15, 7))\nlibrosa.display.specshow(mfccs, sr=sr, x_axis='time')","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:17:03.580413Z","iopub.execute_input":"2021-05-26T07:17:03.580865Z","iopub.status.idle":"2021-05-26T07:17:03.973391Z","shell.execute_reply.started":"2021-05-26T07:17:03.580831Z","shell.execute_reply":"2021-05-26T07:17:03.972363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checking diversity in duration of audios\ntrain_csv['duration_interval'] = \">500\"\ntrain_csv.loc[train_csv['duration'] <= 100, 'duration_interval'] = \"<=100\"\ntrain_csv.loc[(train_csv['duration'] > 100) & (train_csv['duration'] <= 200), 'duration_interval'] = \"100-200\"\ntrain_csv.loc[(train_csv['duration'] > 200) & (train_csv['duration'] <= 300), 'duration_interval'] = \"200-300\"\ntrain_csv.loc[(train_csv['duration'] > 300) & (train_csv['duration'] <= 400), 'duration_interval'] = \"300-400\"\ntrain_csv.loc[(train_csv['duration'] > 400) & (train_csv['duration'] <= 500), 'duration_interval'] = \"400-500\"\n\nbird = mpimg.imread('../input/birdcall-recognition-data/multicolor bird.jpg')\nimagebox = OffsetImage(bird, zoom=0.4)\nxy = (0.5, 0.7)\nab = AnnotationBbox(imagebox, xy, frameon=False, pad=1, xybox=(4.4, 12000))\n\nplt.figure(figsize=(16, 6))\nax = sns.countplot(train_csv['duration_interval'], palette=\"hls\")\nax.add_artist(ab)\nplt.title(\"Distribution of Recordings Duration\", fontsize=16)\nplt.ylabel(\"Frequency\", fontsize=14)\nplt.yticks(fontsize=13)\nplt.xticks(rotation=45, fontsize=13)\nplt.xlabel(\"\");","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:19:05.432274Z","iopub.execute_input":"2021-05-26T07:19:05.432925Z","iopub.status.idle":"2021-05-26T07:19:05.758452Z","shell.execute_reply.started":"2021-05-26T07:19:05.432879Z","shell.execute_reply":"2021-05-26T07:19:05.756857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Top 15 most common elevations for the birds whose recordings we have\ntop_15 = list(train_csv['country'].value_counts().head(15).reset_index()['index'])\ndata = train_csv[train_csv['country'].isin(top_15)]\nbird = mpimg.imread('../input/birdcall-recognition-data/fluff ball.jpg')\nimagebox = OffsetImage(bird, zoom=0.6)\nxy = (0.5, 0.7)\nab = AnnotationBbox(imagebox, xy, frameon=False, pad=1, xybox=(12.2, 7000))\n\nplt.figure(figsize=(16, 6))\nax = sns.countplot(data['country'], palette='hls', order = data['country'].value_counts().index)\nax.add_artist(ab)\n\nplt.title(\"Top 15 Countries with most Recordings\", fontsize=16)\nplt.ylabel(\"Frequency\", fontsize=14)\nplt.yticks(fontsize=13)\nplt.xticks(rotation=45, fontsize=13)\nplt.xlabel(\"\");","metadata":{"execution":{"iopub.status.busy":"2021-05-26T07:21:42.269864Z","iopub.execute_input":"2021-05-26T07:21:42.270286Z","iopub.status.idle":"2021-05-26T07:21:42.884953Z","shell.execute_reply.started":"2021-05-26T07:21:42.270252Z","shell.execute_reply":"2021-05-26T07:21:42.883646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}