Data Augmentation for Audio
Tags: speechrecognition, machinelearning, ai, datascience
Published: 2019-06-08 15:03:25

---
title: Data Augmentation for Audio
published: true
tags: speech-recognition,machine-learning,artificial-intellige,data-science
canonical_url: https://towardsdatascience.com/data-augmentation-for-audio-76912b01fdf6
---

#### Data Augmentation

![](https://cdn-images-1.medium.com/max/1024/0*QsYCC8milwRRx3Tf)<figcaption>Photo by <a href="https://unsplash.com/@makcedward?utm_source=medium&amp;utm_medium=referral">Edward Ma</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

Although tuning model architecture and hyperparameter are successful factor of building a wonderful model, data scientist should also focus on data. No matter how amazing model you build, **garbage in, garbage out (GIGO)**.

Intuitively, lack of data is one of the common issue in actual data science problem. Data augmentation helps to generate synthetic data from existing data set such that generalisation capability of model can be improved.

In the previous [story](https://dev.to/makcedward/data-augmentation-for-speech-recognition-3279-temp-slug-8408894), we explained how we play with spectrogram. In this story, we will talk about a basic augmentation methods for audio. This story and implementation are inspired by [Kaggle’s Audio Data Augmentation Notebook](https://www.kaggle.com/CVxTz/audio-data-augmentation).

### Data Augmentation for Audio

To generate syntactic data for audio, we can apply noise injection, shifting time, changing pitch and speed. [numpy](https://github.com/numpy/numpy) provides an easy way to handle noise injection and shifting time while [librosa](https://github.com/librosa/librosa) (library for Recognition and Organization of Speech and Audio) help to manipulate pitch and speed with just 1 line of code.

#### Noise Injection

It simply add some random value into data by using numpy.

```
import numpy as np

def manipulate(data, noise\_factor):
 noise = np.random.randn(len(data))
 augmented\_data = data + noise\_factor \* noise
 # Cast back to same data type
 augmented\_data = augmented\_data.astype(type(data[0]))
 return augmented\_data
```

![](https://cdn-images-1.medium.com/max/604/1*43FyaaFTlz6qqHcZBy8RAw.png)<figcaption>Comparison between original and noise voice</figcaption>

#### Shifting Time

The idea of shifting time is very simple. It just shift audio to left/right with a random second. If shifting audio to left (fast forward) with x seconds, first x seconds will mark as 0 (i.e. silence). If shifting audio to right (back forward) with x seconds, last x seconds will mark as 0 (i.e. silence).

```
import numpy as np

def manipulate(data, sampling\_rate, shift\_max, shift\_direction):
 shift = np.random.randint(sampling\_rate \* shift\_max)
 if shift\_direction == 'right':
 shift = -shift
 elif self.shift\_direction == 'both':
 direction = np.random.randint(0, 2)
 if direction == 1:
 shift = -shift

augmented\_data = np.roll(data, shift)
 # Set to silence for heading/ tailing
 if shift \> 0:
 augmented\_data[:shift] = 0
 else:
 augmented\_data[shift:] = 0
 return augmented\_data
```

![](https://cdn-images-1.medium.com/max/604/1*qAwU-YJYltf3nXkeF0qO8A.png)<figcaption>Comparison between original and shifted voice</figcaption>

#### Changing Pitch

This augmentation is a wrapper of [librosa](https://librosa.github.io/librosa/generated/librosa.effects.pitch_shift.html) function. It change pitch randomly

```
import librosa

def manipulate(data, sampling\_rate, pitch\_factor):
 return librosa.effects.pitch\_shift(data, sampling\_rate, pitch\_factor)
```

![](https://cdn-images-1.medium.com/max/604/1*w5c8k9poONgevgYCvmQiuQ.png)<figcaption>Comparison between original and changed pitch voice</figcaption>

#### Changing Speed

Same as changing pitch, this augmentation is performed by [librosa](https://librosa.github.io/librosa/generated/librosa.effects.time_stretch.html)function. It stretches times series by a fixed rate.

```
import librosa

def manipulate(data, speed\_factor):
 return librosa.effects.time\_stretch(data, speed\_factor)
```

![](https://cdn-images-1.medium.com/max/604/1*bEEmdjVd_ddPuEKpD_dPeQ.png)<figcaption>Comparison between original and changed speed voice</figcaption>

### Take Away

- Above 4 methods are implemented in [nlpaug](https://github.com/makcedward/nlpaug) package (≥ 0.0.3). You can generate augmented data within a few line of code.
- Data augmentation cannot replace real training data. It just help to generate synthetic data to make the model better.
- Do not blindly generate synthetic data. You have to understand your data pattern and selecting a appropriate way to increase training data volume.

### About Me

I am Data Scientist in Bay Area. Focusing on state-of-the-art in Data Science, Artificial Intelligence , especially in NLP and platform related. Feel free to connect with [me](https://makcedward.github.io/) on [LinkedIn](https://www.linkedin.com/in/edwardma1026) or following me on   [Github](https://github.com/makcedward).

### Extension Reading

- [Audio Data Augmentation in Kaggle competition](https://www.kaggle.com/CVxTz/audio-data-augmentation)
- [librosa](https://librosa.github.io/)
- [Data Augmentation in NLP](https://towardsdatascience.com/data-augmentation-in-nlp-2801a34dfc28)
- [Data Augmentation for Text](https://towardsdatascience.com/data-augmentation-library-for-text-9661736b13ff)
- [Data Augmentation for Spectrogram](https://dev.to/makcedward/data-augmentation-for-speech-recognition-3279-temp-slug-8408894)

* * *