131. ADC Offset Calibration

An ADCSensor class processes readings from an Analog-to-Digital Converter (ADC). Due to hardware circuit imperfections, the sensor signal has a permanent DC noise offset of 100 units. This means even when the input is zero, the sensor reads 100. Currently, the class exposes this calibration detail poorly. It allows users to access raw values directly or perform the subtraction manually. This often leads to bugs where users forget to handle the "negative undershoot" case (where a raw reading is slightly below the offset due to noise), resulting in invalid negative values for a unipolar sensor. Your task is to encapsulate this logic:

  • Store the offset (100) as a private, fixed constant.
  • Implement a public method getCalibratedSample(int raw_val).
  • This method must subtract the offset from the raw input.
  • If the result is negative (i.e., raw < offset), it must be clamped to 0 (saturation).

Program Flow:

  1. Initialize ADCSensor (Internal offset is fixed at 100).
  2. Read integer N (number of samples).
  3. Loop N times:
  4. Read integer raw_input (simulating a hardware register read).
  5. Call the driver to get the calibrated value.
  6. Print the calibrated value.

Input Format:

  • First line: Integer N (number of samples).
  • Next N lines: Integer raw_input.
  • Input is provided via standard input (stdin).

Output Format:

  • For each sample, print: Sample: <value>
  • Each output must be on a new line.

Example: 

Example 1

Input:

3
250
100
50

Output:

Sample: 150
Sample: 0
Sample: 0

 

Constraints:

  • N range: 1 to 20
  • raw_input range: 0 to 4095 (Standard 12-bit ADC range)
  • Fixed Offset: 100
  • Result cannot be negative (Clamp to 0)
  • No dynamic memory allocation

 

 

 

Loading...

Input

3 250 100 50

Expected Output

Sample: 150 Sample: 0 Sample: 0