Clear Filters
Clear Filters

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

46 views (last 30 days)
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 Comment
Peter O
Peter O on 6 Jul 2021
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.

Sign in to comment.

Accepted Answer

Chunru
Chunru on 6 Jul 2021
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.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!