How can I change the pspectrum time units?

3 views (last 30 days)
David Moore
David Moore on 26 Jun 2020
Edited: dpb on 27 Jun 2020
Is it possible to change the default time units for a pspectrum spectrogram? In the followig I'd like the time in the time domain view of my waveform (subplot 1) to match the time units from my spectrogram (subplot 2). Seems something that should be easy to change but I can't for the life of my figure it out!
[x, fs] = audioread(filename1);
x = x(:,1);
t = (0:length(x)-1)/fs;
xTable = timetable(seconds(t'),x);
figure
ax1 = subplot(2,1,1);
stackedplot(xTable)
ax2 = subplot(2,1,2);
pspectrum(xTable,'spectrogram','OverlapPercent',0, ...
'Leakage',1,'MinThreshold',-80,)
colorbar(ax2,'off')

Accepted Answer

dpb
dpb on 26 Jun 2020
t = seconds(0:length(x)-1)/fs;
will fix it.
  4 Comments
David Moore
David Moore on 27 Jun 2020
Thanks again.
I went the route of subplot and plot initially. However, I'm still unable to adjust the spectrogram x-axis time units no matter which way around this I go, with timetable or without.
dpb
dpb on 27 Jun 2020
Edited: dpb on 27 Jun 2020
Works as expected here if set the time to desired units first...I loaded the demo/example file Handel.mat that is about 9 sec of audio data, then computed the time vector but multipled by 60 to pretend it was minutes instead of seconds for illustration...that was the variable tvec in following. Gives a plot of similar type as yours.
Fs=8192; deltT=1/Fs; T=numel(y)/Fs; % sampling rate from Handel demo
tvec=0:deltT:T; tvec=tvec(1:end-1); % build a time vector to match
figure; plot(tvec,y) % just show what it is as double in sec
OK, that demo to illustrate, let's work on the output format for your problem...
tvec=tvec*60; % make an artificial minutes-long record
tty=timetable(tvec,y); % create the timetable
tty.Properties.RowTimes.Format='m'; % set the format to minutes
hAx1=subplot(2,1,1); % get ready to make demo plots...
stackedplot(tty) % use stackplot first
hAx2=subplot(2,1,2); % and a second to compare
plot(seconds(tvec),y) % plot the underlying time as duration
hAx2.XAxis.TickLabelFormat='m'; % also set the minutes for time axis...
dixp('Enter' to continue');pause % and wait to go on...
hAx2.XAxis.TickLabelFormat='s';
Above produces following figure after the pause -- you'll see the units swap after go on...without a lot of digging as did on the other Q? the other day at <<Answers/554917-heatmap-axis-labels>> to see if can manage to get to an underlying axis, the only way with stackedplot appears to be to set the format first; the plot is too opaque.

Sign in to comment.

More Answers (0)

Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!