Adding datetime to spectrogram x axis

6 views (last 30 days)
I have six acoustic files which were recorded consecutively.
I have concatenated the data and shared it here: https://drive.google.com/drive/folders/11ZvIH6JhLJNDe_NrqIXJiM7EPqa08-7r?usp=sharing
I have plotted a spectrogram, but the x axis is in seconds and I would like it to be in a readable format, corresponding to the datetime LOCALDT variable.
% FFT parameters
Fs=48000;
dt = 1/Fs;
NFFT = 1624;
OVERLAP = 0.75;
% spectrogram dB scale
spectrogram_dB_scale = 80; % dB range scale (means , the lowest displayed level is XX dB below the max level)
%Time/frequency analysis (repeat for each axis)
signal=cell2mat(sample.xacc); %concatenate accel. data for all chunks
[samples,~] = size(signal);
[sg,fsg,tsg] = spectrogram(signal,hanning(NFFT),floor(NFFT*OVERLAP),NFFT,Fs);
sg_dBpeak = 20*log10(abs(sg))+20*log10(2/length(fsg)); % NB : X=fft(x.*hanning(N))*4/N; % hanning only
% saturation of the dB range :
% saturation_dB = 60; % dB range scale (means , the lowest displayed level is XX dB below the max level)
min_disp_dB = round(max(max(sg_dBpeak))) - spectrogram_dB_scale;
sg_dBpeak(sg_dBpeak<min_disp_dB) = min_disp_dB;
% plots spectrogram
imagesc(tsg,fsg,sg_dBpeak); %time, frequency, colour
colormap('jet');
axis('xy');%colorbar('vert');
colorbar
  1 Comment
Louise Wilson
Louise Wilson on 15 Feb 2022
I have since realised I can do it like this...
xlim([0 30])
xticks([0 5 10 15 20 25 30])
xticklabels({'09:58:25','09:58:30','09:58:35','09:58:40','09:58:45','09:58:50'})
but I wonder if there is a more intuitive way...

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Feb 2022
The spectrogram function uses surf to create its plots. In R2021b it accepts datetime values for the axis coordinates (see the documentation for X) and according to the online documentation, R2020b (X) supports it as well.
.
  8 Comments
Louise Wilson
Louise Wilson on 16 Feb 2022
It's in the third line of the OP. I couldn't add the file here, it's 20 MB.
Star Strider
Star Strider on 16 Feb 2022
Thank you.
I just didn’t notice it.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!