ploting a logaritmic-distributed data

I am trying to plot data obtained from a spectrum analyzer. The x-scale of its measurements is in logaritmic. I would like to plot it as I see it in the instrument's display:
However, I obtain this:
when
freqs = linspace(startFrequency, stopFrequency, num_of_points);
semilogx(freqs, measurements)
, when
freqs = linspace(startFrequency, stopFrequency, num_of_points);
plot(freqs, measurements)
, when
freqs = logspace(log10(startFrequency), log10(stopFrequency), num_of_points);
plot(freqs, measurements)
and when
freqs = logspace(log10(startFrequency), log10(stopFrequency), num_of_points);
semilogx(freqs, measurements)
On the other hand, I obtain:
in any case of the ones above but with
set(axes, 'xscale', 'log');
How am I supposed to do it?
Many thanks.
Edit: if the data was needed, I have attached it.

1 Comment

freqs = linspace(startFrequency, stopFrequency, num_of_points);
semilogx(freqs, measurements)
xlim([150E3 30E6])
ylim([34 64])
will give the same limits. Looks like perhaps the analyzer cut off the next bin at the LH side. Try stretching the actual axis length out to match; not sure why your plotted spectrum seems more compressed towards the higher frequencies; looks like same RH upper limit if I read the scale correctly.

Sign in to comment.

 Accepted Answer

Ok, there was an stupid solution I didn't try.
freqs = logspace(log10(startFrequency), log10(stopFrequency), num_of_points);
loglog(freqs, spectrum);
set(gca, 'yscale', 'lin');
With this I get exacly what I wanted.
Thank you all anyway for your help.

More Answers (2)

I’m not certain what you want the plot to look like.
This is my best guess:
D = load('Lucas Santisteban spectrum.mat');
measurements = D.spectrum;
num_of_points = length(measurements);
startFrequency = 150E+3;
stopFrequency = 30E+6;
freqs = linspace(startFrequency, stopFrequency, num_of_points);
antilogfreqs = 10.^(freqs/startFrequency);
figure(1)
semilogx(antilogfreqs, measurements)
xt = get(gca, 'XTick');
xtl = linspace(startFrequency, stopFrequency, length(xt));
xtlf = regexp(sprintf('%.1f\\cdot10^%.0f\n', [10.^rem(log10(xtl),1)' fix(log10(xtl))']'), '\n', 'split');
set(gca, 'XTick',xt, 'XTickLabel',xtlf(1:end-1), 'XMinorTick','on')
grid
This code begins by taking the antilog of the frequency vector to create the plot. The ‘xt’ assignment gets the x-tick values the plot call creates, ‘xtl’ creates a linear vector of the frequencies that simply re-samples the frequency vector, the xtlf call creates a formatted version, split into individual cells for the plot labels.
The result:

1 Comment

This is not exacly what I would like to achieve. With your approach, I only get a virtual solution. What I mean is that the real frequencies are not the ones in the Tick, so when you put a marker, the x value doesn't correspond with the actual frequency.
Many thanks anyway.
I can't understand how it can be so difficult, I just want to draw the data I obtain versus a log-spaced frequency and that the plot had a log grid.

Sign in to comment.

Is there a function to do the antifunction of what I obtain in the last graph (the one where I set the scale to log) so the plot was finally correct?

Products

Community Treasure Hunt

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

Start Hunting!