manipulating data for log scale
Hello,
I have data to plot either in linear scale or in semilog scale. However, part of the dataset will be negative. It is customary in the field (semiconductor physics) to plot by taking the absolute value of the data. I have my data in array of doubles, and I pass pointers using the setRawSamples function.
I dont want to apply fabs on my dataset, in order not to lose the negative info. Also it is necessary to easily flip from linear to log scale. So I would like to do it using the QwtLogTransform and the transform function. So I overloaded it in a derived class (see below). First this is not very flexible since the method is "const". Second I have a problem with the graph scale which goes to logMin (1E-150) whatever I do.
I don't understand clearly what happen here (how is the scale calculated, not using transform results ?)
Any hint welcome !
Code:
class LogTransform: public QwtLogTransform
{
public:
virtual double transform( double value ) const override
{
if (value == 0) return 0;
return log(fabs(value));
}
virtual QwtTransform *copy() const override
{
return new LogTransform();
}
};
and also, where the log scale is applied :
QwtLogScaleEngine* scaleEngine = new QwtLogScaleEngine();
scaleEngine->setTransformation( new LogTransform() );
Re: manipulating data for log scale
Quote:
Second I have a problem with the graph scale which goes to logMin (1E-150) whatever I do.
Uwe can answer about Qwt, but this problem could well be due to your comparison in line 7. A floating point value, especially one measured experimentally, is never exactly equal to zero unless it is set to exactly zero. So your comparison is probably only rarely true for your data. A better solution is to define some "epsilon" value, where if the absolute value of the number is less than epsilon, you treat it as zero. For experimental data, this is typically the minimum resolution of the intensity scale - where two measured values are treated as equal if their difference is less than the measurement error (the ability of the measurement to distinguish them).
Code:
double epsilon = 1.0e-6;
if ( std::abs( value ) < epsilon ) return 0;
Quote:
First this is not very flexible since the method is "const".
It is perfectly appropriate that these methods are defined as "const". A const method does not change the state of the class instance when it is called. In this case, transform() simply returns a new value based on the input (changing nothing), and copy() returns a new instance, again changing nothing in the class instance.
If you need to store some value in the class instance as a member variable that changes its value when transform() or copy() are called, then declare that variable as "mutable". It is designed for that purpose - to preserve the const-ness of the interface while allowing minor side-effect changes to occur in member variables.
Re: manipulating data for log scale
Hi d_stranz !
Thanks for your helpful answer.
My point was that an exact value of 0 is problematic, since log(0) -> minus infinity while log (1E-6) = -6. So a value close to zero, but not exactly 0 would not be a problem. So I wanted to eliminate the exact 0 value.
I'll do a test with your suggested solution to see how the scaling behaves.
Now for the "mutable" attribute, I learned something helpful that I did not know. I'll do some test using it as well.
Thanks and Happy easter !
Re: manipulating data for log scale
Quote:
My point was that an exact value of 0 is problematic, since log(0) -> minus infinity while log (1E-6) = -6.
I think you missed my point (and example). I did not say to convert values near zero to epsilon; I said to use the epsilon value to to determine if something was sufficiently close to zero that it could be treated as zero. You can set the actual value used for epsilon to whatever is appropriate for your instrumentation. Clipping the near zero values in this way avoids the logMin problem because you will never pass anything that small to the log() function - it will be trapped and set to zero by the epsilon comparison.
Re: manipulating data for log scale
A logarithmic scale never goes to 0 due to its nature. And because of limitations of a double it always has a minimum - like there is a limitation for what can be displayed as a maximum.
If you need to have a scale that includes the 0 you need to define ranges with different transformations.
F.e logarithmic for >= 0.01 and something based on QwtPowerTransform for below.
The "At 400" transformation in playground/scaleengine shows how to use different transformations.
HTH,
Uwe
Re: manipulating data for log scale
Hello
Thanks for your answers, I think I might not have been clear in my explanations.
Let's illustrate : I may want to draw the diode characteristic where intensity and voltage are related by : I = I0 * (exp(V/V0) - 1)
I0 (let's say something between 10-6 and 10-9 A) and V0 are both positive constants. It is interesting to draw this curve in semi log plot because of its exponential nature. It make sense both in direct and reverse (for V both positive and négative). Of course, for V highly négative values, I will tend to -I0. So this function has negative value for negative value of V. A classical way of representing such data is to plot (semilog) absolute value of I versus V (for V both positive and négative). The point where I = 0 should for V=0 but also real data has noise, so it may happen more than once
My point was to draw absolute value of I without modifying my raw data, but by including the absolute value in the transformation. In this way, I don't loose my original data and I don't need to make a modified copy of it.
I will take a closer look at the already existing transformations, as suggested by Uwe. I didn't had time to experiment more up to now.
Best regards
Oliver
I also need to add that for noisy data, the I value will not take dramatically low values, because of the minimal step of the measuring system. Let's say for example 1E-12, so no value of I lower than that can occur in the data.
Re: manipulating data for log scale
Just to add, you probably want to use QwtPlotCurve::setBaseline to some reasonable number to stop your graph from starting at LOG_MIN.
edit: this is only relevant for Sticks, forgot that. You may need to manipulate the boundingRect otherwise.