Beginner’s guide to Speech Analysis
Tags: datascience, speech, audio, python
Published: 2019-01-07 06:48:11

---
title: Beginner’s guide to Speech Analysis
published: true
tags: data-science,speech,audio,python
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/1*xofsW_JhIcQjwlakxogezg.png)<figcaption>Speech signal from <a href="https://pixabay.com/en/sound-wave-voice-listen-856771/">Pixabay</a></figcaption>

This article gives an introduction to speech signals and its analysis. Also, I have compared with text analysis to see how it different it is from speech.

**Speech as compared to text as a medium of communication**

Speech is defined as the expression of thoughts and feelings by articulating sounds. Speech is the most natural, intuitive and preferred means of communication by human beings. The perceptual variability of speech exists in the form of various languages, dialects, accents, while the vocabulary of speech is growing day by day. More intricate variability at the speech signal level exists in the form of varying amplitude, duration, pitch, timbre and speaker variability.

Text as a means of communication evolved to store and convey information over long distances. It is the written representation of any speech communication. It is a more simple form of communication and is devoid of the previously mentioned intricate variabilities existing in speech.

The intricate variabilities in speech makes it more complicated to analyse but provides additional information using the tone and amplitude variation.

**Representation of speech and text**

Speech and text analysis have wide applications in the present world. They have different representations, and many dissimilarities and challenges are encountered in their analysis.

Let us look at a speech signal taken from one utterance of [CMU US RMS ARCTIC](http://festvox.org/cmu_arctic/dbs_rms.html) speech database. Each of the utterances are recorded as a 16 bit signal at a sampling frequency of 16 kHz, which means there are 16000 samples for each second of the signal and each sample has a resolution of 16 bits per sample. Sampling frequency of an audio signal determines the resolution of the audio samples, higher the sampling rate, higher is the resolution of the signal. Speech signal is read from _‘arctic\_a0005.wav’_ file in the speech database which has a duration of around 1.4 seconds, equivalent to a sequence of 22640 samples, each sample a 16 bit number. The below speech representation is a plot of the speech signal from _‘arctic\_a0005.wav’_ whose equivalent text is _“will we ever forget it”_ :

![](https://cdn-images-1.medium.com/max/1008/1*2quenLCTYGTTIgIoeMPDjg.jpeg)<figcaption>Speech signal, s[n] for the utterance “will we ever forget it”</figcaption>

It can be seen from the above figure that speech can be represented as a variation of amplitude with time. The amplitude is normalised such that the maximum value is _1_. Speech is basically a sequence of articulate sound units like ‘w’, ‘ih’ known as phonemes. Speech signal can be segmented into a sequence of phonemes and silence/ non-speech segments.

The database also contains the corresponding text transcription for the speech signal both at sentence and phoneme level for each wave file. Below is the representation of a part of the above speech signal showing the phonemes and their corresponding time span.

![](https://cdn-images-1.medium.com/max/1008/1*NBdN8HtGpK5Sdll18caYcQ.jpeg)<figcaption>Phoneme level segments of the signal for ‘will’ as ‘w’, ‘ih’ and ‘l’</figcaption>

It is seen from the above figure that the phonemes ‘w’, ‘ih’ and ‘l’ are quasi-periodic in nature, and are categorised as voiced phonemes as they are produced by periodic vibration of vocal folds. Further, ‘ih’ is a vowel while ‘w’ and ‘l’ are semi-vowels. Voiced and unvoiced classes are the broad categorisation of speech sounds based on the vibration of vocal cords. The study and classification of different phonemes is called as phonetics.

![](https://cdn-images-1.medium.com/max/1008/1*0DgnbmR-hkeoCgsDkYtwcw.jpeg)<figcaption>Phoneme level segments of the signal for ‘forget’ as ‘f, ‘er’, ‘g’, ‘eh’ and ‘t’</figcaption>

In the above figure we have unvoiced phonemes like ‘f’, ‘g’ and ‘t’ and voiced phonemes like ‘er’ and ‘eh’. Phonemes ‘g’ and ‘t’ are further categorised as stops which is silence followed by a sudden impulse. It can be observed that the voiced component is quasi periodic while unvoiced components are noisy as they are not produced by periodic vibration of vocal folds.

The phonemes can be mapped to the written form of speech using a phoneme to grapheme mapping. Below is the mapping between the text and the corresponding phonemes:

Text: “will we ever forget it”

Phonetic sequence: ‘w’, ‘ih’, ‘l’, ‘w’, ‘iy’, ‘eh’, ‘v’, ‘er’, ‘f’, ‘er’, ‘g’, ‘eh’, ‘t’, ‘ih’, ‘t’

From the above mapping, it can be seen that the word “will” is mapped to the phonemes ‘w’, ‘ih’, ‘l’.

Further we will look at the frame level frequency domain representation of speech, also popularly known as **Short-time Fourier transform (STFT)** in the speech research community. **Spectrogram** is a visual representation of the frequency domain representation of sound.

![](https://cdn-images-1.medium.com/max/1024/1*N6RhK6_pjgaLH84xWEJ7qg.jpeg)<figcaption>Log scaled spectrogram of the speech signal using a window size of 30 ms and hop size of 7.5 ms</figcaption>

Log scaled spectrogram plotted above is the amplitude of STFT in the log scale. A frame size of 30 ms is chosen which is equivalent to 30 ms x 16 kHz = 480 samples of the speech signal, while frame shift of 7.5 ms is equivalent to 120 samples. The reason behind dividing the speech signal into frames of small duration is that the speech signal is non-stationary and its temporal characteristics change very fast. So, by taking a small frame size, we make an assumption that the speech signal will be stationary and its characteristics will not vary much within the frame. Also, a shorter frame shift is chosen to track the continuity in the speech signal and not miss out any abrupt changes at the edges of the frames. It can be seen from the above plot that frequency domain representation of each frame helps us to analyse speech signal better; as we can easily see the harmonics as parallel red segments in voiced regions and how the amplitude varies for each frequency and frame index. Hence, most of analysis of the speech signal is done in frequency domain. But extraction of temporal information like abrupt changes in the signal (beginning of burst like ‘t’) is better captured in time domain as dividing speech signal into frames discards instantaneous changes in the signal.

We can say that we can get better temporal resolution in the frequency domain by taking a smaller frame size. But there is a tradeoff between resolution in time and frequency domain. Taking a very small frame size will give a higher resolution in time but will give few samples in a single frame and the corresponding Fourier components will have few frequency components. And taking a larger frame size will give lower time resolution but higher frequency resolution due to higher number of samples. So, getting a high resolution both in time and frequency simultaneously is not possible.

It is observed that the y-axis in the log spectrogram plot has frequency upto 8 kHz for a sampling frequency of 16 kHz. This is because, as per Nyquist–Shannon sampling theorem, the maximum frequency which can be observed in a discrete signal is at most half of the sampling frequency which is 8 kHz.

While speech has a lot of variability depending on the environment, speaker, mood and tone of the speaker, text is devoid of all this variability.

The equivalent text is represented as a sequence of alphabets, symbols and spaces as “will we ever forget it”.

**Applications of speech analysis**

_Voice activity detection:_ Identifying segments in a audio waveform where only speech is present, neglecting the non-speech and silent segments

_Speech enhancement:_ Improving the quality of speech signal by filtering and separating the noise from the speech segments

_Speech recognition:_ Converting the speech signal to text, still its a challenge in different conditions, recognition can be vocabulary dependent or independent

_Text to speech:_ Synthesising natural speech from text, making the speech sound very natural with emotions is challenging

_Speaker diarization and speaker recognition:_ Diarization is segmenting the speech signal into segments belonging to different speakers while speaker recognition is identifying who is speaking at a particular time

_Audio source separation:_ Separating mixed speech signal like speech overlapped with speech from a different speaker or noise

_Speech modification:_ Modifying speech like changing its emotion, tone, converting to speech spoken by a different speaker

_Emotional speech classification:_ Identifying emotion of the speech like happy, angry, sad and anxiety

_Keyword spotting:_ Identifying specific keywords in the whole speech utterance

**Applications of text analysis:**

_Text classification:_ Classifying the whole text document into various classes, or sequence of words into different classes

_Named entity recognition:_ Identifying people, organisations, place names, certain abbreviations and other entities

_Text summarisation:_ Generating summary from documents

_Document clustering:_ Identifying similar text documents based on similar content

_Sentiment analysis:_ Identifying the mood, emotion, sentiment and opinion from the text

**Challenges in speech and noise analysis:**

All the above applications of speech and noise analysis are quite challenging to solve. External factors which further complicate speech and noise analysis are various kinds of noise incurred along with the speech and text. Various signal processing, neuroscience based methods, supervised and unsupervised machine learning techniques are explored to solve the same. Due to the unstructured nature of speech signals, deep learning based methods have shown success for various applications.

#### Noise in speech and text :

Noise is any unwanted signal distorting the original signal. Adding noise to speech vs adding noise to text is very different.

Given a speech signal with amplitude _s[n]_, where _n_ is the sample index, noise is any other signal, _w[n]_ which interferes with the speech. The noisy speech signal _u[n]_ can be seen as:

_u[n]=s[n] + w[n]_

In the above case, the noise is additive in nature, which is the most simple case. Noise can also incur in a convolutional form such as reverberation, amplitude clipping and other non-linear distortions of the speech signal.

![](https://cdn-images-1.medium.com/max/1024/1*AOBI3B107I4q-IIEYE9-Ig.jpeg)<figcaption>factory1 noise, w[n] from NOISEX92 database</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*dLkxkw5fpbR1W3nIZWHlDQ.jpeg)<figcaption>Log scaled spectrogram of the factory1 noise using a window size of 30 ms and hop size of 7.5 ms</figcaption>

The above plot is of factory1 noise both in time and frequency domain which is taken from the [NOISEX92](http://www.speech.cs.cmu.edu/comp.speech/Section1/Data/noisex.html) database. The above noise samples are resampled to the same sampling rate of speech samples, 16 kHz as we are adding speech to noise and both should have same sampling rate.

![](https://cdn-images-1.medium.com/max/1024/1*XhUk2WnBUuTjU2rc6lp-Eg.jpeg)<figcaption>Noisy speech, u[n] at an SNR of 0dB</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*cbcK2yD6b6FxxMmPMaRmmA.jpeg)<figcaption>Log scaled spectrogram of noisy speech using a window size of 30 ms and hop size of 7.5 ms</figcaption>

The above plot is of noisy speech both in time and frequency domain. Noise in a speech signal modifies the whole signal and it is difficult to analyse and extract speech segments. Various speech enhancements algorithms exist to reduce the noise component and improve the intelligibility of speech.

Given text sentence, noise can incur in the form of misspelt and missing words which may either change the meaning of the sentence or create a meaningless sentence. For example:

Original text sentence: _‘will we ever forget it’_

Noisy text sentence : _‘will we never forget it’_

In the above noisy text, noise incurs in the form of the word ‘_ever’_ changed to _‘never’,_ which changes the meaning of the sentence.

Another form of noisy text: _‘will we never forggt it’_

In the above noisy text, noise incurs in the form of the word ‘_forget’_ changed to _‘forggt’,_ which makes the sentence meaningless due to the misspelt word _‘forggt’_.

So, it can be seen that adding noise to speech distorts the whole signal while in text distortion is discrete like missing a character/word or mispelling.

**_Illustration of speech analysis_**

We will now illustrate an important speech analysis technique. A recording of any audio signal in general contains lot of silence regions and we may be interested only in the segments where speech is present. This is useful to extract speech segments from the signal containing long silence regions automatically as silence regions do not convey any information. These speech segments can be further analysed for various applications like speech recognition, speaker and emotion classification.

Hence, silence detection is an important pre-processing step in most of the speech applications.

Given a speech signal _s[n]_, silence regions can be detected by comparing the relative energy of segments within short durations. We take a frame size of 20 ms and calculate the short term energy signal, _e[n]_ as sum of square of _s[m]_ where _m_ is within +/-10 ms (in samples) of _n_ _._ The frame size is chosen depending on how much temporal change in energy of the speech signal we want to detect. A short frame size is able to detect abrupt change in energy but may give many alternating frames of silence segments due to inherent silence parts in some phonemes like bursts and in between words.

![](https://cdn-images-1.medium.com/max/1024/1*lI41uPavpqd94GedqJYvMw.jpeg)<figcaption>Short term energy, e[n]<em> </em>of speech signal</figcaption>

It is seen from the above plot that the short term energy of the speech signal varies abruptly and a relative threshold can be used to detect silence regions.

![](https://cdn-images-1.medium.com/max/1024/1*N7c3XYTkMWvXUA3fyfD6SA.jpeg)<figcaption>Silence detection in speech signal</figcaption>

The above plot shows the silence regions highlighted in red by using a threshold of 0.01% of the average short term energy of the speech signal. The threshold is chosen based on observation of the variation of short term energy in the speech signal.

One challenge in silence detection is in case the speech signal is noisy, then the relative energy in silence regions of speech will also be high. This can be solved by seeing the variation of short term energy of lowpass filtered speech signal in case the noise has predominantly high frequency components.

**Demo of the simulations done in the present article can be found at** [**Github**](https://github.com/vijaygirish2001/speech_analysis) **and** [**NBViewer**](https://nbviewer.jupyter.org/github/vijaygirish2001/speech_analysis/blob/master/speech_analysis.ipynb) **.**

Hope you found this article and demo useful. I will try to add more insights on the analysis of speech signal in the upcoming articles.

**_About the author_**

_I am a Data Scientist at_ [_Belong.co_](https://belong.co/) _and have finished my PhD from the Indian Institute of Science Bangalore, India._

* * *