Adding datetime to spectrogram x axis

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 件のコメント

Louise Wilson
Louise Wilson 2022 年 2 月 15 日
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...

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2022 年 2 月 15 日

0 投票

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 件のコメント

Louise Wilson
Louise Wilson 2022 年 2 月 15 日
Thanks Star Strider! I saw in a comment of yours elsewhere that I can use spectrogram to plot, like this:
spectrogram(signal,hanning(NFFT),floor(NFFT*OVERLAP),NFFT,Fs,'yaxis')
and then the x axis is in minutes.
My problem then is that I don't know how to input datetime into the spectrogram function, to maintain a separate datetime for each chunk of acoustic data?
The output of spectrogram gives me the time in seconds, so before even getting to plotting with surf, I have the time info. in the wrong format. I'm not sure how to specify the time format I want in spectrogram?
Star Strider
Star Strider 2022 年 2 月 15 日
My pleasure!
It is certainly possible to create a datetime array with only seconds. It will be necessary to specify the correct input format as demonstrated in Date and Time from Character Vectors so datetime will know how to interpret them and what to do with the values.
For example, using the xticklabels vector you posted —
xtl = {'09:58:25','09:58:30','09:58:35','09:58:40','09:58:45','09:58:50'}';
dtxt = datetime(xtl, 'InputFormat','HH:mm:ss', 'Format','HH:mm:ss')
dtxt = 6×1 datetime array
09:58:25 09:58:30 09:58:35 09:58:40 09:58:45 09:58:50
Then use xtickformat to format them correctly, if necessary afterwards.
.
Louise Wilson
Louise Wilson 2022 年 2 月 15 日
Thanks! So I think you are suggesting I plot like this:
surf(tsg,fsg,sg_dBpeak)
after converting the x data from seconds to datetime.
To check it's all good, I tried this before messing around with the x data, and the surf plot looks like this...!
Star Strider
Star Strider 2022 年 2 月 16 日
Eliminate the completely black effect wiith:
surf(tsg,fsg,sg_dBpeak, 'EdgeColor','none')
I do not have your data, so I cannot run the code to check the axis labels.
.
Louise Wilson
Louise Wilson 2022 年 2 月 16 日
Thanks Star. This works! I included the data in the OP just so you know.
Star Strider
Star Strider 2022 年 2 月 16 日
As always, my pleasure!
If the google drive link was posted previously, I didn’t notice it. Was the file too large to upload here?
Louise Wilson
Louise Wilson 2022 年 2 月 16 日
It's in the third line of the OP. I couldn't add the file here, it's 20 MB.
Star Strider
Star Strider 2022 年 2 月 16 日
Thank you.
I just didn’t notice it.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMeasurements and Spatial Audio についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by