How can I change the pspectrum time units?
10 ビュー (過去 30 日間)
古いコメントを表示
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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/322393/image.jpeg)
0 件のコメント
採用された回答
dpb
2020 年 6 月 26 日
t = seconds(0:length(x)-1)/fs;
will fix it.
4 件のコメント
dpb
2020 年 6 月 27 日
編集済み: dpb
2020 年 6 月 27 日
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/322753/image.jpeg)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Time-Frequency Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!