How can I change the x-axis time stamps in a spectrogram?
조회 수: 37 (최근 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!