def idct(x, type=2, n=None, axis=-1, norm='ortho'): # @ReservedAssignment ''' Return the Inverse Discrete Cosine Transform of an arbitrary type sequence. Parameters ---------- x : array_like The input array. type : {1, 2, 3}, optional Type of the DCT (see Notes). Default type is 2. n : int, optional Length of the transform. axis : int, optional Axis over which to compute the transform. norm : {None, 'ortho'}, optional Normalization mode (see Notes). Default is 'ortho'. Returns ------- y : ndarray of real The transformed input array. See Also -------- dct Notes ----- For a single dimension array `x`, ``idct(x, norm='ortho')`` is equal to matlab ``idct(x)``. 'The' IDCT is the IDCT of type 2, which is the same as DCT of type 3. IDCT of type 1 is the DCT of type 1, IDCT of type 2 is the DCT of type 3, and IDCT of type 3 is the DCT of type 2. For the definition of these types, see `dct`. ''' farr = np.asarray if n is not None: # Hack until scipy.fftpack.idct implement this functionality x = _truncate_or_zero_pad(x, n, axis) n = None if np.iscomplex(x).any(): x0 = farr(x, dtype=complex) return (_idct(x0.real, type, n, axis, norm) + 1j * _idct(x0.imag, type, n, axis, norm)) else: return _idct(farr(x, dtype=float), type, n, axis, norm)
def lowpass_filter(data, max_freq=None): """ Implements a low-pass filter on the input array, by DCTing the input, mapping all but the lowest `max_freq` modes to zero, and then inverting the transform. Parameters ---------- data : numpy.array, The vector to low-pass filter max_freq : None or int, optional The highest frequency to keep. If None then it keeps the minimum of 50 or l/10 frequencies, where l is the length of the data vector Returns ------- numpy.array The low-pass-filtered data. """ n = len(data) if max_freq is None: max_freq = min(int(_np.ceil(n / 10)), 50) modes = _dct(data, norm='ortho') if max_freq < n - 1: modes[max_freq + 1:] = _np.zeros(len(data) - max_freq - 1) out = _idct(modes, norm='ortho') return out
def idct(modes, null_hypothesis, counts=1): """ Inverts the dct function. Parameters ---------- modes : array The fourier modes to be transformed to the time domain. null_hypothesis : array The array that was used in the normalization before the dct. This is commonly the mean of the time-domain data vector. All elements of this array must be in (0,1). counts : int, optional A factor in the normalization, that should correspond to the counts-per-timestep (so for full time resolution this is 1). Returns ------- array Inverse of the dct function """ z = _idct(modes, norm='ortho') x = unstandardizer(z, null_hypothesis, counts) return x
def idctn(x, type=2, axis=None, norm='ortho'): # @ReservedAssignment y = np.atleast_1d(x) shape0 = y.shape if axis is None: y = y.squeeze() # Working across singleton dimensions is useless ndim = y.ndim isvector = max(shape0) == y.size if isvector: if ndim == 1: y = np.atleast_2d(y) y = y.T elif y.shape[0] == 1: if axis == 0: return x elif axis == 1: axis = 0 y = y.T elif axis == 1: return y if np.iscomplex(y).any(): y = idctn(y.real, type, axis, norm) + 1j * \ idctn(y.imag, type, axis, norm) else: y = np.asfarray(y) for dim in range(ndim): y = y.transpose(np.roll(range(y.ndim), -1)) #y = shiftdim(y,1) if axis is not None and dim != axis: continue y = _idct(y, type, norm=norm) return y.reshape(shape0)
def IDCT(modes, null_hypothesis, counts=1): """ Inverts the DCT function. Parameters ---------- modes : array The fourier modes to be transformed to time-domain. null_hypothesis : array The null_hypothesis vector. For the IDCT it is not optional, and all elements of this array must be in (0,1). counts : int, optional TODO Returns ------- array Inverse of the DCT function """ assert (min(null_hypothesis) > 0 and max(null_hypothesis) < 1 ), "All element of null_hypothesis must be in (0,1)!" assert ( len(null_hypothesis) == len(modes) ), "The null hypothesis array must be the same length as the data array!" return _idct(modes, norm='ortho') * _np.sqrt( counts * null_hypothesis * (1 - null_hypothesis)) + counts * null_hypothesis
def idctn(x, type=2, axis=None, norm='ortho'): # @ReservedAssignment y = np.atleast_1d(x) shape0 = y.shape if axis is None: y = y.squeeze() # Working across singleton dimensions is useless ndim = y.ndim isvector = max(shape0) == y.size if isvector: if ndim == 1: y = np.atleast_2d(y) y = y.T elif y.shape[0] == 1: if axis == 0: return x elif axis == 1: axis = 0 y = y.T elif axis == 1: return y if np.iscomplex(y).any(): y = idctn(y.real, type, axis, norm) + 1j * \ idctn(y.imag, type, axis, norm) else: y = np.asfarray(y) for dim in range(ndim): y = y.transpose(np.roll(list(range(y.ndim)), -1)) #y = shiftdim(y,1) if axis is not None and dim != axis: continue y = _idct(y, type, norm=norm) return y.reshape(shape0)
def idct(x, type=2, n=None, axis=-1, norm='ortho'): # @ReservedAssignment ''' Return the Inverse Discrete Cosine Transform of an arbitrary type sequence. Parameters ---------- x : array_like The input array. type : {1, 2, 3}, optional Type of the DCT (see Notes). Default type is 2. n : int, optional Length of the transform. axis : int, optional Axis over which to compute the transform. norm : {None, 'ortho'}, optional Normalization mode (see Notes). Default is 'ortho'. Returns ------- y : ndarray of real The transformed input array. See Also -------- dct Notes ----- For a single dimension array `x`, ``idct(x, norm='ortho')`` is equal to matlab ``idct(x)``. 'The' IDCT is the IDCT of type 2, which is the same as DCT of type 3. IDCT of type 1 is the DCT of type 1, IDCT of type 2 is the DCT of type 3, and IDCT of type 3 is the DCT of type 2. For the definition of these types, see `dct`. ''' farr = np.asarray if np.iscomplex(x).any(): return _idct(farr(x.real), type, n, axis, norm) + \ 1j * _idct(farr(x.imag), type, n, axis, norm) else: return _idct(farr(x), type, n, axis, norm)
def low_pass_filter(data, max_freq=None): """ TODO: docstring """ n = len(data) if max_freq is None: max_freq = min(int(np.ceil(n / 10)), 50) modes = _dct(data, norm='ortho') if max_freq < n - 1: modes[max_freq + 1:] = _np.zeros(len(data) - max_freq - 1) return _idct(modes, norm='ortho')