131. ADC Offset Calibration

#include <iostream>

class ADCSensor {
private:
    // Fixed calibration offset hidden from the user
    const int offset_val; 

public:
    // Initialize offset to 100
    ADCSensor() : offset_val(100) {}

    // Process raw data into calibrated application data
    int getCalibratedSample(int raw) const {
        // Apply offset
        int calibrated = raw - offset_val;

        // Saturate to 0 (Clamp negative results)
        // This handles cases where noise drops the reading below the offset
        if (calibrated < 0) {
            return 0;
        }
        return calibrated;
    }
};

int main() {
    int N;
    if (!(std::cin >> N)) return 0;

    ADCSensor sensor;

    for (int i = 0; i < N; ++i) {
        int raw;
        std::cin >> raw;

        // Use the encapsulated interface to get clean data
        std::cout << "Sample: " << sensor.getCalibratedSample(raw) << std::endl;
    }

    return 0;
}

Explanation & Logic Summary: 

This problem addresses Data Transformation and Validity.

  • The Context: Raw hardware data is rarely "application-ready." It usually requires bias correction, scaling, or filtering.
  • The Flaw: The template code performed the math raw - offset inside main(). This exposes the offset value to the user and fails to handle the edge case where noise pulls the reading below the offset (resulting in negative numbers).
  • The Fix: The getCalibratedSample method encapsulates the correction algorithm. It centralizes the subtraction and adds the critical check if (calibrated < 0) return 0;.
  • The Benefit: The application layer now deals only with "clean" physics values (0 or positive), oblivious to the hardware calibration details.

Firmware Relevance & Real-World Context:

  • Sensor Fusion: In IMUs (accelerometers/gyroscopes), raw registers must be biased and scaled before use. Doing this ad-hoc in main leads to inconsistent data.
  • Unipolar Constraints: Many physical quantities (light intensity, pressure, distance) cannot be negative. Firmware must clamp noise-induced negative values to zero to prevent logic errors downstream.
  • Read-Only Config: Calibration constants (like offset_val) are often stored in OTP (One-Time Programmable) memory or Flash and should be const or private to prevent accidental modification during runtime.

 

 

 

Loading...

Input

3 250 100 50

Expected Output

Sample: 150 Sample: 0 Sample: 0