{"metadata": {"language_info": {"codemirror_mode": {"version": 3, "name": "ipython"}, "file_extension": ".py", "version": "3.6.3", "nbconvert_exporter": "python", "name": "python", "pygments_lexer": "ipython3", "mimetype": "text/x-python"}, "kernelspec": {"display_name": "Python 3", "name": "python3", "language": "python"}}, "cells": [{"outputs": [], "metadata": {"_cell_guid": "93e1a992-9e00-43a9-ad90-88d9f58f011c", "collapsed": true, "_uuid": "46a550f6614d4f36b3e217220195037044f94c79"}, "cell_type": "code", "execution_count": null, "source": ["import os\n", "from pathlib import Path\n", "from subprocess import check_output\n", "\n", "import numpy as np\n", "import pandas as pd\n", "from scipy import signal\n", "from scipy.io import wavfile\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "%matplotlib inline"]}, {"metadata": {"_cell_guid": "1d5151ac-3d17-4c2f-bfd6-152ab1ff3ce8", "_uuid": "fdbb4dd03ec17d6641ded1c13b68e6919875bb02"}, "cell_type": "markdown", "source": ["Check data folders:"]}, {"outputs": [], "metadata": {"_cell_guid": "08c47377-b5ae-4449-9925-17e172b68103", "_uuid": "33ddba4d8969d3f1a3738aac9b19fda7679cdba5"}, "cell_type": "code", "execution_count": null, "source": ["print(check_output([\"ls\", \"../input\"]))\n", "print(check_output([\"ls\", \"../input/train\"]).decode(\"utf8\"))\n", "folders = os.listdir(\"../input/train/audio\")\n", "print(folders)"]}, {"metadata": {"_cell_guid": "c6cf6efc-0862-430b-a4cb-7dfc725c237d", "_uuid": "1fcaa47af409befc11bffa21c0be4c8c29e36f14"}, "cell_type": "markdown", "source": ["Load labels and wav file paths into dataframe:"]}, {"outputs": [], "metadata": {"_cell_guid": "b11f1eba-20e6-452b-a7a2-a126e6d2b792", "_uuid": "ed2837d2cb4706932d1a9d0f47b7acc2b3830c83"}, "cell_type": "code", "execution_count": null, "source": ["train_audio_path = '../input/train/audio'\n", "train_labels = os.listdir(train_audio_path)\n", "print(f'Number of labels: {len(train_labels)}')\n", "\n", "wavs = []\n", "labels = []\n", "for label in train_labels:\n", "    if label == '_background_noise_':\n", "        continue\n", "    files = os.listdir(train_audio_path + '/' + label)\n", "    for f in files:\n", "        if not f.endswith('wav'):\n", "            continue\n", "        wavs.append(f)\n", "        labels.append(label)\n", "\n", "train = pd.DataFrame({'file':wavs,'label':labels})\n", "train.info()"]}, {"metadata": {"_cell_guid": "63892d86-f66b-4080-ac2f-88e91098a73f", "_uuid": "4eb4d875129f70184c8c41ed6738e762f9c04c49"}, "cell_type": "markdown", "source": ["## Labels\n", "Explore label frequencies"]}, {"outputs": [], "metadata": {"_cell_guid": "63268589-1b44-40ab-900c-8403fb672a00", "scrolled": true, "_uuid": "b96004367170788b0c02d254b43868357bca0db7"}, "cell_type": "code", "execution_count": null, "source": ["fig, ax = plt.subplots(figsize=(16, 8))\n", "sns.countplot(ax=ax, x=\"label\", data=train)\n", "print(train.label.unique())"]}, {"metadata": {"_cell_guid": "e5347b7e-7078-43fb-8df5-7c1ee911755d", "_uuid": "fb8dcd43f7335578b8cf157ccea0b300d3c77ffe"}, "cell_type": "markdown", "source": ["## Spectrograms"]}, {"outputs": [], "metadata": {"_cell_guid": "900e96d6-57f7-4af7-b2cf-27879007f88f", "collapsed": true, "_uuid": "414e84f9949d90ff4a3a64c9e2dca6d5ad9d0229"}, "cell_type": "code", "execution_count": null, "source": ["def spectrogram(file, label):\n", "    eps=1e-10\n", "    sample_rate, samples = wavfile.read(str(train_audio_path) + '/' + label + '/' + file)\n", "    frequencies, times, spectrogram = signal.stft(samples, sample_rate, nperseg = sample_rate/50, noverlap = sample_rate/75)\n", "    return np.log(np.abs(spectrogram).T+eps)"]}, {"metadata": {"_cell_guid": "4d8959e9-479f-4fee-bfa7-9164acc929ea", "_uuid": "d86332206505f590de7a755d04a1c189f94ab606"}, "cell_type": "markdown", "source": ["Explore the spectrograms for each label:"]}, {"outputs": [], "metadata": {"_kg_hide-output": false, "_cell_guid": "11ddbaef-f188-4cee-83b0-2fd754d547fd", "_uuid": "321f56d981faa1f65646d651f43da055353f07b2"}, "cell_type": "code", "execution_count": null, "source": ["num_samples = 5\n", "labels = train.label.unique()\n", "fig, axes = plt.subplots(len(labels),num_samples, figsize = (16, len(labels)*4))\n", "for i,label in enumerate(labels):\n", "    files = train[train.label==label].file.sample(num_samples)\n", "    axes[i][0].set_title(label)\n", "    for j, file in enumerate(files):\n", "        specgram = spectrogram(file, label)\n", "        axes[i][j].axis('off')\n", "        axes[i][j].matshow(specgram)"]}], "nbformat_minor": 1, "nbformat": 4}