import torch


def compute_delta(feats: torch.Tensor, dim: int = -1) -> torch.Tensor:
    """Compute delta coefficients of a tensor.

    Args:
        feats: Input features to compute deltas with.
        dim: feature dimension in the feats tensor.

    Returns:
        Tensor: Tensor of deltas.

    Examples
        >>> import torch
        >>> phase = torch.randn(2, 257, 100)
        >>> # Compute instantaneous frequency
        >>> inst_freq = compute_delta(phase, dim=-1)
        >>> # Or group delay
        >>> group_delay = compute_delta(phase, dim=-2)
    """
    if dim != -1:
        return compute_delta(feats.transpose(-1, dim), dim=-1).transpose(-1, dim)
    # First frame has nothing. Then each frame is the diff with the previous one.
    delta = feats.new_zeros(feats.shape)
    delta[..., 1:] = feats[..., 1:] - feats[..., :-1]
    return delta