# relationship of column of spectrogram to original signal

We can get the spectrograms of a signal which is a 2D matrix. How do I get back from the columns of the spectrogram to the time stamps in original signal? For example, what time stamp does the columns index ‘x’ belongs to (i.e. 1.3 sec in the file)?

I do not want the re-construction of the original signal, only relation between column number in spectrogram to a timestamp in the original signal.

So in MATLAB:

[X, fs] = audioread('file.wav');

1. Create spectrogram:

[S, F, T, spec] = spectrogram(X, 128, 64, 256, fs); where 128 window length, 64 is overlap, 256 is number of fft bins and fs is sampling frequency 16KHz.

spec is a 2D matrix, in which the rows represents the frequency and columns represents the time.

Details: X is a one dimensional array which represents the samples of the file.wav. The file.wav is say 3.5 seconds duration and spec will be of 129 x 700 dimension matrix.

Question: I want to know the correspondence of the columns number of the spec with the timestamp of the file.wav. E.g columns number 100 corresponds to 0.75 seconds in the original file file.wav.

• As we do not know what you have coded and what your results are we wont be able to help you. Please add your code and a minimum example of the input / output behavior. – Irreducible Dec 4 '17 at 14:08
• Doesn't the X axis of your 2D spectrogram matrix already represent the time? – dsp_user Dec 4 '17 at 14:25
• I have added some more explanation to the question – AKK Dec 4 '17 at 15:46

[s,w,t] = spectrogram(___)

t Time instants, returned as a vector. The time values in t correspond to the midpoint of each segment.

In your example, since the window length is 128 samples = 8 ms at 16kHz, the first column corresponds to a timestamp of 4 ms. Since you have 50% overlap between windows, the subsequent columns have timestamps of 8 ms, 12 ms, and so on.

In general, suppose you have a window length of $N$ samples, overlap of $M<N$ samples and a sampling frequency $f_s$ samples/second. We can derive the location of the midpoint of the $k^{th}$ window with a little bit of algebra. First we note that the left edge of two consecutive windows differ in time by $\frac{N-M}{f_s}$ seconds. This implies that the left edge of the $k^{th}$ window is at $\frac{(k-1)(N-M)}{f_s}$ seconds. Since each window has a length of $\frac{N}{f_s}$, the midpoint of the $k^{th}$ window is at $\frac{N}{2f_s} + \frac{(k-1)(N-M)}{f_s}$ seconds.

Or if you want to be lazy just use the values returned in the t vector by Matlab.

You can double check that this formula is correct using Matlab.

x = randn([32000,1]); % generate a random data vector
fs = 16000;
N = 128;
M = 55;
NFFT = 256;
[S,F,T] = spectrogram(x, N, M, NFFT, fs);
T_formula = N/fs/2 + (0:length(T)-1)*(N-M)/fs;
disp( sqrt(sum((T-T_formula).^2)) ) % should be practically zero


On my machine this prints 2.7417e-31 which means the two time vector are practically identical.

• Thank you very much for the explanation. Can you give me the source of the formula $(\frac{1}{2} + (k−1)(1−p)) \frac{N}{fs}.$ Any paper/article or book etc. – AKK Dec 4 '17 at 18:22
• See updated answer. I added a derivation of the formula. – Atul Ingle Dec 4 '17 at 19:44
• Note that the formula can also be written as $(\frac{1}{2} + (k-1)(1-\frac{M}{N})\frac{N}{f_s}$. Here $p:=M/N$ is the overlap fraction. – Atul Ingle Dec 4 '17 at 20:16
• Thanks a lot for the explanation. Can you write it back for converting timestamps to bins. I derive this but is not equal to the original bins. $$k = \frac{\frac{x_t * f_s}{N} + \frac{1}{2} - p}{1 - p}$$ – AKK Dec 12 '17 at 15:51
• where k is the obtained bin and x_t is the input timestamp in seconds – AKK Dec 12 '17 at 16:37