# STFT Audio processing with zeros

In this paper they preprocess an audio file with a STFT and then apply two equations to the computed matrix.

I tried implementing all of it in python and used librosa for the STFT. The issue I encountered is, that the matrix includes a lot of low values or zeros. By the 2. equations including log those values get transformed to nan latest in the second step. As soon as there are nans involved all future aggregations I want to compute doesn't work.

Here is an example of the magnitude spectrum, cent spectrum(EQ1) and cent scale(EQ2):

What is the best way to solve such a problem? Do I have to transform the matrix I get from librosa further?

• Add a very small value to $X[k]$ before taking the $\log$. Something like $1e^{-8}$
– Jdip
Jul 3, 2023 at 12:53
• @Jdip I'm wondering, is this a common problem? Jul 3, 2023 at 13:12

The equation isn't entirely correct. Normally you would define the level as

$$L[k] = 20 \cdot \log_{10}\frac{|X[k]|}{X_{ref}}$$

where $$X_{ref}$$ is a suitable reference. Could be 1V (for dBV) 20$$\mu$$ Pa (for dBSPL), 1mW (for dBm), etc. Which reference to use depends on your application your setup and what information is available.

This definition can become a problem when $$X[k] = 0$$. In this case the level is $$-\infty$$ (not, NaN which is just a programming artifact). Whether this is a problem or not depends on what you want to do with the data.

If you don't want to deal with $$-\infty$$ you define the maximum dynamic range and add a small offset to your data. For example, if you want to limit your range to, say, 100dB you can do something like

$$L[k] = 20 \cdot \log_{10}\frac{|X[k]|+10^{5}\cdot \max\{|X[k]|\}}{X_{ref}}$$

I'm wondering, is this a common problem

Yes. Not so much with real physical data as this rarely contains values that are exactly zero but if often happens with mathematical models. A simple example is a first order highpass filter which has a gain of 0 at 0Hz (i.e. $$X[0] = 0$$)

• I want to implement the whole algorithmn of that paper. Basically it converts the cent scale into 6 different vectors which right now mostly consists of nans. My knowledge in signal processing is limited so I'm not sure if that is what the authors intended. Jul 4, 2023 at 16:59
• The main question I have is: Is it normal for a fft to output very small values? Jul 4, 2023 at 17:08
• @Rocket: It really depends on the nature of your signals,. For some class of signals its normal, for other's it isn't. Jul 5, 2023 at 0:51