{"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","_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-16T16:31:54.003357Z","iopub.execute_input":"2022-03-16T16:31:54.003747Z","iopub.status.idle":"2022-03-16T16:32:17.085386Z","shell.execute_reply.started":"2022-03-16T16:31:54.003654Z","shell.execute_reply":"2022-03-16T16:32:17.082804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Acknowledgements:\nhttps://www.kaggle.com/robikscube/working-with-audio-in-python\n\nhttps://www.kaggle.com/fizzbuzz/beginner-s-guide-to-audio-data\n\nhttps://www.kaggle.com/andradaolteanu/birdcall-recognition-eda-and-audio-fe","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pylab as plt\nimport seaborn as sns\n\n\nfrom glob import glob\n\nimport librosa\nimport librosa.display\nimport IPython.display as ipd","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:32:17.102397Z","iopub.execute_input":"2022-03-16T16:32:17.102683Z","iopub.status.idle":"2022-03-16T16:32:19.460110Z","shell.execute_reply.started":"2022-03-16T16:32:17.102654Z","shell.execute_reply":"2022-03-16T16:32:19.459073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_files = glob('../input/kaggle-pog-series-s01e02/train/*.ogg')\ngenres = pd.read_csv(\"/kaggle/input/kaggle-pog-series-s01e02/genres.csv\")\ntrain = pd.read_csv(\"/kaggle/input/kaggle-pog-series-s01e02/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/kaggle-pog-series-s01e02/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:32:19.461704Z","iopub.execute_input":"2022-03-16T16:32:19.462345Z","iopub.status.idle":"2022-03-16T16:32:19.611338Z","shell.execute_reply.started":"2022-03-16T16:32:19.462295Z","shell.execute_reply":"2022-03-16T16:32:19.610685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(audio_files[10])","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:32:19.613702Z","iopub.execute_input":"2022-03-16T16:32:19.614391Z","iopub.status.idle":"2022-03-16T16:32:19.642678Z","shell.execute_reply.started":"2022-03-16T16:32:19.614341Z","shell.execute_reply":"2022-03-16T16:32:19.641804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"genres.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:32:19.644250Z","iopub.execute_input":"2022-03-16T16:32:19.644552Z","iopub.status.idle":"2022-03-16T16:32:19.668987Z","shell.execute_reply.started":"2022-03-16T16:32:19.644516Z","shell.execute_reply":"2022-03-16T16:32:19.668026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We have 19 different genres in our dataset**","metadata":{}},{"cell_type":"code","source":"genres.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:32:19.670182Z","iopub.execute_input":"2022-03-16T16:32:19.670400Z","iopub.status.idle":"2022-03-16T16:32:19.689356Z","shell.execute_reply.started":"2022-03-16T16:32:19.670370Z","shell.execute_reply":"2022-03-16T16:32:19.688473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:32:19.690358Z","iopub.execute_input":"2022-03-16T16:32:19.690567Z","iopub.status.idle":"2022-03-16T16:32:19.708142Z","shell.execute_reply.started":"2022-03-16T16:32:19.690542Z","shell.execute_reply":"2022-03-16T16:32:19.707219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.info()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:32:19.709806Z","iopub.execute_input":"2022-03-16T16:32:19.710116Z","iopub.status.idle":"2022-03-16T16:32:19.723789Z","shell.execute_reply.started":"2022-03-16T16:32:19.710064Z","shell.execute_reply":"2022-03-16T16:32:19.723048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We do not have nay missing data**","metadata":{}},{"cell_type":"code","source":"genre_group = train.groupby(['genre']).count()\nplot = genre_group['genre_id'].plot(kind='bar',figsize=(10,5))\nplot.set_xlabel(\"Genre\")\nplot.set_ylabel(\"Number of Samples\");","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:32:19.725323Z","iopub.execute_input":"2022-03-16T16:32:19.725616Z","iopub.status.idle":"2022-03-16T16:32:20.097637Z","shell.execute_reply.started":"2022-03-16T16:32:19.725576Z","shell.execute_reply":"2022-03-16T16:32:20.096870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We have imbalance in the genre variables, genres like Electronic and Rock have close to 3000 samples whereas genres like Blues and Easy Listening do not have a 100 samples**\n\n**This would need to be accounted for while passing it to a model**","metadata":{}},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:32:20.099627Z","iopub.execute_input":"2022-03-16T16:32:20.099856Z","iopub.status.idle":"2022-03-16T16:32:20.111064Z","shell.execute_reply.started":"2022-03-16T16:32:20.099828Z","shell.execute_reply":"2022-03-16T16:32:20.110173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Visualizing Raw Audio Data**","metadata":{}},{"cell_type":"code","source":"base_dir = \"../input/kaggle-pog-series-s01e02\"\nset_genre = set()\nfor sample in train.values:\n    if sample[4] not in set_genre:\n        set_genre.add(sample[4])\n        gen=sample[4] \n        y, sr = librosa.load(base_dir+'/'+sample[2])\n        pd.Series(y).plot(figsize=(10, 5),\n                  lw=1)\n        plt.title(f\"Raw Audio file for genre {gen}\",)\n        plt.show()\n    ","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:32:20.112226Z","iopub.execute_input":"2022-03-16T16:32:20.112455Z","iopub.status.idle":"2022-03-16T16:32:55.237674Z","shell.execute_reply.started":"2022-03-16T16:32:20.112427Z","shell.execute_reply":"2022-03-16T16:32:55.236437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Checking time duration of audio files**","metadata":{}},{"cell_type":"code","source":"duration = []\nfor i in range(len(audio_files[0:10])):\n    y,sr = librosa.load(audio_files[i])\n    duration.append(librosa.get_duration(y=y, sr=sr))\nduration","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:32:55.239614Z","iopub.execute_input":"2022-03-16T16:32:55.240073Z","iopub.status.idle":"2022-03-16T16:33:08.726920Z","shell.execute_reply.started":"2022-03-16T16:32:55.240018Z","shell.execute_reply":"2022-03-16T16:33:08.726063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Zero Crossing**","metadata":{}},{"cell_type":"markdown","source":"**The zero-crossing rate (ZCR) is the rate at which a signal changes from positive to zero to negative or from negative to zero to positive. Its value has been widely used in both speech recognition and music information retrieval, being a key feature to classify percussive sounds**","metadata":{}},{"cell_type":"markdown","source":"**ZCR can be interpreted as a measure of the noisiness of a signal. For example, it usually exhibits higher values in the case of noisy signals.**\n\nReference: https://www.sciencedirect.com/topics/engineering/zero-crossing-rate","metadata":{}},{"cell_type":"code","source":"set_genre = set()\nfor sample in train.values:\n    if sample[4] not in set_genre:\n        set_genre.add(sample[4])\n        gen=sample[4] \n        y, sr = librosa.load(base_dir+'/'+sample[2])\n        zero = librosa.zero_crossings(y,pad=False)\n        print(f\"The change rate for genre {gen} is {sum(zero)}\")","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:33:08.728305Z","iopub.execute_input":"2022-03-16T16:33:08.728536Z","iopub.status.idle":"2022-03-16T16:34:14.571238Z","shell.execute_reply.started":"2022-03-16T16:33:08.728508Z","shell.execute_reply":"2022-03-16T16:34:14.570255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Below we plot zero crossing across time i.e the zero crossing rate**","metadata":{}},{"cell_type":"code","source":"set_genre = set()\nfor sample in train.values:\n    if sample[4] not in set_genre:\n        set_genre.add(sample[4])\n        gen=sample[4] \n        y, sr = librosa.load(base_dir+'/'+sample[2])\n        zero = librosa.feature.zero_crossing_rate(y,pad=False)\n        plt.plot(zero[0],color = '#A300F9')\n        plt.title(f\"Zero Crossing Rate for genre {gen}\",)\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:34:14.573033Z","iopub.execute_input":"2022-03-16T16:34:14.573351Z","iopub.status.idle":"2022-03-16T16:34:43.304756Z","shell.execute_reply.started":"2022-03-16T16:34:14.573299Z","shell.execute_reply":"2022-03-16T16:34:43.303679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Tempo/ Beats per minute**","metadata":{}},{"cell_type":"code","source":"set_genre = set()\nbeats_per_min = []\ngenre = []\nfor sample in train.values:\n    if sample[4] not in set_genre:\n        set_genre.add(sample[4])\n        gen=sample[4] \n        y, sr = librosa.load(base_dir+'/'+sample[2])\n        bpm,_ = librosa.beat.beat_track(y=y,sr=sr)\n        beats_per_min.append(bpm)\n        genre.append(gen)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:41:02.895438Z","iopub.execute_input":"2022-03-16T16:41:02.895943Z","iopub.status.idle":"2022-03-16T16:41:31.855121Z","shell.execute_reply.started":"2022-03-16T16:41:02.895905Z","shell.execute_reply":"2022-03-16T16:41:31.853815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nbpm = pd.DataFrame()\nbpm[\"bpm\"] = beats_per_min\nbpm[\"genre\"] = genre\nsns.barplot(data=bpm,x=\"bpm\",y=\"genre\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:47:48.405524Z","iopub.execute_input":"2022-03-16T16:47:48.406165Z","iopub.status.idle":"2022-03-16T16:47:48.711213Z","shell.execute_reply.started":"2022-03-16T16:47:48.406127Z","shell.execute_reply":"2022-03-16T16:47:48.710242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**International and Experimental have about 160 bpm whereas easy listening has 80**","metadata":{}},{"cell_type":"markdown","source":"**Work In Progress**","metadata":{}}]}