Plotting a spectrogram with y axis values as whole numbers and time set to seconds

14 vues (au cours des 30 derniers jours)
i want to plot the spectrogram of an audio file using spectrogram function. However, it seems like i can't set the x axis to seconds (it appears as 0.1 ... 1.1 Minutes)- i want the x axes Tick set to 10 seconds Ticks. Also, i want the values on y axis to be shown as whole numbers instead of powers to the base 10 and i want the frequency in Hz instead of kHz... can anybody help me? Heres the code:
[signal, sampleRate] = audioread(filename);
FrontLeft = signal(:, 1); % Front Left
Center = signal(:, 2); % Center
FrontRight = signal(:, 3); % Front Right
SurroundLeft = signal(:, 4); % Surround Left
SurroundRight = signal(:, 5); % Surround Right
LFE = signal(:, 6); % LFE
N = length(signal);
t = (0:N-1)/sampleRate;
fighandle = figure(1);
fighandle.Color = [1 1 1];
axesHandle = gca;
spectrogram(FrontLeft, 128, 64, 128, sampleRate, 'yaxis');
axesHandle.YScale = "log";
  1 commentaire
dpb le 9 Août 2023
Attach the audio file if expect anybody to try to figure out what is what...nothing can be done with an image that can't even read.
The log10 display is owing to you have set .YScale='log'; it's not at all clear what you mean by _" i want the values on y axis to be shown as whole numbers", the results of the spectrogram are going to be what they turn out to be; it may not be possible to make the display integral and show the results.

Connectez-vous pour commenter.

Réponse acceptée

Felix le 10 Août 2023
Thanks everybody for the help! I solved the problem, using this STFT example code and manually change the y axis by adding these three lines:
set(gca, 'YScale', 'log')
set(gca, 'YTick', [20 50 100 200 500 1000 2000 5000 10000])
set(gca, 'YTickLabel', {'20', '50', '100', '200', '500', '1000', '2000', '5000', '10000'})
Now it works as intended :)

Plus de réponses (1)

Paul le 9 Août 2023
Hi Felix,
Return the output arguments of spectrogram and then you can make any plot you want with any units in any of the dimensions, customize the ticks, etc.
  2 commentaires
Paul le 10 Août 2023
Hard for me to day without being able to recreate the problem. If you want, you can attach the .wav file using the paperclip icon in the Insert portion of the ribbon.
I played around with some example data and had trouble with imagesc, but I'm not familiar with that function. However, pcolor seemed to work fine, though I think (but I'm not sure) that there is a subtle difference between how pcolor and imagesc interpret where the data points are located relative to the four corners of each bin. Check the doc, or look for posts here on Answers, where I think it's been discussed.
Here's an example using pcolor.
fs = 100;
N = 2048;
t = (0:N-1)/fs;
x = 5*sin(2*pi*10*t) + randn(1,N);
x = x - mean(x);
[s,f,t] = spectrogram(x,[],[],[],fs,'yaxis');
pcolor(t,f,10*log10(abs(s))),shading flat,colorbar
ylim([0.1 100])

Connectez-vous pour commenter.


En savoir plus sur Audio I/O and Waveform Generation dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by