Contourf plot Yscale log2 vs log10

I have frequencies ranging from 2-194 in a base-2 logarithmic spacing (10 voices per octave) like this:
frex = 2.^([10:76]/10);
I want to plot a spectrogram with contourf using them and make the yaxis logarithmically scaled. I know that the below command does this for base-10 log, but how can I do it for base-2 log? I saw some other questions asking similar, but I don't think the answers actually changed the scale of the y-axis. One person said that log10 and log2 scale shape will be the same. Is that correct?
set(gca,'YScale','log')

 Respuesta aceptada

You can change the base of the logarithm by dividing it by the desired base, then use the appropriate code to change the tick labels:
frex = 2.^((10:76)/10)
frex = 1×67
2.0000 2.1435 2.2974 2.4623 2.6390 2.8284 3.0314 3.2490 3.4822 3.7321 4.0000 4.2871 4.5948 4.9246 5.2780 5.6569 6.0629 6.4980 6.9644 7.4643 8.0000 8.5742 9.1896 9.8492 10.5561 11.3137 12.1257 12.9960 13.9288 14.9285
frex2 = log(frex)/log(2)
frex2 = 1×67
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000
N = 1024;
n = 0:N-1;
w0 = 2*pi/5;
x = sin(w0*n)+10*sin(2*w0*n);
% Compute the short-time Fourier transform using the function defaults. Plot the spectrogram.
figure
spectrogram(x,'yaxis');
figure
spectrogram(x,'yaxis')
Ax = gca;
yt = Ax.YTick;
Ax.YTickLabel = log(yt)/log(2);
This example is from the spectrogram documentation, with my changes to the y-axis labels.
.

6 comentarios

Anas Khan
Anas Khan el 14 de Jul. de 2022
Ok, correct me if I am wrong, but what you did was change the frequencies to be represented in terms of their log2(frex), right? What I'm actually looking for is how to change the scale of the yaxis like this but for log2 spaced intervals rather than log10 since my frequencies are base 2 and not base 10. I just don't know if my tick labels are accurate since I scaled the frequencies using base 2 but the the yscale log function uses base 10 log to change the yscale.
Linear scale:
Log scale:
Star Strider
Star Strider el 14 de Jul. de 2022
My code changes the y-tick labels from log10 to log2. I thought that was what you wanted.
I now have no idea what you want to do.
Ok so you can change the scale of the y-axis by using this:
set(gca,'YScale','log')
I want the frequencies to stay labeled as I've defined them. ( 2.^([10:76]/10) ). So first y-tick label will be 2 last one should be 194. I already have code to assign the y-tick labels. What I want is for the spacing between lower frequencies to be larger than higher frequencies. See how in my second plot the real estate between 10 and 40 (difference of 30) is the same as 40 and 200 (difference of 160). That's what the Yscale being changed to log does, but is it correct to use this if my frequencies are not base-10?
The range of the tick values has to be the same as the range of the original ticks. The tick labels can be whatever you want them to be.
Example —
frex = 2.^((10:76)/10)
frex = 1×67
2.0000 2.1435 2.2974 2.4623 2.6390 2.8284 3.0314 3.2490 3.4822 3.7321 4.0000 4.2871 4.5948 4.9246 5.2780 5.6569 6.0629 6.4980 6.9644 7.4643 8.0000 8.5742 9.1896 9.8492 10.5561 11.3137 12.1257 12.9960 13.9288 14.9285
frex2 = log(frex)/log(2)
frex2 = 1×67
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000
N = 1024;
n = 0:N-1;
w0 = 2*pi/5;
x = sin(w0*n)+10*sin(2*w0*n);
% Compute the short-time Fourier transform using the function defaults. Plot the spectrogram.
% figure
% spectrogram(x,'yaxis');
figure
spectrogram(x,'yaxis')
set(gca,'YScale','log')
ytv = ( 2.^([10:76]/10) );
Ax = gca;
yt = Ax.YTick;
Ax.YTick = linspace(min(yt), max(yt), numel(ytv));
Ax.YTickLabel = ytv;
This is the approach I would use. Make appropriate changes to get the result you want.
.
Anas Khan
Anas Khan el 14 de Jul. de 2022
Thank you!
Star Strider
Star Strider el 14 de Jul. de 2022
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2021b

Preguntada:

el 14 de Jul. de 2022

Comentada:

el 14 de Jul. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by