美文网首页
spectrogram强制使用Hz为频率单位

spectrogram强制使用Hz为频率单位

作者: LIANG静闲 | 来源:发表于2019-10-24 12:42 被阅读0次

通常,我们使用matlab内置函数spectrogram来绘制信号的时频图,非常的方便。
但是对于下图,显然我们只关心100Hz以下的频段。


时频图

使用ylim([0 0.1])命令可以控制纵轴频显示的频率范围。但是频率以kHz为单位,看着很不方便。


调整频率显示范围
修改频率轴以Hz为单位的操作如下:

方法1 假修改
假修改是指欺骗眼睛,实际上图中实际坐标并没有更改。如下图Datatip所示,实际Y轴坐标没有改变,只是改变了yticklabel。

yticks(0:0.01:0.1)
yticklabels(num2cell(0:10:100))
ylabel('Frequency (Hz)')

或者另一段代码可以有同样的效果,自动化的程度更高些。

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
假修改

方法2 真修改
真修改是指图中坐标与坐标轴的显示是一致的。

  • step 1 双击spectrogram字样,右键打开spectrogram函数;
  • step 2 在函数内找到第168行“pspectrogram({x},'spect',varargin{:});”,双击pspectrogram字样,右键打开pspectrogram函数;
  • step 3 在函数第163、164行,有两句“[f,~,uf] = engunits(f,'unicode');”、“freqlbl = getfreqlbl([uf 'Hz']);” ,这两句控制着频率显示的单位。将这两句替换为“freqlbl = getfreqlbl([ 'Hz']);”,以后使用spectrogram就可以强制使用Hz为频率单位了。
% [f,~,uf] = engunits(f,'unicode');% 自动调整频率单位
% freqlbl = getfreqlbl([uf 'Hz']);  % 自动调整频率单位
freqlbl = getfreqlbl([ 'Hz']);   % 强制使用Hz,禁止自动调整频率单位
  • step 4 建议不直接修改内置函数,重命名一个自己的函数会更好,让原函数保留自动调整频率范围的功能。除非你愿意改来改去。
真修改

下面附上我修改好的绘图函数程序代码以及调用格式。

[S,f,t]=spectrogram(wt,4096,4096-16,4096,fs_w,'yaxis');
figure;displayspectrogram(t,f,S)
function displayspectrogram(t,f,Pxx,faxisloc,esttype,thresh)

% 本函数修改自matlab自带同名函数,用于绘制STFT时频图
% 2019/10/24 Leung
% faxisloc  填写'yaxis'或'xaxis'或者空着,默认yaxis,即横轴时间,纵轴频率
% esttype   填写{'psd','power'}或者空着,默认'psd'
% thresh    sets to zero those elements of ps such that 10 log10(ps) ≤ thresh. 
%           Specify thresh in decibels.
%           即设置colorbar的下界。如-20dB应设为:-20
% see "help spectrogram" for complete description of all input arguments.

narginchk(3,6); % narginchk(minArgs,maxArgs)

switch nargin
    case 3
        faxisloc = 'yaxis';
        esttype = 'psd';
        threshold = 0;
    case 4
        esttype = 'psd';
        threshold = 0;
    case 5
        threshold = 0;
    case 6
        threshold = 10^(thresh/10);
end
        
% remove low-power estimates if requested
if threshold>0
  Pxx(Pxx<threshold) = 0;
end


% Cell array of the standard frequency units strings
% Use engineering units
% [f,~,uf] = engunits(f,'unicode');% 自动调整频率单位
% freqlbl = getfreqlbl([uf 'Hz']);  % 自动调整频率单位
freqlbl = getfreqlbl([ 'Hz']);   % 强制使用Hz,禁止自动调整频率单位
[t,~,ut] = engunits(t,'unicode','time');
timelbl = [getString(message('signal:spectrogram:Time')) ' (' ut ')'];


h = newplot;
if strcmpi(faxisloc,'yaxis')
  xlbl = timelbl;
  ylbl = freqlbl;
else
  xlbl = freqlbl;
  ylbl = timelbl;
end

hRotate = uigettool(ancestor(h,'Figure'),'Exploration.Rotate');
if isempty(hRotate) || strcmp(hRotate.State,'off')
  if strcmp(faxisloc,'yaxis')
    hndl = imagesc(t, f, 10*log10(abs(Pxx)+eps));
  else
    hndl = imagesc(f, t, 10*log10(abs(Pxx)'+eps));
  end
  hndl.Parent.YDir = 'normal';

  setupListeners(hndl);
else
  if strcmp(faxisloc,'yaxis')
    hndl = surf(t, f, 10*log10(abs(Pxx)+eps),'EdgeColor','none');
  else
    hndl = surf(f, t, 10*log10(abs(Pxx)'+eps),'EdgeColor','none');
  end
  axis xy
  axis tight
  view(0,90);
end  

if threshold>0
  Pmax = max(Pxx(:));
  if threshold < Pmax
    set(ancestor(hndl,'axes'),'CLim',10*log10([threshold Pmax]))
  end
end

if strcmpi(esttype,'power')
  cblabel = getString(message('signal:dspdata:dspdata:PowerdB'));
else
  cblabel = getString(message('signal:dspdata:dspdata:PowerfrequencydBHz'));
end
%sigutils.internal.colorbari('titlelong',cblabel);
h = colorbar;
h.Label.String = cblabel;

ylabel(ylbl);
xlabel(xlbl);


% -------------------------------------------------------------------------
function setupListeners(hndl)
hAxes = ancestor(hndl,'Axes');
hRotate = uigettool(ancestor(hndl,'Figure'),'Exploration.Rotate');
eYScale = addlistener(hAxes,'YScale','PreSet',@(src,evt) image2surf(hndl));
eView = addlistener(hAxes,'View','PostSet',@(src,evt) image2surf(hndl));
if ~isempty(hRotate)
    eRotate = addlistener(hRotate,'State','PostSet',@(src,evt) image2surf(hndl));
else
    eRotate = [];
end

if ~isprop(hndl,'TransientUserDataListener')
    pi = addprop(hndl,'TransientUserDataListener');
    pi.Transient = true;
end

set(hndl,'TransientUserDataListener',{eYScale,eView,eRotate});

相关文章

  • spectrogram强制使用Hz为频率单位

    通常,我们使用matlab内置函数spectrogram来绘制信号的时频图,非常的方便。但是对于下图,显然我们只关...

  • 常识MHz bit/s

    Hz是频率单位,例如10Hz就是表示每秒运算10次 Mbps是Million bit per secend (表示...

  • 音视频开发基础术语

    帧率 用于测量显示帧数的单位,所谓的测量单位为每秒显示的帧数,英文表示FPS或HZ。每秒现实的帧数或者刷新频率关系...

  • Android音频开发(1):基础知识

    Android音频开发(1):基础知识 导读 人的说话频率基本上为300Hz3400Hz,但是人耳朵听觉频率基本上...

  • 人声频率范围及各频段音色效果

    一、人声频率范围 实际人声频率 男:低音82~392Hz,基准音区64~523Hz男中音123~493Hz,男高音...

  • 人声频率范围及各频段音色效果

    一、人声频率范围 实际人声频率 男:低音82~392Hz,基准音区64~523Hz男中音123~493Hz,男高音...

  • 2017-09-11

    人声频率范围 实际人声频率 男:低音82~392Hz,基准音区64~523Hz 男中音123~493Hz,男高音1...

  • 开发中音频常用名词记录

    频率(Frequency):物体每秒来回振动的次数,赫兹(Hz);理论上,人类的发声频率是85Hz ~ 1100H...

  • 多媒体知识点整理(3)

    音频基础&& 话音编码 人的发音范围 人的发音频率范围:80Hz---3.4kHz人的说话频率范围:300Hz -...

  • 屏幕刷新频率 动画原理 时间间隔

    屏幕刷新频率 图像在屏幕上更新的速度,即屏幕上的图像每秒钟出现的次数,单位是赫兹(Hz)。 一般笔记本电脑频率大概...

网友评论

      本文标题:spectrogram强制使用Hz为频率单位

      本文链接:https://www.haomeiwen.com/subject/ejervctx.html