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

26 visualizaciones (últimos 30 días)
Hi,
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;
N/sampleRate;
figure(1);
fighandle = figure(1);
fighandle.Color = [1 1 1];
axesHandle = gca;
%Spektrogramm
spectrogram(FrontLeft, 128, 64, 128, sampleRate, 'yaxis');
axesHandle.YScale = "log";
  1 comentario
dpb
dpb el 9 de Ag. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Felix
Felix el 10 de Ag. de 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 :)

Más respuestas (1)

Paul
Paul el 9 de Ag. de 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 comentarios
Felix
Felix el 10 de Ag. de 2023
Thanks a lot, i'm almost there. The only problem right now: i want the YScale to be logarithmic, but if i run the code with "axesHandle.YScale = 'log' ", the spectrogram won't show up in figure. Any idea why that is? I attached both figures.
filename = 'C:\Users\felix\OneDrive\Uni-BME8\AudioFiles\BRSoloScene\SoloAudio.wav';
[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;
N/sampleRate;
figure(1);
fighandle = figure(1);
fighandle.Color = [1 1 1];
axesHandle = gca;
%Spektrogramm
[s, f, t] = spectrogram(FrontLeft, 128, 64, 128, sampleRate, 'yaxis');
imagesc(t, f, 10*log10(abs(s)));
axis xy;
axesHandle.YLim = [0.1 10000]
%axesHandle.YScale = 'log';
colormap default;
colorbar;
cbar = colorbar;
cbar.Label.String = {'Intensität in dB'};
xlabel('Zeit (s)');
ylabel('Frequenz (Hz)');
title('Spektrogramm des Front-Left-Kanals');
Paul
Paul el 10 de Ag. de 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.
rng(100);
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');
figure
pcolor(t,f,10*log10(abs(s))),shading flat,colorbar
ylim([0.1 100])
set(gca,'YScale','log')

Iniciar sesión para comentar.

Categorías

Más información sobre Time-Frequency Analysis en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by