Spectrogram function cutting off beginning and end of signal
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
I have a 10 second audio clip (called originalsound) with a sampling frequency of 2,000 Hz (called downsampledrate) that I want to plot using spectrogram. When I do so with the following code:
figure; spectrogram(originalsound,N,[],[],downsampledrate,'yaxis')
title('Original Sound')
set(gca,'FontSize',16)
yt = get(gca, 'YTick');
set(gca, 'YTick',yt, 'YTickLabel',yt*1E+3) %change y-axis to Hz from kHz
ylabel('Frequency (Hz)') %change y-axis label
the plot generates without 0 or 10 labeled for time on the x-axis.

I tried fixing this by changing the xlim from 0-10, but then I get white bars on either side of the spectrogram.

If I am plotting a 10 second sound, why are there not 10 seconds shown in the spectrogram? Is there some sort of windowing effect going on?
0 Commenti
Risposta accettata
Paul
il 24 Apr 2023
Hi Danielle,
Create a fake signal using the the parameters in the question
downsampledrate = 2e3;
rng(100);
originalsound = rand(1,numel(0:(1/downsampledrate):10));
The quesiton didn't specify the window length, so I'll just pick one
N = 1024;
With this window length, the number of N-point segments in the signal is
nseg = (numel(originalsound)/N)
Of course there can only be an integer number of segments. The input data will be truncated.
nseg = floor(nseg)
However, I believe with these parameters there will be 50% overlap of the segements, so there will really be double the number of segments
nseg = nseg*2;
Create the spectrogram
figure; spectrogram(originalsound,N,[],[],downsampledrate,'yaxis')
title('Original Sound')
set(gca,'FontSize',16)
yt = get(gca, 'YTick');
set(gca, 'YTick',yt, 'YTickLabel',yt*1E+3) %change y-axis to Hz from kHz
ylabel('Frequency (Hz)') %change y-axis label
xlim([0 10])
We see the white bars. Use output arguments to get the actual time vector
[~,~,t] = spectrogram(originalsound,N,[],[],downsampledrate);
t([1 end])
According to spectrogram, "The time values in t correspond to the midpoint of each segment." The midpoint of the first segment is at N/2 and the midpoint of the last segment (with the assumed 50% overlap) is at nseg*N
[N/2 , nseg*N/2]/downsampledrate
We get a match.
Based on the documentation, I don't think you'll ever get the spectrogram to start/stop at the first/last time point. Where the actual time points fall will depend how spectrogram determines the length and locations of the segments based on the input arguments.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Time-Frequency Analysis in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
