Changing the x-axis from Time instead samples

Hi everybody!
I'm a new user of Matlab, so I really appreciated your help. I'm doing the spectrogram of a chirp signal. Here is how I defined it:
fs = 1000;
t = 0:0.001:1;
y = chirp(t,60,1,20);
Later, I use the spectrogram as below, with a hamming window of 64 samples:
w1 = hamming (64);
spectrogram (y, w1, [], [], fs, 'yaxis');
The point is that when the graph is displayed, the x axis is in samples, and I would like to be in seconds. I try several things but I can´t do it. I atacched a copy of the graph.
So, I want that the x-axis be in time, not in number of samples. Can anybody help me? I apreciated so much,
Thanks!

 採用された回答

Star Strider
Star Strider 2020 年 11 月 9 日

1 投票

When I run yoiur code in R2020b, the x-axis is labeled ‘Time (ms)’.
To convert them (and the x-axis label) to seconds:
fs = 1000;
t = 0:0.001:1;
y = chirp(t,60,1,20);
w1 = hamming (64);
spectrogram (y, w1, [], [], fs, 'yaxis');
Ax = gca;
xt = Ax.XTick;
Ax.XTickLabel = xt/1000;
Ax.XLabel.String = 'Time (sec)';
.

4 件のコメント

Matias Calzada Gianero
Matias Calzada Gianero 2020 年 11 月 9 日
Great! Thanks for your answer. So, I understand that the x-axis is already in mS, not in samples. Am I right? Thank you again!
Star Strider
Star Strider 2020 年 11 月 9 日
編集済み: Star Strider 2020 年 11 月 9 日
My pleasure!
That is how I understand it, and that is the way it is plotted in R2020b. I believe time and frequency are the default units for spectrogram plots if a sampling frequency is specified. The documentation does not appear to discuss that, at least when I looked through it. If the units are other than time (for example, space), the code here can produce the correct designation for what would otherwise be the time axis label.
Matias Calzada Gianero
Matias Calzada Gianero 2020 年 11 月 9 日
It makes sense what you say. Thank you so much, you help me a lot!! Thanks for your knowledge and time!! Regards!!
Star Strider
Star Strider 2020 年 11 月 9 日
As always, my pleasure!
I very much appreciate your compliment!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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