How can I change the pspectrum time units?

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

t = seconds(0:length(x)-1)/fs;
will fix it.

4 Comments

Thanks for the reply but unfortuntely this didn't resolve the problem
[x, fs] = audioread(filename1);
x = x(:,1);
t = seconds(0:length(x)-1) / fs;
xTable = timetable(t',x);
figure
ax1 = subplot(2,1,1);
stackedplot(xTable)
ax2 = subplot(2,1,2);
pspectrum(xTable,'spectrogram','OverlapPercent',0, ...
'Leakage',1,'MinThreshold',-80)
I never used stackedplot; i presumed it would have a duration axis and you could set the units for the axis in the plot.
Unfortunately, it's another case where TMW has built the object but won't let you get at the internals. :(
So, looks like you'll have to use
t = seconds(0:length(x)-1)/fs;
t.format='m';
xTable = timetable(t',x);
to get stackedplot to behave as desired.
Why/how the above gives min on the spectrum plot is a mystery to me...
I would probably just dispense with the timetable and just use subplot and plot of the duration instead and have control over how I wanted it to look directly instead of by subterfuge.
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.
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

Asked:

on 26 Jun 2020

Edited:

dpb
on 27 Jun 2020

Community Treasure Hunt

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

Start Hunting!