How to plot negative dB values

20 views (last 30 days)
Ashwin
Ashwin on 19 Mar 2015
Edited: John D'Errico on 24 Mar 2015
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!

Answers (2)

John D'Errico
John D'Errico on 19 Mar 2015
Edited: John D'Errico on 19 Mar 2015
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 Comments
Ashwin
Ashwin on 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 on 24 Mar 2015
Edited: John D'Errico on 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.

Sign in to comment.


Image Analyst
Image Analyst on 19 Mar 2015
Have you tried calling ylim()?
ylim([maxNegValue, maxPosValue]);
  2 Comments
Ashwin
Ashwin on 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?
Image Analyst
Image Analyst on 24 Mar 2015
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!