Reverberation time and Sound analysis of rooms using Python

Tomasz Trzos
01 November 2018
8 min read

Have you ever thought that as a web developer you would have to deal with sound analysis? Neither have I but I faced it during a project for a company which is the leading Polish acoustic furniture producer. The case was a web app with a solution to record sound, analyse it and calculate reverberation time using Python. It sounds like a nightmare for a programmer without specific knowledge about sound and acoustics, doesn't it? Fortunately, a lot of Python libraries which come in handy!

So let me walk you through the whole process. Would you like to learn how to analyse sound?

Read a wav file to measure reverberation time

First of all, you have to read a wav file and find out some details from it. In Python it is a simple task because you only need a library called the SciPy, which is quite extensive. But let's focus only on the package You can download a sample sound file with a strong signal (which is used to measure reverberation time) to compare your results with mine: download (watch out for level of your volume).

The method above returns two variables: sample rate of wav file and data (an array containing numbers). The numbers can have different ranges and types depending on the different types of wav formats.

In our case, we have an int16 NumPy dtype that is simple to check. Let's see what we can read from our file. 

Visualisation of a sound: an amplitude of digital signal

What can you do with such data? You can use another interesting library which is widely used for visualising any kind of data and preparing different kinds of charts: matplotlib. What we want to achieve is a chart which shows us a waveform of a digital signal. You can compare your result with a chart I generated using an audio editor called Audacity.

You have your sound data in a range from -2^15 to 2^15-1 so we can simply scale it to values in a range from -1 to 1 by dividing all the values by 2^15. Then you have to prepare a time array whose length will be the same as the array with our sound data. For that case, you should use a NumPy library, which helps a lot with calculations on arrays. The next step is to use a plot function from matplotlib and, voilá, we have a finished chart.

Visualisation of a sound: a spectrogram

To calculate a reverberation time you need a level of dB in a particular time. You can try to write a code step by step, but I used a specgram method from matplotlib which returns me everything I need. Let's focus on how to get a spectrogram and in the next part, we will go to reverberation time. As in the previous example, you can compare your result with a spectrogram from Audacity.

I won't analyse a parameter for specgram function, but if you want to read more about it I encourage you to check the documentation of matplotlib.pyplot.specgram. Let’s see what this function returns.


Decibel values for a specific frequency

Reverberation time of room

We should be conscious that reverberation time is an important issue because it's really inconvenient to stay in a room where it is too high. Now you will be able to check it yourself  following these steps:

1. Find a maximum value of dB's in our array
2. Slice data and time arrays to the point where you found a maximum value
3. Find a value which will be equal to max-5dB
4. Slice the data and time arrays to the point where you found a max-5dB
5. Find a value which will be equal to max-25dB
6. Calculate the time which takes amplitude to drop from max less 5dB to max less 25dB (RT20)
7. Multiply your time by 3 which gives you a RT60

Does it sound a bit tricky and hard to understand? Let's have a look at the code below.

  • green oval: max
  • yellow oval: max - 5dB
  • red oval: max - 25dB

Now you have an algorithm which works on a raw signal. To improve our results we should calculate a reverberation time on data, which was normalized in some way. The best choice for that problem is a linear regression.

Linear regression

An improved algorithm to calculate a reverberation time of room

The following step is changing your algorithm to calculate the same way as before but on a linear regression. Let's look at the code below. You almost finished!

Ok, that’s it! You just wrote a script which allows you to check reverberation time in your room. The solution in this article was a bit simplified compared to the solution in the project for our client, but it is enough for checking reverberation time in your own room. Python libraries give us a possibility to deal with unusual use cases which would be more difficult to implement e.g. in Ruby. 

Any comment you want to share with us? Drop us a line: 

You may also like...


The rarely told advantages of Ruby on Rails for developers

Implementing new ideas into reality is always hard. But as it is with the creation of every product, not only software projects, correctly selected tools can make this road much smoother. Let&rs...

Adrian Nowak
27 August 2020
3 min read

Vue for React developers

Even though I work primarily in React, during my relatively short career as front-end developer I have sort of built myself a reputation as the guy who look...

Maciek Kozłowski
12 August 2020
6 min read