I have a signal in Malab, and I need a matrix, probably transposed specgram matrix without using specgram tool. Is it possible?

  • 1
    $\begingroup$ Yes, this is possible. You just have to calculate the spectrogram manually, using a windowed short time fourier transform. However just saying you need a spectrogram is not enough information. You need to specify several parameters like the window, its size, the overlap. $\endgroup$
    – Jazzmaniac
    Nov 20 '13 at 20:04
  • $\begingroup$ Thanks for sharing this source code for spectrogram. Would you please explain that what does this line exactly do? windowed = framed .* hann(length(framed)); and what is chunk for? if i re sample my audio to 200 fs and 20 ms , how its ganna change? num is a vector or matrix? i saw the num contain in matlab, it is a row with 1000 columns... Forgive me for such a simple question but i am a newbie and i would appreciate if you guide me . Thanks a million $\endgroup$
    – drizzle
    Nov 25 '13 at 14:18

Yes, the easiest way to do this is using reshape and FFT, since reshape will give you a matrix and the FFT will operate along a dimension of the matrix. I've applied a 1024 rectangular window on a data set of 65536 points, sampling frequency Fs (1024), and signal frequency (100).

x = randn(1, 65536) + cos(2*pi*100/1024*(0:65536-1));
z = reshape(x, 1024, []);
y = 20*log10(abs(fft(z)));

This example doesn't have any overlap, but all you would need to do is shift z then concatenate it with z prior to taking the FFT. This all depends on your overlap percentage.

z = [z; circhift(z, [0 -1])];

This is not complete given the first column is now at the end, so you would have to omit that column, but you get the idea.


Do you need choose one chunk size, one window type, overlap percentage and of course use the matlab function FFT to returns the discrete Fourier transform, here a simple spectrogram using chunk = 2048, 50% of overlap and a hann window.


CHUNK = 2048


num = [CHUNK : OVERLAP :length(x)];

SPEC = zeros( length(num), NFFT/2 );

for i=length(num):-1:1,
    framed = x( (num(i)-CHUNK) + 1 : num(i) );
    windowed = framed .* hann(length(framed));
    Fourrier = fft(windowed, NFFT);
    SPEC(i,:) = log(abs(Fourrier(1:NFFT/2)));




a little explanation

When I do windowed = framed .* hann(length(framed)) Im applying a window over the signal, this step decreasing the amplitude component of the ends, this is used to avoid the appearance of spurious high frequency components of the Fourier transform, if you plot a Hann window you can see a similar Gaussian format !

The "CHUNK" is the slice cut audio, for this example I'm getting 2048 samples, and at each slice I'm overlapping 50% of samples!

The "num" are the linear calc of the position when cut the signal to analyses


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