2
votes

I want to convert the .wav file into a Spectrogram.

So I used this Python file.

import glob
import numpy as np
from matplotlib import pyplot as plt
import scipy.io.wavfile as wav
from numpy.lib import stride_tricks

""" short time fourier transform of audio signal """
def stft(sig, frameSize, overlapFac=0.5, window=np.hanning):
    win = window(frameSize)
    hopSize = int(frameSize - np.floor(overlapFac * frameSize))

    # zeros at beginning (thus center of 1st window should be for sample nr. 0)
    samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)
    # cols for windowing
    cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1
    # zeros at end (thus samples can be fully covered by frames)
    samples = np.append(samples, np.zeros(frameSize))

    frames = stride_tricks.as_strided(samples, shape=(cols, frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy()
    frames *= win

    return np.fft.rfft(frames)    

""" scale frequency axis logarithmically """    
def logscale_spec(spec, sr=44100, factor=20.):
    timebins, freqbins = np.shape(spec)

    scale = np.linspace(0, 1, freqbins) ** factor
    scale *= (freqbins-1)/max(scale)
    scale = np.unique(np.round(scale))

    # create spectrogram with new freq bins
    newspec = np.complex128(np.zeros([timebins, len(scale)]))
    for i in range(0, len(scale)):
        if i == len(scale)-1:
            newspec[:,i] = np.sum(spec[:,scale[i]:], axis=1)
        else:        
            newspec[:,i] = np.sum(spec[:,scale[i]:scale[i+1]], axis=1)

    # list center freq of bins
    allfreqs = np.abs(np.fft.fftfreq(freqbins*2, 1./sr)[:freqbins+1])
    freqs = []
    for i in range(0, len(scale)):
        if i == len(scale)-1:
            freqs += [np.mean(allfreqs[scale[i]:])]
        else:
            freqs += [np.mean(allfreqs[scale[i]:scale[i+1]])]

    return newspec, freqs

""" plot spectrogram"""
def plotstft(audiopath, binsize=2**10, plotpath=None, colormap="jet"):
    samplerate, samples = wav.read(audiopath)
    s = stft(samples, binsize)

    sshow, freq = logscale_spec(s, factor=1.0, sr=samplerate)
    ims = 20.*np.log10(np.abs(sshow)/10e-6) # amplitude to decibel

    timebins, freqbins = np.shape(ims)

    plt.figure(figsize=(15, 7.5))
    plt.imshow(np.transpose(ims), origin="lower", aspect="auto", cmap=colormap, interpolation="none")
    plt.colorbar()

    plt.xlabel("time (s)")
    plt.ylabel("frequency (hz)")
    plt.xlim([0, timebins-1])
    plt.ylim([0, freqbins])

    xlocs = np.float32(np.linspace(0, timebins-1, 5))
    plt.xticks(xlocs, ["%.02f" % l for l in ((xlocs*len(samples)/timebins)+(0.5*binsize))/samplerate])
    ylocs = np.int16(np.round(np.linspace(0, freqbins-1, 10)))
    plt.yticks(ylocs, ["%.02f" % freq[i] for i in ylocs])

    if plotpath:
        plt.savefig(plotpath, bbox_inches="tight")
    else:
        plt.show()

    plt.clf()


if __name__ == '__main__':
    path='../tf_files/data_audio/'

    folders=glob.glob(path+'*')
    for folder in folders:
        waves = glob.glob(folder+'/' + '*.wav')
        print (waves)
        if len(waves) == 0:
            continue
        for f in waves:
            #try:
            print ("Generating spectrograms..")
            plotstft(f)
            #except Exception as e:
                #print ("Something went wrong while generating spectrogram:")

However, the result was different from what I expected.

['../tf_files/data_audio/test_wav_files/22601-8-0-0_2(volume).wav', '../tf_files/data_audio/test_wav_files/22601-8-0-6_2(volume).wav', '../tf_files/data_audio/test_wav_files/518-4-0-0(volume).wav', '../tf_files/data_audio/test_wav_files/drill1.wav', '../tf_files/data_audio/test_wav_files/chunk0.wav', '../tf_files/data_audio/test_wav_files/siren2.wav', '../tf_files/data_audio/test_wav_files/bark2.wav', '../tf_files/data_audio/test_wav_files/bark3.wav', '../tf_files/data_audio/test_wav_files/14111-4-0-0_2(volume).wav', '../tf_files/data_audio/test_wav_files/drill2.wav', '../tf_files/data_audio/test_wav_files/22601-8-0-3_2(volume).wav', '../tf_files/data_audio/test_wav_files/siren1.wav', '../tf_files/data_audio/test_wav_files/siren3.wav', '../tf_files/data_audio/test_wav_files/518-4-0-3(volume).wav', '../tf_files/data_audio/test_wav_files/drill3.wav', '../tf_files/data_audio/test_wav_files/4910-3-0-0_2(volume).wav', '../tf_files/data_audio/test_wav_files/344-3-5-0(volume).wav', '../tf_files/data_audio/test_wav_files/bark1.wav', '../tf_files/data_audio/test_wav_files/344-3-1-0(volume).wav']

Generating spectrograms..

Traceback (most recent call last):

File "z_make_spectrogram.py", line 95, in plotstft(f) File "z_make_spectrogram.py", line 54, in plotstft s = stft(samples, binsize) File "z_make_spectrogram.py", line 13, in stft samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)

TypeError: 'numpy.float64' object cannot be interpreted as an integer Error in sys.excepthook:

Traceback (most recent call last): File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook from apport.fileutils import likely_packaged, get_recent_crashes File "/usr/lib/python3/dist-packages/apport/init.py", line 5, in from apport.report import Report File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in import apport.fileutils File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in from apport.packaging_impl import impl as packaging File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in import apt File "/usr/lib/python3/dist-packages/apt/init.py", line 23, in import apt_pkg

ModuleNotFoundError: No module named 'apt_pkg'

Original exception was: Traceback (most recent call last):

File "z_make_spectrogram.py", line 95, in plotstft(f) File "z_make_spectrogram.py", line 54, in plotstft s = stft(samples, binsize) File "z_make_spectrogram.py", line 13, in stft samples = np.append(np.zeros(np.floor(frameSize/2.0)), sig)

TypeError: 'numpy.float64' object cannot be interpreted as an integer

When the 13th line was fixed with this syntax, The same error occurred too.:

samples = np.append(np.zeros(np.floor(int(frameSize/2.0))), sig)

For reference, I am currently using the tensorflow 1.4.

Therefore, I am not sure if it is OK to change the numpy version to 1.11.

Is there a way to correct this error?

.

.

  • edited

I fixed 13th line.:

samples = np.append(np.zeros(frameSize//2), sig)

And, I got this result.

The same error still occurs and I don't know why.

1
Why do you want to use floats? frameSize should be an integer, and then if you use //2 you still get an integer. What's the point of floor if you have an int already? So many bad things here. - Matthieu Brucher
Apparently np.floor returns a float even when passed an integer. This fact doesn't seem to be well-documented (unless someone else can find a reference?). But as @MatthieuBrucher says, the call to np.floor is redundant, so you should just be able to do np.zeros(int(frameSize / 2)) - myrtlecat
@myrtlecat Oh, I just applied the command you gave me. I modified question for result. - hayeon
The new error has a different cause (i.e., warrants either a new question, or you should rewrite the question), but in essence, is the same. Look at where the error originates in your code: ` frames = stride_tricks.as_strided(samples, shape=(cols, frameSize), strides=(samples.strides[0]*hopSize, samples.strides[0])).copy(). Typically, the shape and the strides parameters should take integers. But a few lines before, you have cols = np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1. Again, np.ceil` returns a float: you'll have to convert it to an integer first before using. - 9769953

1 Answers

0
votes

Both your errors originate from the usage of numpy.floor or numpy.ceil. While not properly documented, these functions return floating points (even if the input is an integer array).
As you are using the resulting values in arguments that require integer input, you'll have to convert them to integer first (simply by casting them).

For the first error, you can use integer division instead (as suggested in a comment as well):

samples = np.append(np.zeros(frameSize//2), sig)

For the cols parameter, where you rely on numpy.ceil, there is no simple shortcut, and you should simply use

cols = int(np.ceil( (len(samples) - frameSize) / float(hopSize)) + 1)

instead.