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 operatoruse_oldest_time: (Optional) Controls timestamp of output messagestrue: Use newer message's timestampfalse: 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
| Time | Input Value | Output Value | Notes |
|---|---|---|---|
| 1 | 10.0 | - | First value buffered |
| 2 | 15.0 | 5.0 | First difference (15.0 - 10.0) |
| 4 | 12.0 | -3.0 | Next difference (12.0 - 15.0) |
| 5 | 20.0 | 8.0 | Next 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