Scaling analog values in PLC Programming - Time & Space Complexity
When scaling analog values in PLC programming, it is important to know how the time to process changes as the number of values grows.
We want to see how the work done grows when more values need scaling.
Analyze the time complexity of the following code snippet.
FOR i := 0 TO n-1 DO
scaledValue[i] := (rawValue[i] - inputMin) * (outputMax - outputMin) / (inputMax - inputMin) + outputMin;
END_FOR
This code scales each raw analog input value to a new range, processing all values one by one.
- Primary operation: The FOR loop that scales each value.
- How many times: Exactly n times, once for each input value.
Each new input value adds one more scaling operation, so the total work grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 scaling calculations |
| 100 | 100 scaling calculations |
| 1000 | 1000 scaling calculations |
Pattern observation: The work increases evenly as the number of values increases.
Time Complexity: O(n)
This means the time to scale values grows in direct proportion to how many values there are.
[X] Wrong: "Scaling multiple values takes the same time no matter how many values there are."
[OK] Correct: Each value needs its own calculation, so more values mean more work and more time.
Understanding how loops affect time helps you explain how your code handles larger data sets efficiently and clearly.
What if we added a nested loop inside the FOR loop to process each value multiple times? How would the time complexity change?
Practice
Solution
Step 1: Understand analog input signals
Raw sensor signals are usually voltage or current values that need interpretation.Step 2: Purpose of scaling
Scaling converts these raw signals into meaningful units like temperature or pressure.Final Answer:
To convert raw sensor signals into meaningful engineering units -> Option CQuick Check:
Scaling = Convert raw to useful units [OK]
- Confusing scaling with speeding up the PLC
- Thinking scaling reduces program size
- Mixing analog and digital signal conversion
raw from range raw_min to raw_max into a scaled output range scale_min to scale_max?Solution
Step 1: Recall the scaling formula
The formula maps raw input linearly to the scaled range using subtraction and division.Step 2: Check each formula
scaled = (raw - raw_min) * (scale_max - scale_min) / (raw_max - raw_min) + scale_min correctly subtracts raw_min, scales by ratio of output to input range, then adds scale_min.Final Answer:
scaled = (raw - raw_min) * (scale_max - scale_min) / (raw_max - raw_min) + scale_min -> Option AQuick Check:
Scaling formula = scaled = (raw - raw_min) * (scale_max - scale_min) / (raw_max - raw_min) + scale_min [OK]
- Adding raw_min instead of subtracting
- Using addition instead of subtraction in denominator
- Mixing max and min values incorrectly
scaled = (raw - 0) * (100 - 0) / (4095 - 0) + 0, what is the scaled temperature?Solution
Step 1: Substitute values into formula
scaled = (2500 - 0) * (100 - 0) / (4095 - 0) + 0 = 2500 * 100 / 4095Step 2: Calculate the scaled value
scaled = 250000 / 4095 ≈ 61.07°CFinal Answer:
61.07°C -> Option AQuick Check:
2500/4095 ≈ 0.61 * 100 = 61.07 [OK]
- Using raw_max as 2500 instead of 4095
- Forgetting to subtract raw_min
- Mixing up numerator and denominator
scaled = (raw - 100) * (200 - 0) / (1000 - 0) + 0
What is the error in this scaling formula?
Solution
Step 1: Identify raw input range
Raw input minimum is 100, maximum is 1000, so input range is 1000 - 100 = 900.Step 2: Check denominator in formula
The formula uses (1000 - 0) = 1000, which is incorrect; it should be (1000 - 100) = 900.Final Answer:
The denominator should be (1000 - 100) instead of (1000 - 0) -> Option BQuick Check:
Input range = max - min = 900, not 1000 [OK]
- Using wrong input range denominator
- Confusing scale and raw min/max
- Subtracting scale_min instead of adding
raw to pressure?Solution
Step 1: Identify raw and scale ranges
Raw range: 500 to 3500; pressure range: 20 to 100 psi.Step 2: Apply scaling formula
Use (raw - raw_min) * (scale_max - scale_min) / (raw_max - raw_min) + scale_min.Step 3: Check each option
pressure = (raw - 500) * (100 - 20) / (3500 - 500) + 20 correctly uses raw_min=500, raw_max=3500, scale_min=20, scale_max=100.Final Answer:
pressure = (raw - 500) * (100 - 20) / (3500 - 500) + 20 -> Option DQuick Check:
Correct raw and scale min/max used in formula [OK]
- Using 0 instead of raw_min in subtraction
- Adding 0 instead of scale_min at end
- Mixing raw_max with 0 in denominator
Ready to prove it?
Pass the test and this pattern moves unknown
