Applying Calculations to Specific Samples
There are different methods how a calculation can be limited to specific samples.
First, the use of the If-Then-Else function is a powerful way to define the true and the false case behavior.
Second, the If-Then-Else function also allows to exclude specific samples, so that these are skipped in the calculation.
The If-Then-Else function requires a condition which results in True and False.
Then and Else represent the two behaviors namely for the True case (Then) and the False case (Else).
Result = Condition ? True Case : False Case
The three parts (Condition, True case, False case) in the If-Then-Else structure are expressions which can use the same input signals or different ones. The True case and False case can be complex formulas, single input signals, or constants.
You want to calculate the power of the engine, but only in case the car is driving uphill.
Uphill condition = Gradient (Altitude) > 0
True case = EngineSpeed * Load
False case = 0
Power_Uphill = Gradient (Altitude) > 0 ? EngineSpeed * Load [* Factor] : 0
(Depending on the expected unit for Power and the units given for EngineSpeed and Load, an additional factor for unit conversion must be included.)
If-Then-Else within another calculation
If the If-Then-Else function is embedded in a more complex calculation, typically either the True case or the False case should be a neutral value for the overall calculation. The most simple neutral value is 0 for an addition or subtraction, while it is 1 for a multiplication or division.
-
You want to know the total amount of CO2 emissions for the speed range of 40 to 80 km/h. By integrating the CO2 emission gas flow (in g/s), you can calculate the amount.
-
Condition for speed range: (Speed > 40) AND (Speed <= 80)
-
True case: integration of CO2 emission gas flow
-
False case: neutral value for integration, i.e. 0
CO2_Amount = Accumulate_Prefix_Integral ( ( (Speed > 40) AND (Speed <= 80) ) ? CO2_Emission : 0 )
Accumulate_Prefix_Integral is the name of the Integral function starting with the first sample.
-
-
You want to calculate the distance driven on a test trip in a speed range of 40 to 80 km/h only.
The distance can be calculated as Integral of the Speed signal. Only the sections within the given speed range shall be considered.
-
Condition for speed range: (Speed > 40) AND (Speed <= 80)
-
True case: current Speed value is used for integral calculation
-
False case: a neutral value for the integral calculation, i.e. 0
Distance = [Factor *] Accumulate_Prefix_Integral ( ( (Speed > 40) AND (Speed <= 80) ) ? Speed : 0 )
Assuming that the unit for the speed is km/h, and the distance shall be in kilometers, you must apply a conversion factor = 1 / 3600.
-
-
You want to know for how long the speed was in the range of 40 to 80 km/h
Duration = Accumulate_Prefix_Integral ( (Speed > 40) AND (Speed <= 80) )
As the condition itself results already in a value of 1 or 0, a pure integral operation is sufficient, and the If-Then-Else function must not be used.
By the mentioned formula the duration is given in seconds.
If-Then-Else to ignore samples
In all the above listed examples, the If-Then-Else function allows to calculate a result based on a specific condition. There is a calculation result for every sample, including those which do not fulfill the condition. By the appropriate selection of the neutral value, there is no effect for the calculation result, but still a value for every time stamp, i.e. every input sample. This can be seen, for example, by the curve drawn in an oscilloscope, which is a continuous line.
In some cases it is not so simple to find a neutral value for the calculation result. Then it is helpful if you can ignore samples for the calculation. This is the case in an average calculation where you have to ignore samples as there is not an appropriate neutral value.
To exclude samples completely, i.e. that there is no result of the calculation, you can use the No Value function. Actually, the No Value function does not delete samples, but sets a flag to the value that shall be ignored.
There are two ways of creating samples with a No Value flag.
-
You want to get statistical data for the speed range of 40 to 80km/h. To eliminate the samples with a different speed these get the No Value flag assigned.
-
Condition for speed range: (Speed > 40) AND (Speed <= 80)
-
True case: keep current Speed value as it is
-
False case: set the No Value flag for the sample
Selected_Samples = (Speed > 40) AND (Speed <= 80) ? Speed : NoValue (0)
When you add the Selected_Samples signal to a Statistical Data instrument, only samples in the defined speed range will be used as basis for the statistics.
When you assign the signal to an oscilloscope, the curve is limited to the sections in which samples within the defined speed range exist.
Note: The term NoValue ( 0 ) means that a sample with value 0 and the No Value flag will be set.
or
As a frequent use case is to ignore samples for a calculation, there is a separate function to assign the No Value state to samples.
In contrast to the former definitions, you must define the condition in a way that it is clear which samples shall be excluded.
-
Condition for speed range to be excluded: (Speed <= 40) Or (Speed > 80)
(In contrast to the True condition above)
-
Function to assign the No Value state for specific samples (outside 40 - 80 km/h):
SetNoValueStatus ( Speed, ( (Speed <= 40) Or (Speed > 80) )
This is equivalent to
Selected_Samples = (Speed > 40) AND (Speed <= 80) ? Speed : NoValue (Speed)
In this case, the NoValue (signal) causes that the original signal value of Speed is kept, but gets the No Value flag assigned.
This selection of samples for the speed signal can be used as input for e.g. an Average calculation (since start of the recording).
Average_Speed = Accumulate_Prefix_Average ( Selected_Samples )
-
-
The No Value function can also be used to suppress drawing samples in an oscilloscope, for example for the distance calculation mentioned above.
Interrupted_Distance_Curve = (Speed > 40) AND (Speed <= 80) ? Distance : NoValue (0)
With Distance = [Factor *] Accumulate_Prefix_Integral ( ( (Speed > 40) AND (Speed <= 80) ) ? Speed : 0 )
Note: Although the calculation
Accumulate_Prefix_Integral ( ( (Speed > 40) AND (Speed <= 80) ) ? Speed : NoValue (0) )
would also show a curve with the same gaps as the Interrupted_Distance_Curve signal, the result would not be as you might expect it.
This is an effect of the Integral function: If there is no sample (or a sample with No Value flag), the integral uses the last available sample value for the whole time range until the next sample is available.
If-Then-Else to ignore samples with Not a Number (NaN) state
In some cases your recorded signal includes already samples with Not a Number (NaN) value.
Typically such specific samples prevent a subsequent calculation, and again you need a method to exclude such samples from your calculation.
-
To eliminate a NaN sample, you need to detect first a NaN sample, and then assign the No Value flag instead.
Condition for Not a Number: InputSignal != InputSignal
As NaN prevents calculation for a sample, the condition is true if the input signal has at that point in time a NaN sample.
You can directly replace the lengthy If-Then-Else function with the shorter SetNoValueStatus function:
InputSignal_without_NaN = SetNoValueStatus (InputSignal, InputSignal != InputSignal)
You can use the InputSignal_without_NaN for calculations with history (like Average, Minimum, Maximum) and still get a result in which the NaN samples are excluded.
-
Excluding NaN samples from integral calculation
As already mentioned above, the No Value state effects an integral calculated in an undesired manner.
Therefore, to exclude NaN values from an integral calculation, you must use the If-Then-Else calculation, so that the NaN samples get the neutral value for the integral calculation, namely 0.
Integral_excl_NaN = Accumulate_Prefix_Integral ( InputSignal != InputSignal ? 0 : InputSignal)
See also