def resample(self, rate):
rate1 = self.sample_rate.value<EOL>if isinstance(rate, units.Quantity):<EOL><INDENT>rate2 = rate.value<EOL><DEDENT>else:<EOL><INDENT>rate2 = float(rate)<EOL><DEDENT>if (rate2 / rate1).is_integer():<EOL><INDENT>raise NotImplementedError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>elif (rate1 / rate2).is_integer():<EOL><INDENT>factor = int(rate1 / rate2)<EOL>newsize = int(self.size / factor)<EOL>old = self.value.reshape((newsize, self.size // newsize))<EOL>if self.bits:<EOL><INDENT>nbits = len(self.bits)<EOL><DEDENT>else:<EOL><INDENT>max_ = self.value.max()<EOL>nbits = int(ceil(log(max_, <NUM_LIT:2>))) if max_ else <NUM_LIT:1><EOL><DEDENT>bits = range(nbits)<EOL>itr = numpy.nditer(<EOL>[old, None],<EOL>flags=['<STR_LIT>', '<STR_LIT>'],<EOL>op_axes=[None, [<NUM_LIT:0>, -<NUM_LIT:1>]],<EOL>op_flags=[['<STR_LIT>'], ['<STR_LIT>', '<STR_LIT>']])<EOL>dtype = self.dtype<EOL>type_ = self.dtype.type<EOL>for x, y in itr:<EOL><INDENT>y[...] = numpy.sum([type_((x >> bit & <NUM_LIT:1>).all() * (<NUM_LIT:2> ** bit))<EOL>for bit in bits], dtype=self.dtype)<EOL><DEDENT>new = StateVector(itr.operands[<NUM_LIT:1>], dtype=dtype)<EOL>new.__metadata_finalize__(self)<EOL>new._unit = self.unit<EOL>new.sample_rate = rate2<EOL>return new<EOL><DEDENT>elif rate1 < rate2:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>
Resample this `StateVector` to a new rate

        Because of the nature of a state-vector, downsampling is done
        by taking the logical 'and' of all original samples in each new
        sampling interval, while upsampling is achieved by repeating
        samples.

        Parameters
        ----------
        rate : `float`
            rate to which to resample this `StateVector`, must be a
            divisor of the original sample rate (when downsampling)
            or a multiple of the original (when upsampling).

        Returns
        -------
        vector : `StateVector`
            resampled version of the input `StateVector`