How can I force the time-axis unit of spectrogram to be 'second(s)' ?

16 ビュー (過去 30 日間)
zhehao.nkd
zhehao.nkd 2021 年 5 月 25 日
コメント済み: zhehao.nkd 2021 年 5 月 31 日
When I use the spectrogram function (https://www.mathworks.com/help/signal/ref/spectrogram.html) , I find that depends on the time duration of the y data, the time-axis units are dynamic, shifting between 'second(s)' and 'milisecond(s)'. So is it possible to force the unit to be 'second(s)'?
  2 件のコメント
Adam Danz
Adam Danz 2021 年 5 月 25 日
Are you referring to your inputs or are you referring to something in the plot produced by spectrogram?
It would be helpful to provide a screen shot of the figure or supply the input data, depending on what you're describing.
zhehao.nkd
zhehao.nkd 2021 年 5 月 26 日
Sorry for my vague description. I am referring to the plot generated by function spectrogram without defining the output.
For example,
fs = 32000;
y1 = wgn(1*fs,1,1);
y2 = wgn(2*fs,1,1);
figure;
subplot(2,1,1)
spectrogram(y1,hamming(512),512-round(fs/1e3),512,fs,'yaxis');
subplot(2,1,2)
spectrogram(y2,hamming(512),512-round(fs/1e3),512,fs,'yaxis');
This two subplots will have different time units as ms and s. So how can I force the unit to be s?

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

採用された回答

Adam Danz
Adam Danz 2021 年 5 月 26 日
編集済み: Adam Danz 2021 年 5 月 27 日
I don't know whether there's an input to spectrogram that could solve this but here's how to fix the labels after the plots are created.
You could detect whether the x-axis is in seconds or ms by the xlabel;
if strcmpi(ax1.XLabel.String, 'Time (ms)')
ax1.XTickLabels = compose('%.1f',ax1.XTick/1000); % set tick labels
xlabel(ax1, 'Time (s)') % set axis label
end
P1=[10,-1];
fs = 32000;
y1 = wgn(1*fs,1,1);
y2 = wgn(2*fs,1,1);
figure;
ax1 = subplot(2,1,1); % get handle
spectrogram(y1,hamming(512),512-round(fs/1e3),512,fs,'yaxis');
subplot(2,1,2)
spectrogram(y2,hamming(512),512-round(fs/1e3),512,fs,'yaxis');
ax1.XTickLabels = compose('%.1f',ax1.XTick/1000); % set tick labels
xlabel(ax1, 'Time (s)') % set axis label

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTime-Frequency Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by