{"cells":[{"metadata":{"_cell_guid":"4dbc794b-ab2f-42e0-a579-f0d25f0861af","_uuid":"2adacbc4448c40140686dbe13d6781e8d84be740","trusted":true},"cell_type":"code","source":"import numpy as np\nimport librosa\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set()\nimport os\nimport scipy\nimport librosa.display\nfrom IPython.display import Audio\nimport scipy.io.wavfile\nplt.style.use(\"ggplot\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"random_zero = \"../input/birdsong-recognition/train_audio/aldfly/XC134874.mp3\"\nAudio(random_zero)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"samples, sample_rate = librosa.load(random_zero)\nplt.figure(figsize=(12, 4))\nlibrosa.display.waveplot(samples.astype('float'), sr=sample_rate)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = librosa.stft(samples.astype('float'))\nXdb = librosa.amplitude_to_db(X)\nplt.figure(figsize=(12, 5))\nlibrosa.display.specshow(Xdb, sr=sample_rate, x_axis='time', y_axis='hz')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Change pitch and speed","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pitch_speed = samples.copy()\n# you can change low and high here\nlength_change = np.random.uniform(low=0.8, high = 1)\nspeed_fac = 1.0  / length_change\nprint(\"resample length_change = \",length_change)\ntmp = np.interp(np.arange(0,len(y_pitch_speed),speed_fac),np.arange(0,len(y_pitch_speed)),y_pitch_speed)\nminlen = min(y_pitch_speed.shape[0], tmp.shape[0])\ny_pitch_speed *= 0\ny_pitch_speed[0:minlen] = tmp[0:minlen]\nAudio(y_pitch_speed, rate=sample_rate)\nlibrosa.display.waveplot(y_pitch_speed.astype('float'), sr=sample_rate)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Change pitch only","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pitch = samples.copy()\nbins_per_octave = 12\npitch_pm = 2\npitch_change =  pitch_pm * 2*(np.random.uniform())   \nprint(\"pitch_change = \",pitch_change)\ny_pitch = librosa.effects.pitch_shift(y_pitch.astype('float64'), \n                                      sample_rate, n_steps=pitch_change, \n                                      bins_per_octave=bins_per_octave)\nAudio(y_pitch, rate=sample_rate)\nlibrosa.display.waveplot(y_pitch.astype('float'), sr=sample_rate)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Change speed only","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"y_speed = samples.copy()\nspeed_change = np.random.uniform(low=0.9,high=1.1)\nprint(\"speed_change = \",speed_change)\ntmp = librosa.effects.time_stretch(y_speed.astype('float64'), speed_change)\nminlen = min(y_speed.shape[0], tmp.shape[0])\ny_speed *= 0 \ny_speed[0:minlen] = tmp[0:minlen]\nAudio(y_speed, rate=sample_rate)\nlibrosa.display.waveplot(y_speed.astype('float'), sr=sample_rate)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## value augmentation","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"y_aug = samples.copy()\ndyn_change = np.random.uniform(low=1.5,high=3)\nprint(\"dyn_change = \",dyn_change)\ny_aug = y_aug * dyn_change\nprint(y_aug[:50])\nprint(samples[:50])\nAudio(y_aug, rate=sample_rate)\nlibrosa.display.waveplot(y_aug.astype('float'), sr=sample_rate)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## add distribution noise","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"y_noise = samples.copy()\n# you can take any distribution from https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.random.html\nnoise_amp = 0.005*np.random.uniform()*np.amax(y_noise)\ny_noise = y_noise.astype('float64') + noise_amp * np.random.normal(size=y_noise.shape[0])\nAudio(y_noise, rate=sample_rate)\nlibrosa.display.waveplot(y_noise.astype('float'), sr=sample_rate)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## random shifting","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"y_shift = samples.copy()\ntimeshift_fac = 0.2 *2*(np.random.uniform()-0.5)  # up to 20% of length\nprint(\"timeshift_fac = \",timeshift_fac)\nstart = int(y_shift.shape[0] * timeshift_fac)\nprint(start)\nif (start > 0):\n    y_shift = np.pad(y_shift,(start,0),mode='constant')[0:y_shift.shape[0]]\nelse:\n    y_shift = np.pad(y_shift,(0,-start),mode='constant')[0:y_shift.shape[0]]\nAudio(y_shift, rate=sample_rate)\nlibrosa.display.waveplot(y_shift.astype('float'), sr=sample_rate)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## apply hpss","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"y_hpss = librosa.effects.hpss(samples.astype('float64'))\nprint(y_hpss[1][:10])\nprint(samples[:10])\nAudio(y_hpss[1], rate=sample_rate)\nlibrosa.display.waveplot(y_hpss[1].astype('float'), sr=sample_rate)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Shift silent to the right","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"samples[(samples > 200) | (samples < -200)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sampling=samples[(samples > 200) | (samples < -200)]\nshifted_silent =sampling.tolist()+np.zeros((samples.shape[0]-sampling.shape[0])).tolist()\nAudio(shifted_silent, rate=sample_rate)\nlibrosa.display.waveplot(np.array(shifted_silent).astype('float'), sr=sample_rate)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Streching","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"input_length = len(samples)\nstreching = samples.copy()\nstreching = librosa.effects.time_stretch(streching.astype('float'), 1.1)\nif len(streching) > input_length:\n    streching = streching[:input_length]\nelse:\n    streching = np.pad(streching, (0, max(0, input_length - len(streching))), \"constant\")\nAudio(streching, rate=sample_rate)\nlibrosa.display.waveplot(streching.astype('float'), sr=sample_rate)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12, 4*9))\nplt.subplot(9, 1, 1)\nlibrosa.display.waveplot(samples.astype('float'), sr=sample_rate,x_axis=None)\nplt.title('original')\nplt.subplot(9, 1, 2)\nlibrosa.display.waveplot(y_pitch_speed.astype('float'), sr=sample_rate,x_axis=None)\nplt.title('augmented pitch and speed')\nplt.subplot(9, 1, 3)\nlibrosa.display.waveplot(y_pitch, sr=sample_rate,x_axis=None)\nplt.title('augmented pitch only')\nplt.subplot(9, 1, 4)\nlibrosa.display.waveplot(y_speed.astype('float'), sr=sample_rate,x_axis=None)\nplt.title('augmented speed only')\nplt.subplot(9, 1, 5)\nlibrosa.display.waveplot(y_aug.astype('float'), sr=sample_rate,x_axis=None)\nplt.title('amplify value')\nplt.subplot(9, 1, 6)\nlibrosa.display.waveplot(y_noise, sr=sample_rate,x_axis=None)\nplt.title('augmented noise')\nplt.subplot(9, 1, 7)\nlibrosa.display.waveplot(y_hpss[1], sr=sample_rate,x_axis=None)\nplt.title('apply hpss')\nplt.subplot(9, 1, 8)\nlibrosa.display.waveplot(np.array(shifted_silent).astype('float'), sr=sample_rate,x_axis=None)\nplt.title('shift silent to right')\nplt.subplot(9, 1, 9)\nlibrosa.display.waveplot(streching, sr=sample_rate)\nplt.title('stretching')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}