Skip to content

Math Channel

Math channel operations and expressions

Math channel operations for waveform analysis.

MathOperations

Static methods for mathematical operations on waveforms.

add staticmethod

add(waveform1, waveform2)

Add two waveforms.

Parameters:

Name Type Description Default
waveform1

First waveform

required
waveform2

Second waveform

required

Returns:

Type Description

Result waveform with voltage = v1 + v2

Source code in scpi_control/math_channel.py
@staticmethod
def add(waveform1, waveform2):
    """Add two waveforms.

    Args:
        waveform1: First waveform
        waveform2: Second waveform

    Returns:
        Result waveform with voltage = v1 + v2
    """
    if waveform1 is None or waveform2 is None:
        return None

    return MathOperations._create_result_waveform(waveform1, waveform1.voltage + waveform2.voltage)

subtract staticmethod

subtract(waveform1, waveform2)

Subtract two waveforms.

Parameters:

Name Type Description Default
waveform1

First waveform

required
waveform2

Second waveform

required

Returns:

Type Description

Result waveform with voltage = v1 - v2

Source code in scpi_control/math_channel.py
@staticmethod
def subtract(waveform1, waveform2):
    """Subtract two waveforms.

    Args:
        waveform1: First waveform
        waveform2: Second waveform

    Returns:
        Result waveform with voltage = v1 - v2
    """
    if waveform1 is None or waveform2 is None:
        return None

    return MathOperations._create_result_waveform(waveform1, waveform1.voltage - waveform2.voltage)

multiply staticmethod

multiply(waveform1, waveform2)

Multiply two waveforms.

Parameters:

Name Type Description Default
waveform1

First waveform

required
waveform2

Second waveform

required

Returns:

Type Description

Result waveform with voltage = v1 * v2

Source code in scpi_control/math_channel.py
@staticmethod
def multiply(waveform1, waveform2):
    """Multiply two waveforms.

    Args:
        waveform1: First waveform
        waveform2: Second waveform

    Returns:
        Result waveform with voltage = v1 * v2
    """
    if waveform1 is None or waveform2 is None:
        return None

    return MathOperations._create_result_waveform(waveform1, waveform1.voltage * waveform2.voltage)

divide staticmethod

divide(waveform1, waveform2, epsilon=1e-12)

Divide two waveforms.

Parameters:

Name Type Description Default
waveform1

First waveform (numerator)

required
waveform2

Second waveform (denominator)

required
epsilon

Small value to prevent division by zero

1e-12

Returns:

Type Description

Result waveform with voltage = v1 / v2

Source code in scpi_control/math_channel.py
@staticmethod
def divide(waveform1, waveform2, epsilon=1e-12):
    """Divide two waveforms.

    Args:
        waveform1: First waveform (numerator)
        waveform2: Second waveform (denominator)
        epsilon: Small value to prevent division by zero

    Returns:
        Result waveform with voltage = v1 / v2
    """
    if waveform1 is None or waveform2 is None:
        return None

    # Prevent division by zero
    denominator = np.where(np.abs(waveform2.voltage) < epsilon, epsilon, waveform2.voltage)

    return MathOperations._create_result_waveform(waveform1, waveform1.voltage / denominator)

integrate staticmethod

integrate(waveform)

Integrate a waveform (cumulative sum).

Parameters:

Name Type Description Default
waveform

Input waveform

required

Returns:

Type Description

Result waveform with integrated values

Source code in scpi_control/math_channel.py
@staticmethod
def integrate(waveform):
    """Integrate a waveform (cumulative sum).

    Args:
        waveform: Input waveform

    Returns:
        Result waveform with integrated values
    """
    if waveform is None:
        return None

    # Calculate time step
    dt = np.mean(np.diff(waveform.time)) if len(waveform.time) > 1 else 1.0

    # Cumulative integration using trapezoidal rule
    integrated = np.cumsum(waveform.voltage) * dt

    return MathOperations._create_result_waveform(waveform, integrated)

differentiate staticmethod

differentiate(waveform)

Differentiate a waveform.

Parameters:

Name Type Description Default
waveform

Input waveform

required

Returns:

Type Description

Result waveform with differentiated values

Source code in scpi_control/math_channel.py
@staticmethod
def differentiate(waveform):
    """Differentiate a waveform.

    Args:
        waveform: Input waveform

    Returns:
        Result waveform with differentiated values
    """
    if waveform is None:
        return None

    # Calculate derivative using numpy gradient (more stable than diff)
    dt = np.mean(np.diff(waveform.time)) if len(waveform.time) > 1 else 1.0
    differentiated = np.gradient(waveform.voltage, dt)

    return MathOperations._create_result_waveform(waveform, differentiated)

scale staticmethod

scale(waveform, factor)

Scale a waveform by a constant factor.

Parameters:

Name Type Description Default
waveform

Input waveform

required
factor

Scaling factor

required

Returns:

Type Description

Result waveform with voltage = v * factor

Source code in scpi_control/math_channel.py
@staticmethod
def scale(waveform, factor):
    """Scale a waveform by a constant factor.

    Args:
        waveform: Input waveform
        factor: Scaling factor

    Returns:
        Result waveform with voltage = v * factor
    """
    if waveform is None:
        return None

    return MathOperations._create_result_waveform(waveform, waveform.voltage * factor)

offset staticmethod

offset(waveform, offset_value)

Add a DC offset to a waveform.

Parameters:

Name Type Description Default
waveform

Input waveform

required
offset_value

Offset to add

required

Returns:

Type Description

Result waveform with voltage = v + offset

Source code in scpi_control/math_channel.py
@staticmethod
def offset(waveform, offset_value):
    """Add a DC offset to a waveform.

    Args:
        waveform: Input waveform
        offset_value: Offset to add

    Returns:
        Result waveform with voltage = v + offset
    """
    if waveform is None:
        return None

    return MathOperations._create_result_waveform(waveform, waveform.voltage + offset_value)

abs_value staticmethod

abs_value(waveform)

Absolute value of a waveform.

Parameters:

Name Type Description Default
waveform

Input waveform

required

Returns:

Type Description

Result waveform with voltage = |v|

Source code in scpi_control/math_channel.py
@staticmethod
def abs_value(waveform):
    """Absolute value of a waveform.

    Args:
        waveform: Input waveform

    Returns:
        Result waveform with voltage = |v|
    """
    if waveform is None:
        return None

    return MathOperations._create_result_waveform(waveform, np.abs(waveform.voltage))

invert staticmethod

invert(waveform)

Invert a waveform.

Parameters:

Name Type Description Default
waveform

Input waveform

required

Returns:

Type Description

Result waveform with voltage = -v

Source code in scpi_control/math_channel.py
@staticmethod
def invert(waveform):
    """Invert a waveform.

    Args:
        waveform: Input waveform

    Returns:
        Result waveform with voltage = -v
    """
    if waveform is None:
        return None

    return MathOperations._create_result_waveform(waveform, -waveform.voltage)

MathChannel

MathChannel(scope, name: str)

Math channel for performing operations on oscilloscope waveforms.

Supports expressions like: - "C1 + C2" - "C1 - C2" - "C1 * C2" - "C1 / C2" - "INTG(C1)" - integrate - "DIFF(C1)" - differentiate - "ABS(C1)" - absolute value - "INV(C1)" - invert - "2 * C1 + 1" - scale and offset

Initialize math channel.

Parameters:

Name Type Description Default
scope

Parent oscilloscope instance

required
name str

Math channel name (e.g., "M1", "M2")

required
Source code in scpi_control/math_channel.py
def __init__(self, scope, name: str):
    """Initialize math channel.

    Args:
        scope: Parent oscilloscope instance
        name: Math channel name (e.g., "M1", "M2")
    """
    self.scope = scope
    self.name = name
    self.expression = ""
    self.enabled = False
    self._result_waveform = None

    logger.info(f"Math channel {name} initialized")

set_expression

set_expression(expression: str)

Set the math expression.

Parameters:

Name Type Description Default
expression str

Math expression string

required
Source code in scpi_control/math_channel.py
def set_expression(self, expression: str):
    """Set the math expression.

    Args:
        expression: Math expression string
    """
    self.expression = expression.strip()
    logger.info(f"Math channel {self.name} expression set to: {self.expression}")

enable

enable()

Enable the math channel.

Source code in scpi_control/math_channel.py
def enable(self):
    """Enable the math channel."""
    self.enabled = True
    logger.info(f"Math channel {self.name} enabled")

disable

disable()

Disable the math channel.

Source code in scpi_control/math_channel.py
def disable(self):
    """Disable the math channel."""
    self.enabled = False
    self._result_waveform = None
    logger.info(f"Math channel {self.name} disabled")

compute

compute(waveforms: Dict[str, any]) -> Optional[any]

Compute the math channel result.

Parameters:

Name Type Description Default
waveforms Dict[str, any]

Dictionary of channel_name -> waveform

required

Returns:

Type Description
Optional[any]

Computed waveform or None if disabled/error

Source code in scpi_control/math_channel.py
def compute(self, waveforms: Dict[str, any]) -> Optional[any]:
    """Compute the math channel result.

    Args:
        waveforms: Dictionary of channel_name -> waveform

    Returns:
        Computed waveform or None if disabled/error
    """
    if not self.enabled or not self.expression:
        return None

    try:
        result = self._evaluate_expression(self.expression, waveforms)
        self._result_waveform = result
        return result
    except Exception as e:
        logger.error(f"Math channel {self.name} computation error: {e}")
        return None

get_result

get_result()

Get the most recent computation result.

Returns:

Type Description

Result waveform or None

Source code in scpi_control/math_channel.py
def get_result(self):
    """Get the most recent computation result.

    Returns:
        Result waveform or None
    """
    return self._result_waveform

See Also

  • Oscilloscope - Main oscilloscope control class for SCPI communication
  • Waveform - Waveform acquisition and data handling