Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log scale a 2D Matrix / Image

I have a 2D numpy array of a audio spectrogram and I want to save it as an image.

I'm using librosa library to get the spectrum. And I can also plot it using librosa.display.specshow() function. There are number of different scaling types as you can see below.

import PIL
import librosa
import librosa.display

def display_spectrogram(spectrum, sampling_rate):
    """
    Frequency types:
    ‘linear’, ‘fft’, ‘hz’ : frequency range is determined by the FFT window and sampling rate.
    ‘log’ : the spectrum is displayed on a log scale.
    ‘mel’ : frequencies are determined by the mel scale.
    ‘cqt_hz’ : frequencies are determined by the CQT scale.
    ‘cqt_note’ : pitches are determined by the CQT scale.
    """

    librosa.display.specshow(spectrum, sr=sampling_rate, x_axis='time', y_axis='log')
    plt.colorbar(format='%+2.0f dB')
    plt.title('Spectrogram')
    plt.show()

I can also transform the spectrogram (a numpy array) to an image and save like below.

img = PIL.Image.fromarray(spectrum)
img.save("out.png")

I have the original spectrogram (linear scaled) and I want to save it with y-axis in log scale. I looked into the library's source code in order to understand how it scaled but cannot figure it out.

How can I log scale the y-axis of an image / 2D numpy array ?


linear matrix log scaled result

like image 341
enesdemirag Avatar asked Sep 17 '25 14:09

enesdemirag


1 Answers

The actual log-transform of the Y axis is done by matplotlib. You can test this by doing ax.set_yscale('linear') vs ax.set_yscale('linear'). So the easiest alternative would be to tweak the matplotlib figure to remove ticks, borders etc. Here is one example of that: https://stackoverflow.com/a/37810568/1967571

If you want to do the log-scaling yourself, the steps are

  • Compute the current frequencies on Y axis. Using librosa.fft_frequencies
  • Compute the desired frequencies on Y axis. Using numpy.logspace or similar
  • Sample the spectrogram at the desired frequencies, using for example scipy.interpolate (interp1d)
like image 102
Jon Nordby Avatar answered Sep 19 '25 04:09

Jon Nordby