## measure time from fft spectrogram

### LO (view profile)

on 12 May 2019
Latest activity Commented on by LO

on 13 May 2019

### Greg Dionne (view profile)

trying to convert the time scale of a signal visualized on a spectrogram in sec/msec.
the spectrogram has been calculated using known values of
1) sampling frequency (20k)
2) window size (in samples) (1024)
3) overlap (0.9)
4) total duration of the recording in samples (tspec = 11755)
5) total duration of the recording in seconds (60)
calculation of the total duration in seconds: total duration (samples) / total duration (seconds)
-> length(tspec)/60 = 195.9167
calculation of the signal duration in sec = duration of signal in samples / 195.9167
is this correct or is there any smearing of the signal time units due to nfft and overlap ?

### Greg Dionne (view profile)

on 13 May 2019

You can extract the time vector from the third output of the spectrogram function.
[S,F,T,Pxx] = spectrogram(X,...)
F is the frequency vector corresponding to the rows in S and Pxx.
T is the time vector corresponding to the columns of S and Pxx.
The time and frequencies correspond to the center of each estimate in S and Pxx; the resolution of the estimates in time and frequency are governed by the choice of window and overlap used. If there aren't enough samples to complete a full FFT at the end of the input signal, X, then the result is truncated.

LO

### LO (view profile)

on 13 May 2019
then - if I have an event measured only using samples and freq (the output of medfreq for instance, although the input of it is freq and time intervals it gives samples in output) - the correct conversion would be
length(tspec)/60 = 195.9167 --> samples per sec
event duration in sec = duration in samples / 195.9167
with no other adjustments due to nfft and overlap... is that right ?
Greg Dionne

### Greg Dionne (view profile)

on 13 May 2019
That sounds right... but perhaps an example would work better to explain how the NFFT and overlap affect the location of the time estimates:
If you load an audio clip and take a spectrogram with settings comparable to yours (1024 size FFT 90% overlap (921 samples), you can inspect the output for more information on the time and frequencies that correspond to the spectral estimates in Pxx.
First, load the sample file and view its length.
>> size(mtlb)
ans =
4001 1
>> Fs
Fs =
7418
So the signal is 4001 samples / (7418 samples/second) = 0.5394 seconds.
If you choose an FFT length of 1024 samples, that means you are inspecting 0.138 seconds worth of data in each FFT. An overlap of 90% (921 samples) means that you obtain estimates every 1024 - 921 = 103 samples or 0.0139 seconds.
Let's take the spectrogram now with those settings:
[S,F,T,Pxx] = spectrogram(mtlb,kaiser(1024,10),921,1024,Fs,'power');
>> size(Pxx)
ans =
513 29
>> size(F)
ans =
513 1
>> size(T)
ans =
1 29
So you have 29 estimates in time. If you inspect the time vector you will see:
>> T
T =
Columns 1 through 11
0.0690 0.0829 0.0968 0.1107 0.1246 0.1384 0.1523 0.1662 0.1801 0.1940 0.2079
Columns 12 through 22
0.2218 0.2356 0.2495 0.2634 0.2773 0.2912 0.3051 0.3190 0.3328 0.3467 0.3606
Columns 23 through 29
0.3745 0.3884 0.4023 0.4161 0.4300 0.4439 0.4578
You will see that the first estimate is centered within your FFT window (0.069 = 0.138/2). The next estimate is a difference of 0.0829 - 0.069 = 0.0139, which matches the time interval between estimates. These estimates continue until the last time where a complete FFT can be made. This is the last estimate (0.4578) which contains data up to 0.4578 + 0.138/2 = 0.5268 seconds. The last 12.6 milliseconds of data from (0.5268 to 0.5394 seconds) are discarded since it does not comprise a complete FFT record.
Hope this helps.
-Greg
LO

### LO (view profile)

on 13 May 2019
absolutely! thanks for clarifying all this. I know these are basics. but it helped :)