def value2diff(X, percent=True):
    """
    Returns the 1st discrete difference of array X.

    X           (n, )           Input dataset
    dX          (n-1, )         1st discrete differences

    Notes:
    - the discrete difference can be calculated in percent or in value.
    - dX is one element shorter than X.
    - X must be a 1D array.
    """
    X = X.flatten()             # Reshape to (n, )

    # Discrete difference in percent
    if (percent):
        dX = X[1:] / X[:-1] - 1.0

    # Discrete difference in value
    else:
        dX = X[1:] - X[:-1]

    return dX