* @param lenA Dimensions of A
     * @param lenB Dimensions of B
     * @param mode Correlation mode; see PyMulitarray.convolve
     * @return Unpadded vector
     */
    public static PyMultiarray XC_unpad_vector(final PyMultiarray R, int lenA, int lenB, final int mode) {
        final boolean debug_this = false;
        if (debug_this) {
            System.out.println("R = " + R);
        }
        lenB = Math.max(lenA, lenB);
        lenA = Math.min(lenA, lenB);
        final int L = lenA + lenB;
        final int L2 = L / 2;
        final char type = R.typecode();
        final int R_dim = R.dimensions[0];

        /*
         * First pull out the non-zero part of the FFT. Regardless of
         * the mode we're in, we'll always be returning a subset of
         * this. For two vectors of length lenA and lenB, there will
         * always be (lenA + lenB - 1) of these.
         */

        final PyMultiarray tmp = PyMultiarray.zeros(new int[] { L - 1 }, type);

        if (L % 2 == 1) {
            if (debug_this) {
                System.out.println("UNPAD(odd): type = '" + type + "', L2 = "
                        + L2 + ", R_dim = " + R_dim);
            }
            tmp.setslice(
                    0,
                    L2 + 1,
                    1,
                    R.getslice(R_dim - (L2 + 1), R_dim + 1, 1));
            tmp.setslice(L2, L - 1, 1, R.getslice(0, L2 - 1, 1));
        }
        else {
            if (debug_this) {
                System.out.println("UNPAD(even): type = '" + type + "', L2 = "
                        + L2 + ", R_dim = " + R_dim);
            }
            tmp.setslice(0, L2 - 1, 1, R.getslice(R_dim - L2 + 1, R_dim, 1));
            tmp.setslice(L2 - 1, L + 1, 1, R.getslice(0, L2, 1));
        }

        final int start = L - lenB;
        if (debug_this) {
            System.out.println("\nUNPAD: tmp swapped = " + tmp);
        }
        PyMultiarray result;

        // Now pull out the desired elements, in accordance with 'mode'.
        switch (mode) {
        case 0:
            result = PyMultiarray.zeros(new int[] { lenB - lenA + 1 }, type);
            if (debug_this) {
                System.out.println("\nUNPAD: L = " + L + ", lenB = " + lenB
                        + ", start = " + start);
            }
            result.setslice(
                    0,
                    L - 2 * start + 1,
                    1,
                    tmp.getslice(start - 1, L - start, 1));
            break;
        case 1:
            result = PyMultiarray.zeros(new int[] { lenB }, type);
            if (debug_this) {
                System.out.println("\nUNPAD: lenB = " + lenB + ", start/2 = "
                        + start / 2
                        + ", L-start/2 = " + (L - start / 2));
            }
            result.setslice(
                    0,
                    lenB,
                    1,
                    tmp.getslice(start / 2, lenB + start / 2, 1));
            break;
        case 2: // Return all elements.
            result = tmp;
            break;
        default:
            throw Py.ValueError("mode must be 0,1, or 2");
        }

        // Finally, reverse the result inplace, then return.
        result.setslice(
                0,
                result.dimensions[0],
                1,
                result.getslice(result.dimensions[0], -1, -1));
        if (debug_this) {
            System.out.println("\nUNPAD: result = " + result);
        }
        return result;
    }