Skip to main content

Week 4 [05-11.11.18] Sound analysis and reverberation time of rooms using Python



Today I would like to share with you my first article which I wrote after finish work on interesting project for company which is produces acoustic furnitures. I hope that this article will be quite interesting for most of you because most of us are programmers.

"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. It sounds like a nightmare for a programmer without specific knowledge about sound and acoustics, doesn't it? Fortunately, we have Python and 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 scipy.io.wavfile. You can download a sample sound file with 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 a 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 on a 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 a 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."

1. Have you ever heard about reverberation time?
2. Do you have any experience with programming in python or ruby language? Maybe can you share with us any interesting libraries or plugins?
3. Do you have an idea which programming language could also deal with case as described in the article? Have you ever had such unusual cases in you job?

Comments

Peter Clemenza said…
1. Have you ever heard about reverberation time?
Yes, reverb effect is used quite often on sound samples and voice records while dealing with music making

2. Do you have any experience with programming in python or ruby language? Maybe can you share with us any interesting libraries or plugins?
I write automated tests in python for medical equipment software in my job. You should definitely check out:
tikinter: (native python library for gui programming, its cross-platform, quite old however, but powerful (i wrote my own code editor within it: https://mikazz.github.io/Edit/
however sill under development.

selenium: site automation, web scrapping, perfect for writing bots.

3. Do you have an idea which programming language could also deal with case as described in the article? Have you ever had such unusual cases in you job?
I think there is better way, especially for non-programmers who struggle with putting everything written here together. I recommend audacity plugin or fl studio where I think same results can be achieved.
1. I've never heard about this term, but it seems to me that it is quite important when it comes to audio business.
2. Nowadays python becoming extremely popular language especially in data analysis.
Before I started learning this language I'd been a bit puzzled. Why python ? In most if benchmarks it is much slower than for example Java or C++. All my confusion disappeared after few months, after I reimplemented small neural network form java to python.
I was astonished. In java the whole code was like around 800 lines while in python it took about 70 lines. I guess it is obvious. Most of job was done with the help of early mentioned numpy library just because neural nets are mostly is a set of particular matrices operations. For now, I'm working in AI project where mostly I use python with various libraries for neural networks e.g tensorflow, keras, pytorch, tflear. Also I had a small task in computer vision field where I learned really cool library which is called OpenCV. It's extremely powerful tool for image processing.
3. In my opinion, almost all low level languages are able to deal with such type of tasks. It's just a matter of time and probably mental health )
Unknown said…
Thank you for response Peter. I see that you also did interesting project using python language. I would like to learn more about this! Thank you also for your examples of useful libraries.

The case was that this script was implemented in web application and this sound analysis have been doing in the background. I know audacity app and it was used to comparison but it is only good for manual analysis.
Unknown said…
I agree entirely that languages like python are more programmer friendly. I work as a Ruby on Rails developer and mostly I use a ruby language which is quite similar to python. Clean code in these languages are very readable, short and it is pleasure and fun to using it.
Unknown said…
No, I haven't heard about reverberation time but I would surely like to learn more about the math behind it.
Yes, I have programming experience in Python. In fact, I am working as a Python web developer. But I also had the opportunity to work with some popular Python data analysis tools and libraries like numpy, pandas or seaborn. If you want to take a shot at data science then you should probably checkout Jupyter Notebooks, because this tool will make your life much easier while working with numpy or pandas. It's sad that Python didn't receive much recognition in web-development because this language is really powerful. And maybe it's slower than C++ or Java but if you use PyPy interpreter with JIT instead of standard CPython than you will notice major performance improvements in your programs.
I think that R would be useful for this kind of work too because it also has an extensive amount of different libraries created specifically for data analysis.
No, not yet.

Also I think you should post this article on Medium because it's really worth sharing.
Yes, i heard a lot about that and i practise every week learning python on my PSO classes. Honestly, i'm not a fan of python or ruby, but i have some experience in it. Unfortunately, i don't even know what i can share that might be interesting and new for you as i'm not experienced enough. Don't know a lot about languages that can help to repeat the sucess of python to describe reverberation time. But i agree with a person above and i would say that R language will be competetive in that kind of work. Have you ever had such unusual cases in you job? Luckily, no, i work in web development sphere.
I haven't got an experience with python yet -- my main languages were C++ and Java so far. Despite their pros, I see some obvious cons in Python and Ruby. And, while, as Volodymyr stated, the same program in Python can take a ten times less room than the same program in Java, their main fields remain different. They just were designed and serve different purpose.
Unknown said…
Yes, I've heard. My girl in her master's thesis deals in the processing of sound and also used the Python language and determined similar parameters and graphs.
I mainly program in Java. From time to time, I will write a python script for web scrapping, for example.
I think Python is great for such scientific things. Similar or even more advanced things can be done in MatLab.
Iman Masjedi said…
No, unfortunately I have not heard anything about this.
Yes, I have the Python programming experience. One of the Python's charm is the writing of artificial intelligence applications that today have a lot of users in human life and, in my opinion, the future of the programming industry will move to that side.
Yes, I think the C programming language has the ability to analyze audio data.
Vladlen Kyselov said…
Unfortunately I am new to Python and signal analysis, because I have had a bad course from this subject on my bachelor degree. I had to all those terrible signal transformations in Matlab and to be honest I started to hate this subject, but now I have subject called PSO where I am "reviving" my faith in signal analysis and transformation. That`s a very interesting article, it was really interesting to read it, thank you!
No, I have never heard about reverberation time, mostly because I have been particularly interested in sound analysis.
I have some experience in Python, currently I am writing my engineering thesis in this language. In the beginning it was hard for me get used to it. I was learning Java and C++ before and Python has completely different philosophy. However when I was discovering Python more and more I was astonished how easy I can do things which required tons of code in other languages.
I am not sure which programming language could deal with this case with so little effort. Probably custom solution written in C/C++ would be faster but I am sure it would require enormous work.
Kristina Moroz said…
Well, a lot of thing are new for me. And this is one them. But it is actually was really interesting to red. I have never before worked with this languages, Python, I mean. So I have PSO classes and it's really interesting and I started like it. Earlier I had to deal with Matlab but honestly I hate it. Unfortunately I can't help you with this problem but I hope you'll find something.
1. Have you ever heard about reverberation time?
Yes, I've heard about it in school.
2. Do you have any experience with programming in python or ruby language? Maybe can you share with us any interesting libraries or plugins?
I do have, but it is only python classes in our university, so that's not much (I can't recommend any libraries or plugins).
3. Do you have an idea which programming language could also deal with case as described in the article? Have you ever had such unusual cases in you job?
I think most of programming languages would deal with those cases, but it is all about how complex solution it would be. I think in C++ you could do it with not so big effort.

1. That's how I heard about the reverberation time in the context of creating sounds and music. Unfortunately, I have never worked on it personally.
2. I have never programmed in either Python or Ruby. I don't like programming and I try to avoid it when I can. I don't think I'll be helpful this time about interesting libraries.
3. In my opinion, all script programming languages ​​can deal with it. The effects are already dependent on the experience of the programmer.
Filip Sawicki said…
No I haven’t heard about reverberation time, I’m not interested in audio engineering. However I have lots of experience in python programming language, especially in data engineering, analysis and science. Heck, it is my favorite language by far. Regarding libraries I always use numpy, pandas, scikit-learn, keras, tensorflow, matplotlib with seaborn and lots and lots of others. I think that for this task R programming language would suit you well, but this is only for rapid prototyping or quick analysis.
Unknown said…
I have not heard about reverberation time before but I’m glad that I read it now because it’s very interesting.

I have a little experience both with programming with python or ruby. As I said my experience is very little so I can’t share with you any interesting libraries or plugins.

Sorry, I have no idea which programming language could also deal with the case as described in the article and I never have had such unusual cases in my job.
Unknown said…
I don't remember ever hearing about reverberation time, but it does sound like something that needs to be considered when recording or playing various sounds.

I don't have much experience with ruby, but I have done some python. I haven't really done anything very interesting in python, but I can recommend Anaconda - a plugin for sublime text designed to function as an IDE for Python. It's very convenient for people who use Sublime Text and also use Python.

As for which programming languages could deal with cases described in the article, most of them propably could. However, most of them are also propably not very well suited for it, and would require putting in considerable work to achieve what was described above.
1. I have heard about it

2. I have a little experience with Python, but with web development - I've been using Flask and SQLAlchemy

3. I'm not sure - I'm not really into this area of programming
Unknown said…
1. Have you ever heard about reverberation time?
Never heard about that.
2. Do you have any experience with programming in python or ruby language? Maybe can you share with us any interesting libraries or plugins?
Just basic stuff in Python, but I dropped it after 2 weeks, so don't know any intersting to share :D
3. Do you have an idea which programming language could also deal with case as described in the article? Have you ever had such unusual cases in you job?
Sorry sir, but I don't like programming, it's not part of my job or even studies. :/
This will be a short comment, but I'm not really in the programming and it's boring for me :/
Unknown said…
I hope that it was a bit interesting and I didn't bore you to death. :D
Unknown said…
For sure plugin as you mentioned is also important to comfortable work with a code so thank you shared it! :)
Unknown said…
I've heard from you that you work as a Python developer and I thought that it could be interesting article for you. It is good thought to publish it on Medium and I will probably do it in the nearest future. Thank your for that! :)
Unknown said…
I work as a web developer too but I had to deal with such unusual case so you can't be sure that you won't have similar case in your career. :D
Unknown said…
Yeah, I agree with you that it is really convenient to write a code which is shorter and easier to understand. It is one of reasons why I appreciate a Ruby language so much.
Unknown said…
I see that you have a lot of experience as a python developer. I have also used some of libraries which you mentioned. Using them makes our work much easier when we have to deal with such cases as I described in this article.
Unknown said…
I think that when you are not fluent using C++ it would be very hard to deal with such complex case. Then I would have to focus not only on logics but also on complexity of C++ language. This is reason why I think that it is better to use scrip languages as python.
Unknown said…
I think that Python is a mix of programming language and MatLab so in my opinion it is better to use Python itself.
Unknown said…
If you don't like matlab it can be hard to like a Python. But thank you for your time and I hope that it was although a bit interesting for you! :)
Unknown said…
It was interesting article and you did a great job.
No, I not interested at sound so much to know what is reverberation time.
I studied only basics of Python in university when I had classes related to operations on data. It’s interesting language but it’s unusual for me. I'm more used to object-oriented programming languages such as Java or C#. I know that usually Python used for complex math calculations because it has powerful libraries to work with.
No, I hadn’t such unusual cases in my job. I’ve heard about R language, that it also used for math calculations and data analysis, but I don’t know for sure can it handle audio input or no.
Unknown said…
I'm happy that you liked my article and it encourages me to write next one. But now I have to focus on the Engineering Thesis :D I have to admit that I have never heard much about the R language so I will have to take a closer look at this.
It was very interesting article. I heard something but didn't pay attention to it. But after this article, I am interested to read more about it. Once I tried to program in Python, but it was very simple programs, nothing interesting. I think C++ could deal with this program. It will take more time and code lines, but it will work faster. Unfortunately, no. At work I am engaged in support of the system.
Marcin Zając said…
1. I never heard about reverberation time. I don't have much experience with audio analysis.

2. I haven't any experience with ruby. I'm currently learning python, becouse I need use neural networks in my current project, but I haven't any special plugins to share. I use tensorflow (slim). My main programming languages are PHP and java.

3. I think python is a good choice. If you would like to increase performance, you can try c/c++, but it will probably take longer to write.

It's very interesting article, which shows one of the main adventages of coding for me: you can obtain domain knowledge.
Unknown said…
Thank you very much for your attention and I am very happy that you like my article. It is really motivating for me to write next one. The next one will be about Docker, Kubernetes and Google Cloud Platform so maybe it will be also quite enjoyable. :)
Maciej Nowak said…
Obviously I have heard about reverberation but not about reverberation time. I don't have any experience with programming in Python, as I write in C#, however, I might try it out just for fun after finishing PJATK. I don't know if you would find library for your problem in .NET and I don't think that I had any unusual cases in my job, at least in comparison to yours.
Illia Lukisha said…
1. Have you ever heard about reverberation time?
Yes, I did, somewhere on physics classes.

2. Do you have any experience with programming in python or ruby language? Maybe can you share with us any interesting libraries or plugins?
I have experience in both. I started my career programming in ruby for popular framework Ruby on Rails. Also, I wrote my bachelor thesis program, in python. I used it for machine learning, because python is the best language for data analysis.

3. Do you have an idea which programming language could also deal with case as described in the article? Have you ever had such unusual cases in you job?
I think c/c++/java can deal with this task as well.
Unknown said…
1. I should have heard about reverberation time as I hold BSc in Telecommunications Engineering but I don't remember what is it as I work as a software engineer and have nothing to do with signal processing.
2. I have some academic experience with Python, during MSc course I have used it for NLP and Big Data projects with aforementioned libraries like Keras, NumPy, Pandas or NLTK.
3. I think there are some libraries suited to this task available in popular programming languages like C++, Java or MATLAB
As a musician, I heard a lot of about reverberation time. I am a guitar player and a reverb means a lot in this area. I also have a reverb guitar pedal too :)
As a programmer, I am a data scientist and I work with AI libraries day to day. I have experience with programming in python >1 year. Libraries, which I use in everyday tasks are: tensorflow/keras, scikit learn and seaborn(for visualization). Also, I was thinking to make my graduate work dealing with sound analysis, but finally I chose a classification problem.
In my job, I have never deal with music and sound analysis. Mostly I have a classification or predicting problems.
lukejamie07 said…
Thank you for sharing such informative post.Nice post.

NEMT Dispatch Software
swati parmar said…
This is such a nice post which you shared here. it really helps me out. Keep sharing posts like these.

Popular posts from this blog

Week 12 (12.01-18.01.15) Are you an early bird or a night owl ?

Owls are nocturnal creatures. They’re wide awake at night and they sleep during the day. If this sounds like bliss to you, then, like about 20 percent of the population who find themselves most active at around 9 pm, you may fall into the same category as our feathered friend. Night owls often have difficulty waking up in the morning, and like to be up late at night.  Studies of animal behaviour indicate that being a night owl may actually be built into some people’s genes. This would explain why those late-to-bed, late-to-rise people find it so difficult to change their behaviour. The trouble for night owls is that they just have to be at places such as work and school far too early. This is when the alarm clock becomes the night owl’s most important survival tool. Experts say that one way for a night owl to beat their dependence on their alarm clocks is to sleep with the curtains open. The Theory is that if they do so, the morning sunlight will awaken them gently and natura...

Week 11 [03-09.06.2019] The problem with ecological cars emission in UK

The problem with ecological cars emission in UK Since the adoption of the European Emission Allowance Directive in the European Parliament, all car makers have tried to submit. Since 1992, the Euro I standard has been in force, which limited the emission of carbon monoxide to the atmosphere. The Euro VI standard currently applies, which limits the series of exhaust gases. These include: hydrocarbons, nitrogen and carbon oxides, and dust.   The most significant change was brought by the Euro IV standard. For the first time it introduced the limitation of nitrogen oxides, which are responsible for the harmful compounds of smog.   What is smog?   Smog consists of sulfur oxides, nitrogen and carbon. In addition, solid substances such as suspended dust (PM). Dust suspend in atmospheric aerosols may be in liquid and solid form. These can be particles of sea salt, clouds from the Sahara and artificial compounds made by people. These compounds...

Week 4 [06-12.11.2017] This is what happens when you reply to spam email.

James Veitch is a British comedian. In today’s Ted Talk James with characteristic for himself a sense of humor shows how he deals with spam emails and why responding to junk messages may be sometimes dangerous. Questions: What do you think about James’s  way of dealing with spam? Why are junk messages legal, even though it sometimes may be a fraud? Dou you have a problem with spam? How do you deal with with it?