I am calculating spectrogram of a audio file of 36 second using the following code snippet:

window = np.hamming(window_width)
length = signal.size
count = 0
for i in range(0, length-window_width, window_overlap):
    cur_frame = signal[i:i+window_width]
    window_frame = cur_frame*window
    spec = sp.fft(window_frame)
    if(i == 0):
        width = int(length/window_overlap)
        height = int(spec.size)
        specgram = np.empty([height, width])
        energy = np.empty(width)
    energy[count] = getEnergy(spec)
    for j in range(height):
        val = spec[j]       
        specgram[j, count] = math.sqrt(math.pow(val.real, 2)+math.pow(val.imag, 2))
    count = count+1
print abs(specgram).max()

I get a ndarray of shape (4000L, 1008L)

but when i calculate spectrogram using Pylab:

Pxx, freqs, bins, im=pylab.specgram(sound_info, Fs=frame_rate)

I get ndarray of dimension: (129L, 125999L).

Actually I need to get the spectrogram which is the dimension of (frequency,nframes)

Thank you in advance.

  • 2
    $\begingroup$ What is the question exactly? Figure out what kind of frame rate (= overlap) and resolution (= FFT size) you want, and pass the appropriate parameters to specgram or modify your code accordingly. $\endgroup$ – pichenettes Jul 31 '13 at 10:19

How @Pichenettes said, we need more information to help properly

I'll take a guess:

If your Frame Rate are 44100, you will have 1587600 samples for 36 second of audio (36*44100), for the calculation example:

Pxx, freqs, bins, im=pylab.specgram(sound_info, Fs=frame_rate)

pylab.specgram seems use FFT Size = 256 and Overlap = 128 (50%) its give me the follow dimensional array:

floor(1587600 / 128) = 12403

(128, 12403)

You need set properly FFT Size and Overlap in your souce code.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.