I want to change the spectrogram time scale from minutes to seconds but I am unsure how.

조회 수: 92 (최근 30일)
I am using the audioread function to read a .wav file and I have generated a spectrogram using the spectrogram function from the data. I have resampled the sample with a different sampling frequency and now I would like to change the x scale to seconds instead of minutes.
trial1 = 'Charge_trial_1.wav';
[y1,Fs1] = audioread(trial1); %Reading the wav file
t1 = 0:1.00002/Fs1:length(y1)/Fs1; %Calculating the time
newFs1 = 3000; %New sampling frequency
[P1,Q1] = rat(newFs1/Fs1); %Assigning ratio values to new Fs
newy1 = resample(y1,P1,Q1); %Resampling the data
newt1 = 0:1.000002/newFs1:length(newy1)/newFs1; %Creating a new time
figure
spectrogram(newy1,256,250,[],newFs1,'yaxis','psd'); %Generating the spectrogram
title('Charge trial 1')
Here is a sample plot
  댓글 수: 1
Peter O
Peter O 2021년 7월 6일
I don't have a Signal Processing Toolbox handy so this is a stab in the dark here going off a lot of memory and typical advanced plotting behaviors. Bode plots from bode behave similarly. If you click in the middle of the spectrogram you should be able to get a handle to the spectrogram object using gco. There might be a TimeUnits or Units parameter to change.
  1. Click on center of spectrogram plot.
  2. Type spgm = gco in the command window of MATLAB
  3. Inspect the spgm struct that comes back for a TimeUnits or similar field that has "minutes"
  4. Try changing that to "seconds" if you find it.
  5. If that works, try passing the field name (e.g. TimeUnits) with the parameter you want to the function call: spectrogram(x,....,'TimeUnits','seconds') in order to do it automatically.
  6. The other possibility to access it, if you can't get it directly from gco is to inspect the children of the axis using get(gca,'Children') to see if there's a SpectrogramObject or something which has properties for tweaking.
Again, apologies if this doesn't work.

댓글을 달려면 로그인하십시오.

채택된 답변

Chunru
Chunru 2021년 7월 6일
You can change the xticklabel:
h = gca;
h.XTickLabel = string(h.XTick * 60);
xlabel('Time (s)');
Alternatively, compute the spectrogram by
[s, f, t] = spectrogram(...)
Then you can do your own plotting.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by