How can I change the x-axis time stamps in a spectrogram?
67 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
Adam Drake
2023-3-14
编辑:Adam Drake
2023-3-14
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 个评论
Adam Drake
2023-3-15
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time-Frequency Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!