How can I change the pspectrum time units?
조회 수: 16 (최근 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')
댓글 수: 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.
추가 답변 (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!