Skip to main content

Difference

The Difference operator calculates sequential differences between pairs of numeric values in a data stream. It maintains a buffer of size 2 to compute differences between consecutive values.

Configuration

Required Parameters

  • id: Unique identifier for the operator
  • use_oldest_time: (Optional) Controls timestamp of output messages
    • true: Use newer message's timestamp
    • false: Use older message's timestamp

Example Configuration

{
"id": "diff1",
"use_oldest_time": true
}

Port Configuration

Inputs

  • Port 0: Accepts NumberData messages

Outputs

  • Port 0: Emits NumberData messages with computed differences

Operation

The operator emits messages with values calculated as:

output_value = newest_value - oldest_value

Message Flow Example

TimeInput ValueOutput ValueNotes
110.0-First value buffered
215.05.0First difference (15.0 - 10.0)
412.0-3.0Next difference (12.0 - 15.0)
520.08.0Next difference (20.0 - 12.0)

Key Characteristics

  • Buffer size: 2 messages
  • Output timing: Configurable via use_oldest_time
  • Processing: O(1) per message
  • Memory usage: O(1) fixed
  • Must receive 2 messages before first output

Error Handling

The operator will throw exceptions for:

  • Invalid message types on input port
  • Type mismatches on input

Use Cases

Ideal for:

  • Rate of change calculations
  • Delta detection
  • Trend analysis
  • Signal differentiation

Example Usage

// Create operator using newer message timestamps
auto diff = std::make_shared<Difference>("diff1", true);
auto col = std::make_shared<Collector>("c", std::vector<std::string>{"number"});
diff->connect(col, 0, 0);

// Process some values
diff->receive_data(create_message<NumberData>(1, NumberData{10.0}), 0);
diff->receive_data(create_message<NumberData>(2, NumberData{15.0}), 0);
diff->execute();

// Access difference via the downstream Collector
const auto& output = col->get_data_queue(0);
// Output will contain a message with value 5.0 at time 2