Rickson
Rickson

Reputation: 1090

Changing axis units of MATLAB spectrogram

How can the axis units of the figure generated by spectrogram forced to be always "s" (seconds) for the x-axis and "Hz" (Hertz) for the y-axis?

The following spectrogram may serve as a demonstrator. In this example, they have been auto-adjusted by MATLAB to "mins" and "kHz".

t = 0:0.0001:200;
x = chirp(t,100,1,200,'quadratic');
spectrogram(x,128,120,128,2e3,'yaxis')

Upvotes: 1

Views: 1925

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

The spectrogram function calls pspectrogram to do the actual work, and this in turn calls engunits to determine the "engineering units" appropriate for your signal. Apparently spectrogram doesn't have an input option to avoid the unit conversion.

Therefore, the best way seems to be to get the actual outputs of spectrogram and plot the image yourself, without unit conversion:

[~,F,T,P] = spectrogram(x,128,120,128,2e3,'yaxis');
imagesc(T, F, 10*log10(P+eps)) % add eps like pspectrogram does
axis xy
ylabel('Frequency (Hz)')
xlabel('Time (s)')
h = colorbar;
h.Label.String = 'Power/frequency (dB/Hz)';
  • Image generated by the original code:

    enter image description here

  • Image obtained with the code above:

    enter image description here

Upvotes: 2

Related Questions