How can I change the x-axis time stamps in a spectrogram?

67 views (last 30 days)
I have acoustic data spanning over multiple hours in which signals of interest are marked with a start and end time stamp. I now want to visualise just my signal of interest, x, by making a subplot(2,1) showing a waveform and spectrogram. The time axis should show the timestamps from the original recording, i.e. for a 5s signal that started after the first 20s of recording I'd like the x-axis to span from 20-25s.
To do so I extracted the signal of interest from the original data and wrote a time vector using the start time (20 seconds into the recording) and duration of the signal and dividing by my sample rate fs.
signal_start = 20;
t = signal_start + ((1:length(x))/fs);
Then I used the following code for plotting:
figure(1); hold on
subplot(2,1,1); plot(t,x);
subplot(2,1,2); spectrogram(x,1024,512,1024,fs,'yaxis');
For the waveform that works perfectly fine as I can just plot my signal x over my time vector t. However, I cannot figure out how to now use my defined time vector t with the spectrogram function to obtain the same x-axis for both my subplots. When using spectrogram as below, the x-axis always spans from 0 to the signal duration, i.e. 0s to 5s for this example. I have looked at the spectrogram documentation but could not find any option to add another inout argument to define my x-axis. So I guess I need some aditional code to do so?

Answers (1)

Adam Drake
Adam Drake on 14 Mar 2023
Edited: Adam Drake on 14 Mar 2023
figure(1)
subplot(2,1,1)
plot(t,x)
subplot(2,1,2)
spectrogram(x,1024,512,1024,fs,'yaxis')
xlim([t(1) t(end)])
% or set(gca,'XLim',[t(1) t(end)]);
  2 Comments
ch
ch on 15 Mar 2023
Thanks Adam for your response. Unfortunately this doesn't solve the problem because the spectrogram within the thereby set x limits is empty as the spectrogram is still produced at 0-5s and xlim (same as set(gca...)) only shifts the window shown in the figure, not where the spectrogram is plotted.
I could use this approach if I were to load in the entire acoustic data and not just my extracted signal. However, the dataset is quite large and this would slow down my computer a lot, which is why I am looking for an alternative way.
Adam Drake
Adam Drake on 15 Mar 2023
Get rid of the xlim, switch to: set(gca,'XTickLabel',t) You may have to experiment but basically you're just overriding the x-axis label markers. In the future, if you could provide enough so that we can recreate what you see on your end that would help.

Sign in to comment.

Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!