def get_fd_gradients(mp, deltas, multi_state_elt=None):
    """Calculate centered finite difference gradients for each of the
    parameters of the model parameterisation mp.

    "deltas" must be a sequence of the same length as the parameter list, and
    contains the step size for the difference calculations for each parameter.

    "multi_state_elt" selects a particular state for use when mp is a multi-
    state parameterisation.
    """

    p_vals = mp.get_param_vals()
    assert len(deltas) == len(p_vals)
    fd_grad = []

    for i in range(len(deltas)):

        val = p_vals[i]

        p_vals[i] -= deltas[i] / 2.0
        mp.set_param_vals(p_vals)
        if multi_state_elt is None:
            rev_state = mp.get_state()
        else:
            rev_state = mp.get_state(multi_state_elt=multi_state_elt)

        p_vals[i] += deltas[i]
        mp.set_param_vals(p_vals)
        if multi_state_elt is None:
            fwd_state = mp.get_state()
        else:
            fwd_state = mp.get_state(multi_state_elt=multi_state_elt)

        fd_grad.append((fwd_state - rev_state) / deltas[i])

        p_vals[i] = val

    # return to the initial state
    mp.set_param_vals(p_vals)

    return fd_grad