How to plot negative dB values

I need to plot a 3D graph which represents an antenna pattern. The input is Cartesian coordinates with the center of the graph at the origin. The graph needs to be scaled properly so I need to set the axis scaling to logarithmic. I tried using
set(gca, 'xscale', 'log', 'yscale', 'log', 'zscale', 'log');
but then it does not plot any of the values whose coordinates have negative components.
Is there a way to get around this? Is it possible to plot in normal scaling axis but compute the log scale and plot the points? The axis does not need to be label but the graph should look reasonably close to how it would if the axis was truly log scaled.
Thank you!

Réponses (2)

John D'Errico
John D'Errico le 19 Mar 2015
Modifié(e) : John D'Errico le 19 Mar 2015

1 vote

I think you misunderstand how a log scale idea works. For example, suppose you have some data that varies over 10 orders of magnitude, so numbers that run from 1e-5 to as large as 1e5.
If you try to plot that data on standard axes, you will get useless crap, missing out on any useful information.
So instead, you decide to plot it on a log scale. You don't log the data yourself, AND also set the axes to a log scale (or use semilogy or loglo to do the plots). The point is, these tools implicitly (internally) log the data, then set the axes and tick marks to be done appropriately.
In the case of db data, where you have what are essentially already logged numbers, you CANNOT use the log scale axes, because MATLAB will then form the log of your data! db data already is in a log scale!
The trick is, if you need for the axes to be log scale, then you will need to exponentiate the data first. If 1 db is a factor of 2, then you need to raise 2 to those powers, THEN use a tool like semi-logy, or set a log scale. If 1 db is a factor of 10, then raise 10 to the appropriate power before plotting.
As you can see, there will no longer be a problem with negative numbers, since you are giving the code data in the proper form.
Remember, if you wish to just plot the data in db (thus log) form, then you cannot use the log scaling feature, because the first thing it will do is to log your data. And of course, this is why it fails when you pass in negative numbers. As it turns out, suppose all of your numbers were positive, and you tried a log scaling? You would still get the wrong plot, since the code would first log your data. Essentially you would then be plotting doubly logged numbers.
I hope this makes sense.

4 commentaires

Ashwin
Ashwin le 19 Mar 2015
Modifié(e) : Ashwin le 19 Mar 2015
The problem is not the data set or the the range that it is in. The problem is that I need to plot a 3D graph where the axis is scaled like a log graph to get a proper representation of what I am trying to show. Matlab, when I set the axis to log, does not have any coordinates such as (0, 0, -1). Therefore I cannot plot a 3D graph where the graph is centered at the origin because I could need to use all quadrants which means x, y, or z could be negative numbers.
I could shift the graph so it is confined to the first quadrant (all positive numbers for x, y, and z) but then the scale is thrown off because of how log graph scales compared to normal ones.
I hope that clears up the problem I am having and if I misunderstood your answer, I apologize in advance!
Thanks.
EDIT: when you set the axis to log scale using gca, the limits of the axis [inf, 0)
John D'Errico
John D'Errico le 19 Mar 2015
Modifié(e) : John D'Errico le 19 Mar 2015
Yes, the problem IS the data set, and what it represents. Your data is in the form of dB values. They are already in log "units". You CANNOT use a log scale on numbers that are ALREADY logged, since then the plotting tools will try to log your data. As you have found out, this causes a problem because it is meaningless to log dB values, especially if they are negative. Shifting the numbers so they are all positive by adding a constant is also completely wrong, since again, you will get a meaningless result.
These plotting tools accomplish log scales by immediately taking the log of your data. Then they plot on a linear scale, and set the tickmarks to match the log scale. There is no option to NOT take the log of your data, to tell it that you have already implicitly done so, because these are numbers in dB units.
In order to get the log scales to work properly for your data, you need to UNLOG the data. (Ok, I admit that unlog is not a word, but I was too lazy to type exponentiate or antilog.) That is, you must transform the data so that it has been exponentiated (as a power of 10 or 2, or whatever is appropriate, depending on the dB scale the data lives in.) Then the log scale will indeed work properly.
So the answer really is simple. You just need to think about my response, and what MATLAB does when you try to use log scale axes. Software is inflexible. It does exactly what it is designed to do, how it was written. It does not do what you want it to know how to do, achieved by reading your mind. (Ok, maybe in the next MATLAB release, if you get the mind reading toolbox. I have been reliably informed that toolbox is in beta test.)
Ashwin
Ashwin le 24 Mar 2015
I don't believe the problem is that I am logging a db value to plot. I have converted to power and tried it as well with little luck. The problem is, how would I plot the point (-1, -1, -1) in a log scale axis plot? Anything below 0 does not exist in the plot limit.
John D'Errico
John D'Errico le 24 Mar 2015
Modifié(e) : John D'Errico le 24 Mar 2015
What you believe or not is not relevant.
Nothing you will do will make the log of -1 exist as a real number. That is just simple mathematics.
log(-1)
ans =
0 + 3.1416i
As well, nothing that you do will force code that is already written to do a specific task, force it to do what you personally want it to do. In this case, that requires it to your mind, then rewrite itself to do as you desire.
Apparently nothing I say will get through your hard held misconceptions. So since you resist doing something logical, your only viable solution is to write your own plotting tool that will do as you desire. I do hope you add that mind reading feature, so it can read your mind.

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 19 Mar 2015

0 votes

Have you tried calling ylim()?
ylim([maxNegValue, maxPosValue]);

2 commentaires

Ashwin
Ashwin le 19 Mar 2015
Negative data are still ignored because I have set the axis to log scale. Is there a way to set the axis to log scale without matlab ignoring the negative values?
The -1 is the value after the log has been taken, right? Because you can't take the log of a negative value. So, what's wrong with this:
y = 2 * rand(1, 100); % Create sample data.
semilogy(y, 'LineWidth', 2);
grid on;
You'll see the -1 as the exponent on the 10 in the y axis tick labels.

Connectez-vous pour commenter.

Catégories

Question posée :

le 19 Mar 2015

Modifié(e) :

le 24 Mar 2015

Community Treasure Hunt

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

Start Hunting!

Translated by