ploting a logaritmic-distributed data
Show older comments
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.
Accepted Answer
More Answers (2)
Star Strider
on 2 May 2016
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
Lucas Santisteban
on 3 May 2016
Edited: Lucas Santisteban
on 3 May 2016
Lucas Santisteban
on 3 May 2016
0 votes
Categories
Find more on Blue in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!