Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ravising-h authored Jun 4, 2019
1 parent e7285f6 commit 30e6ed9
Showing 1 changed file with 347 additions and 0 deletions.
347 changes: 347 additions & 0 deletions Creating_Dataset _MFCC.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,347 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: progressbar in /home/ravisingh/anaconda3/lib/python3.7/site-packages (2.5)\n"
]
}
],
"source": [
"from pip._internal import main\n",
"main([\"install\",\"progressbar\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Importing Important Libraries."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"import progressbar\n",
"import time\n",
"import os\n",
"import struct\n",
"import matplotlib.pyplot as plt\n",
"import IPython.display as ipd\n",
"import pandas as pd\n",
"import numpy as np\n",
"import librosa # for sound processing.\n",
"import DataCollection as dc # a local module"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Getting Metadata"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>slice_file_name</th>\n",
" <th>fsID</th>\n",
" <th>start</th>\n",
" <th>end</th>\n",
" <th>salience</th>\n",
" <th>fold</th>\n",
" <th>classID</th>\n",
" <th>class</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>100032-3-0-0.wav</td>\n",
" <td>100032</td>\n",
" <td>0.0</td>\n",
" <td>0.317551</td>\n",
" <td>1</td>\n",
" <td>5</td>\n",
" <td>3</td>\n",
" <td>dog_bark</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>100263-2-0-117.wav</td>\n",
" <td>100263</td>\n",
" <td>58.5</td>\n",
" <td>62.500000</td>\n",
" <td>1</td>\n",
" <td>5</td>\n",
" <td>2</td>\n",
" <td>children_playing</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>100263-2-0-121.wav</td>\n",
" <td>100263</td>\n",
" <td>60.5</td>\n",
" <td>64.500000</td>\n",
" <td>1</td>\n",
" <td>5</td>\n",
" <td>2</td>\n",
" <td>children_playing</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>100263-2-0-126.wav</td>\n",
" <td>100263</td>\n",
" <td>63.0</td>\n",
" <td>67.000000</td>\n",
" <td>1</td>\n",
" <td>5</td>\n",
" <td>2</td>\n",
" <td>children_playing</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>100263-2-0-137.wav</td>\n",
" <td>100263</td>\n",
" <td>68.5</td>\n",
" <td>72.500000</td>\n",
" <td>1</td>\n",
" <td>5</td>\n",
" <td>2</td>\n",
" <td>children_playing</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" slice_file_name fsID start end salience fold classID \\\n",
"0 100032-3-0-0.wav 100032 0.0 0.317551 1 5 3 \n",
"1 100263-2-0-117.wav 100263 58.5 62.500000 1 5 2 \n",
"2 100263-2-0-121.wav 100263 60.5 64.500000 1 5 2 \n",
"3 100263-2-0-126.wav 100263 63.0 67.000000 1 5 2 \n",
"4 100263-2-0-137.wav 100263 68.5 72.500000 1 5 2 \n",
"\n",
" class \n",
"0 dog_bark \n",
"1 children_playing \n",
"2 children_playing \n",
"3 children_playing \n",
"4 children_playing "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.read_csv(\"UrbanSound8K/metadata/UrbanSound8K.csv\")\n",
"data.head()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8732"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.shape[0]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(8732, 2)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"###data = data.sort_values(by=['fold', 'classID',\"fsID\"], ascending=[True, True, True])\n",
"dataset = np.zeros(shape = (data.shape[0],2),dtype = object)\n",
"dataset.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Extracting Feature"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"||$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ || 99%\r"
]
}
],
"source": [
"bar = progressbar.ProgressBar(maxval=data.shape[0], widgets=[progressbar.Bar('$', '||', '||'), ' ', progressbar.Percentage()])\n",
"bar.start()\n",
"for i in range(data.shape[0]):\n",
" \n",
" fullpath, class_id = dc.path_class(data,data.slice_file_name[i])\n",
" try:\n",
" X, sample_rate = librosa.load(fullpath, res_type='kaiser_fast')\n",
" mfccs = np.mean(librosa.feature.melspectrogram(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)\n",
" except Exception:\n",
" print(\"Error encountered while parsing file: \", file)\n",
" mfccs,class_id = None, None\n",
" feature = mfccs\n",
" label = class_id\n",
" dataset[i,0],dataset[i,1] = feature,label\n",
" \n",
" bar.update(i+1)\n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"np.save(\"dataset_melspectrogram\",dataset,allow_pickle=True)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"l = np.load(\"dataset_melspectrogram.npy\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(8732, 2)"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l.shape"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'car_horn'"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"l[8730,1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 30e6ed9

Please sign in to comment.