On the internet, I have seen people use Matplotlib/Python and short-time fourier transformations to create spectrograms. Are their other formula/algorithms to create spectrograms? And are they possible to implement in python or be called through command prompt?

  • $\begingroup$ i have MATLAB code that opens an audio file and creates a spectrogram using surf(). is that what you're looking for? it's pretty simple. $\endgroup$ – robert bristow-johnson Jun 21 '19 at 3:52
  • $\begingroup$ @robertbristow-johnson I was asking about a command line called exe/program that can use different algorithm/formulas to create a spectrogram. I notice that they all seem to use short-time Fourier transformations, and I was wondering if there were different formulas. $\endgroup$ – itisyeetimetoday Jun 21 '19 at 4:32
  • $\begingroup$ there all pretty much the same formula. different windows, different frame widths and different frame hop distances. $\endgroup$ – robert bristow-johnson Jun 21 '19 at 5:01
  • $\begingroup$ @LaurentDuval No, but thanks for the advice. My final project was too similar compared to Melnet, using spectrogram for speech synthesis, to be worth pursuing for ISEF. $\endgroup$ – itisyeetimetoday Jul 29 '19 at 4:16
  • $\begingroup$ Do not hesitate then to upvote answers and validate the most useful one $\endgroup$ – Laurent Duval Jul 29 '19 at 20:43

so here's a short MATLAB script that will compute and draw a spectrogram.

if ~exist('inputFile', 'var')
    inputFile = 'tom_hit.wav';

if ~exist('frameLength', 'var')
    frameLength = 8192;
frameLength = 2.^(ceil(log2(frameLength) - 1e-10));

if ~exist('frameHop', 'var')
    frameHop = 256;
frameHop = 2.^(floor(log2(frameHop) + 1e-10));
frameHop = min(frameHop, frameLength/2);                                    % insure at least 50% overlap

[inputBuffer, Fs] = audioread(inputFile);

[inputFileSize, nChannels] = size(inputBuffer);

nSamples = 2.^(ceil(log2(inputFileSize + frameLength) - 1e-10));            % zero-pad on both sides and round up to nearest power of 2

nFrames = floor((nSamples-frameLength)/frameHop);

x = zeros(nSamples, 1);
x(frameLength/2+1:frameLength/2+inputFileSize) = inputBuffer(:,1);          % use left channel only, zero-pad half frame on both sides

clear inputBuffer;                                                          % free this memory

dB_floor = -90;

beta = 5;
frameWindow = gausswin(frameLength, beta);                                  % gaussian window

XX = zeros(nFrames, frameLength/2+1);

for frame = 1:nFrames

    X = fft( fftshift( x((frame-1)*frameHop+1:(frame-1)*frameHop+frameLength, 1).*frameWindow ) );

    XX(frame,:) = abs( X(1:frameLength/2+1) ).^2 + 10^(dB_floor/10);        % leave XX as energy with dB_floor dB floor.


[taxis, faxis] = meshgrid(0:Fs/frameLength:Fs/2, 0:frameHop/Fs:(nFrames-1)*frameHop/Fs);
surf(faxis, taxis, 10*log10(XX), 'EdgeColor','none', 'LineStyle','none');

Sometimes people use time-frequency representations (TFRs) as a generalization of the spectrogram.

This C code calculates some different TFRs of Cohen’s class: bilinear TFRs.


There are spectral analyzers that come with executables (and sometimes source code) like:

Even FFMpeg can do the job: ffmpeg -i inputfile.mp3 -lavfi showspectrumpic=s=800x400:mode=separate spectrogram.png

The basic formula is typically composed of:

  • select a frame, and apply a window,
  • compute a Fourier transform,
  • display magnitude or square magnitude values.

as detailed by Robert. As Peter wrote, there are many extensions or reformulation, termed time-frequency distributions or time-scale representations, that can be implemented as filter banks, quadratic formulae, etc. Among script language implementations, you can check:

This is still an open research topic.


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.