{"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":"2023-04-21T18:07:41.262652Z","iopub.execute_input":"2023-04-21T18:07:41.264081Z","iopub.status.idle":"2023-04-21T18:08:08.307601Z","shell.execute_reply.started":"2023-04-21T18:07:41.264015Z","shell.execute_reply":"2023-04-21T18:08:08.306355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data augmentation definition:\n**1. Data augmentation is the process by which we create new synthetic training samples by adding small perturbations on our initial training set.**\n\n**2. The objective is to make our model invariant to those perturbations and enhance its ability to generalize**\n\n**3. In order for this to work adding the perturbations must conserve the same label as the original training sample.**\n\n**4. In images data augmentation can be performed by shifting the image, zooming, rotating, etc.**\n\n**5. In our case we will add noise, stretch and roll, pitch shift..**","metadata":{}},{"cell_type":"code","source":"#Import stuff\n\nimport numpy as np\nimport random\nimport itertools\nimport librosa\nimport IPython.display as ipd\nimport matplotlib.pyplot as plt\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:09:25.342229Z","iopub.execute_input":"2023-04-21T18:09:25.342782Z","iopub.status.idle":"2023-04-21T18:09:25.372537Z","shell.execute_reply.started":"2023-04-21T18:09:25.342734Z","shell.execute_reply":"2023-04-21T18:09:25.370931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_audio_file(file_path):\n    input_length = 16000\n    data = librosa.core.load(file_path)[0] #, sr = 16000\n    \n    if len(data) > input_length:\n        data = data[: input_length]\n    else:\n        data = np.pad(data, (0, max(0, input_length - len(data))), \"constant\")\n    return data","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:10:34.270728Z","iopub.execute_input":"2023-04-21T18:10:34.272798Z","iopub.status.idle":"2023-04-21T18:10:34.280856Z","shell.execute_reply.started":"2023-04-21T18:10:34.272717Z","shell.execute_reply":"2023-04-21T18:10:34.279749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_time_series(data):\n    fig = plt.figure(figsize = (14, 8))\n    plt.title(\"Raw wave \")\n    plt.ylabel(\"Amplitude\")\n    plt.plot(np.linspace(0, 1, len(data)), data)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:11:13.067716Z","iopub.execute_input":"2023-04-21T18:11:13.068154Z","iopub.status.idle":"2023-04-21T18:11:13.075860Z","shell.execute_reply.started":"2023-04-21T18:11:13.068118Z","shell.execute_reply":"2023-04-21T18:11:13.074153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = load_audio_file(\"../input/dl-sprint-buet-cse-fest-2022-processed-dataset/new_train/common_voice_bn_30999159.wav\")\nplot_time_series(data)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:12:11.830999Z","iopub.execute_input":"2023-04-21T18:12:11.831529Z","iopub.status.idle":"2023-04-21T18:12:19.604903Z","shell.execute_reply.started":"2023-04-21T18:12:11.831489Z","shell.execute_reply":"2023-04-21T18:12:19.603786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(data, rate = 16000)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:12:44.717005Z","iopub.execute_input":"2023-04-21T18:12:44.717426Z","iopub.status.idle":"2023-04-21T18:12:44.728309Z","shell.execute_reply.started":"2023-04-21T18:12:44.717392Z","shell.execute_reply":"2023-04-21T18:12:44.726726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wn = np.random.randn(len(data))\ndata_wn = data + 0.005 * wn\nplot_time_series(data_wn)\n# We limited the amplitude of the noise so we can still hear the word even with the noise,\n# which is the objective\nipd.Audio(data_wn, rate = 16000)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:14:48.339816Z","iopub.execute_input":"2023-04-21T18:14:48.340227Z","iopub.status.idle":"2023-04-21T18:14:48.661782Z","shell.execute_reply.started":"2023-04-21T18:14:48.340192Z","shell.execute_reply":"2023-04-21T18:14:48.660353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shifting The Sound\ndata_roll = np.roll(data, 1600)\nplot_time_series(data_roll)\nipd.Audio(data_roll, rate = 16000)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:15:18.996793Z","iopub.execute_input":"2023-04-21T18:15:18.997314Z","iopub.status.idle":"2023-04-21T18:15:19.238446Z","shell.execute_reply.started":"2023-04-21T18:15:18.997271Z","shell.execute_reply":"2023-04-21T18:15:19.236953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = load_audio_file(\"../input/dl-sprint-buet-cse-fest-2022-processed-dataset/new_train/common_voice_bn_31565582.wav\")\nplot_time_series(data)\n\n#2nd aud","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:19:04.615368Z","iopub.execute_input":"2023-04-21T18:19:04.615848Z","iopub.status.idle":"2023-04-21T18:19:04.868473Z","shell.execute_reply.started":"2023-04-21T18:19:04.615805Z","shell.execute_reply":"2023-04-21T18:19:04.867108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Hear it ! \nipd.Audio(data, rate=16000)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:19:14.188134Z","iopub.execute_input":"2023-04-21T18:19:14.188560Z","iopub.status.idle":"2023-04-21T18:19:14.198498Z","shell.execute_reply.started":"2023-04-21T18:19:14.188525Z","shell.execute_reply":"2023-04-21T18:19:14.197419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adding white noise\nwn = np.random.randn(len(data))\ndata_win = data + 0.005 * wn\nplot_time_series(data_wn)\n\nipd.Audio(data_wn, rate = 16000)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:19:49.222339Z","iopub.execute_input":"2023-04-21T18:19:49.222788Z","iopub.status.idle":"2023-04-21T18:19:49.538926Z","shell.execute_reply.started":"2023-04-21T18:19:49.222752Z","shell.execute_reply":"2023-04-21T18:19:49.537479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shifting The Sound\ndata_roll = np.roll(data, 1600)\nplot_time_series(data_roll)\nipd.Audio(data_roll, rate = 16000)","metadata":{"execution":{"iopub.status.busy":"2023-04-21T18:20:32.606057Z","iopub.execute_input":"2023-04-21T18:20:32.606620Z","iopub.status.idle":"2023-04-21T18:20:32.922092Z","shell.execute_reply.started":"2023-04-21T18:20:32.606564Z","shell.execute_reply":"2023-04-21T18:20:32.920494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}