首页 > 解决方案 > 使用 MATLAB spectrogram() 绘制频率 (Hz) 与时间 (sec) 的关系图

问题描述

我想从时间信号中生成频谱图(功率谱密度),在 y 轴上具有以 Hz 为单位的频率,在 x 轴上以秒为单位的时间。

我有一个像这样生成的正弦信号:

dt = 0.04; % Integration time step in ms
T = 10000;  % simulation time in ms -> so 10 seconds of simulation
nt = round(T/dt); % simulation steps

x = sin(2*pi*frequency*(1:1:nt)*dt/1000 + phase);
figure; plot((1:1:nt)*dt/1000, x)

在此处输入图像描述

我将频谱图/功率谱密度绘制为(ps我不熟悉):

fs = 1000/dt;
figure; spectrogram(x, [], [], [], fs, 'yaxis', 'psd')

在此处输入图像描述

我预计情节是Hz-vs-Seconds,但我得到了kHz-vs-Seconds。也可以通过设置fs=1/dt;该图变为 Hz-vs-Hours。

标签: matlabtime-seriesspectrogram

解决方案


像上面一样执行您的代码,但yticks(yticks*1000)不要执行以下操作:

% Get yRuler-object
ax = gca;
yRuler = ax.YAxis;

% Loop through TickValues, multiply and insert into TickLabels cell-array
for n = 1:numel(yRuler.TickValues)
    yRuler.TickLabels{n} = num2str(yRuler.TickValues(n) * 1000);
end

不是特别简洁,但它应该可以完成这项工作。


推荐阅读