def invert_component(self, cls, w, z, h): """ Invert a single PLCA component to separated features in the original feature space. """ w = P.atleast_2d(w) if cls == plca.PLCA: w = w.T h = P.atleast_2d(h) return cls.reconstruct(w, z, h)
def stack_vectors(data, win=1, hop=1, zero_pad=True): """ :: create an overlapping stacked vector sequence from a series of vectors data - row-wise multidimensional data to stack win - number of consecutive vectors to stack [1] hop - number of vectors to advance per stack [1] zero_pad - zero pad if incomplete stack at end """ data = pylab.atleast_2d(data) nrows, dim = data.shape hop = min(hop, nrows) nvecs = nrows / int(hop) if not zero_pad else int( pylab.ceil(nrows / float(hop))) features = pylab.zeros((nvecs, win * dim)) i = 0 while i < nrows - win + 1: features[i / hop, :] = data[i:i + win, :].reshape(1, -1) i += hop if i / hop < nvecs: x = data[i::, :].reshape(1, -1) features[i / hop, :] = pylab.c_[x, pylab.zeros( (1, win * dim - x.shape[1]))] return features
def _pvoc2(self, X_hat, Phi_hat=None, R=None): """ :: alternate (batch) implementation of phase vocoder - time-stretch inputs: X_hat - estimate of signal magnitude [Phi_hat] - estimate of signal phase [R] - resynthesis hop ratio output: updates self.X_hat with modified complex spectrum """ N, W, H = self.nfft, self.wfft, self.nhop R = 1.0 if R is None else R dphi = P.atleast_2d((2 * P.pi * H * P.arange(N / 2 + 1)) / N).T print("Phase Vocoder Resynthesis...", N, W, H, R) A = P.angle(self.STFT) if Phi_hat is None else Phi_hat U = P.diff(A, 1) - dphi U = U - P.np.round(U / (2 * P.pi)) * 2 * P.pi t = P.arange(0, n_cols, R) tf = t - P.floor(t) phs = P.c_[A[:, 0], U] phs += U[:, idx[1]] + dphi # Problem, what is idx ? Xh = (1 - tf) * Xh[:-1] + tf * Xh[1:] Xh *= P.exp(1j * phs) self.X_hat = Xh
def _phase_map(self): self.dphi = (2 * P.pi * self.nhop * P.arange(self.nfft / 2 + 1)) / self.nfft A = P.diff(P.angle(self.STFT), 1) # Complete Phase Map U = P.c_[P.angle(self.STFT[:, 0]), A - P.atleast_2d(self.dphi).T] U = U - P.np.round(U / (2 * P.pi)) * 2 * P.pi self.dPhi = U return U
def _ichroma(self, V, **kwargs): """ :: Inverse chromagram transform. Make a signal from a folded constant-Q transform. """ if not (self._have_hcqft or self._have_cqft): return None a, b = self.HCQFT.shape if self._have_hcqft else self.CQFT.shape complete_octaves = a / self.nbpo # integer division, number of complete octaves if P.remainder(a, self.nbpo): complete_octaves += 1 X = P.repeat(V, complete_octaves, 0)[:a, :] # truncate if necessary X /= X.max() X *= P.atleast_2d(P.linspace(1, 0, X.shape[0])).T # weight the spectrum self.x_hat = self._icqft(X, **kwargs) return self.x_hat
def extract(self): Features.extract(self) mf = (self.X.T * self._logfrqs).sum(1) / self.X.T.sum(1) self.X = (((self.X / self.X.T.sum(1)).T * ((P.atleast_2d(self._logfrqs).T - mf)).T)**2).sum(1)