MCPcopy Index your code
hub / github.com/tyiannak/pyAudioAnalysis / spectrogram

Function spectrogram

pyAudioAnalysis/ShortTermFeatures.py:389–452  ·  view source on GitHub ↗

Short-term FFT mag for spectogram estimation: Returns: a np array (numOfShortTermWindows x num_fft) ARGUMENTS: signal: the input signal samples sampling_rate: the sampling freq (in Hz) window: the short-term window size (in samples)

(signal, sampling_rate, window, step, plot=False,
                show_progress=False)

Source from the content-addressed store, hash-verified

387
388
389def spectrogram(signal, sampling_rate, window, step, plot=False,
390 show_progress=False):
391 """
392 Short-term FFT mag for spectogram estimation:
393 Returns:
394 a np array (numOfShortTermWindows x num_fft)
395 ARGUMENTS:
396 signal: the input signal samples
397 sampling_rate: the sampling freq (in Hz)
398 window: the short-term window size (in samples)
399 step: the short-term window step (in samples)
400 plot: flag, 1 if results are to be ploted
401 show_progress flag for showing progress using tqdm
402 RETURNS:
403 """
404 window = int(window)
405 step = int(step)
406 signal = np.double(signal)
407 signal = signal / (2.0 ** 15)
408 signal = dc_normalize(signal)
409
410 num_samples = len(signal) # total number of signals
411 count_fr = 0
412 num_fft = int(window / 2)
413 specgram = np.zeros((int((num_samples-window) / step) + 1, num_fft),
414 dtype=np.float64)
415 for cur_p in tqdm(range(window, num_samples - window + 1, step),
416 disable=not show_progress):
417 count_fr += 1
418 x = signal[cur_p:cur_p + window]
419 X = abs(fft(x))
420 X = X[0:num_fft]
421 X = X / len(X)
422 specgram[count_fr-1, :] = X
423
424 freq_axis = [float((f + 1) * sampling_rate) / (2 * num_fft)
425 for f in range(specgram.shape[1])]
426 time_axis = [float(t * step) / sampling_rate
427 for t in range(specgram.shape[0])]
428
429 if plot:
430 fig, ax = plt.subplots()
431 imgplot = plt.imshow(specgram.transpose()[::-1, :])
432 fstep = int(num_fft / 5.0)
433 frequency_ticks = range(0, int(num_fft) + fstep, fstep)
434 frequency_tick_labels = \
435 [str(sampling_rate / 2 -
436 int((f * sampling_rate) / (2 * num_fft)))
437 for f in frequency_ticks]
438 ax.set_yticks(frequency_ticks)
439 ax.set_yticklabels(frequency_tick_labels)
440 t_step = int(count_fr / 3)
441 time_ticks = range(0, count_fr, t_step)
442 time_ticks_labels = \
443 ['%.2f' % (float(t * step) / sampling_rate) for t in time_ticks]
444 ax.set_xticks(time_ticks)
445 ax.set_xticklabels(time_ticks_labels)
446 ax.set_xlabel('time (secs)')

Callers

nothing calls this directly

Calls 1

dc_normalizeFunction · 0.85

Tested by

no test coverage detected