def is_in_dict(x, dic, tol=0.01, by='max_err'): """ 用来判断一个向量是否包含于一个字典所描述的子空间中 Example: >>> W1 = np.mat([[0.5, 0, 0], [0.5, 1, 0], [0, 0, 0], [0, 0, 1]]) >>> W2 = np.mat([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]], dtype=np.double) >>> v = W1[:, 0] >>> is_in_dict(v, W2) True :param x: 待测试向量 :param dic: 描述子空间字典 :param tol: 可接受的阈值 :param by: 判断方式,max_err,mean_err,act """ if x is not np.matrixlib.defmatrix.matrix: vec = np.mat(x).T else: vec = x if vec.shape[1] != 1: vec = vec.T h = decompose_with_dict(vec, dic) re_ = np.dot(dic, h) err = np.square(vec - re_).reshape(-1, 1) if by == 'max_err': return np.max(err) < tol elif by == 'mean_err': return np.mean(err) < tol elif by == 'act': return np.any(h <= tol) else: raise Exception('get wrong param by')
def __getWight(self): if os.access('Mixwight.txt', os.F_OK): wight = loadValue('Mixwight.txt') else: wight = decompose_with_dict(np.abs(self.__signalMixSTFT), self.__dictMixVector) dumpValue(wight, 'Mixwight.txt') return wight
def Initialization(): flielist = [ r'dataspeech\track1.wav', r'dataspeech\track10.wav', r'dataspeech\across.wav' ] signalA = readSignal(flielist[0]) signalB = readSignal(flielist[1]) mixSignal = readSignal(flielist[2]) IndividVector, W_siga, W_sigb, sigADict, sigBdict = MixVectorAndIndividVector( signalA, signalB).process() specSignal = get_spec(mixSignal) W_mix = decompose_with_dict(np.abs(specSignal), IndividVector) return IndividVector, W_mix, sigADict, W_siga, sigBdict, W_sigb
def get_child(x, dic, tol=0.001): """ 获取一个向量所包含的字典中的成分 Example: >>> W1 = np.mat([[0.5, 0, 0], [0.5, 1, 0], [0, 0, 0], [0, 0, 1]]) >>> W2 = np.mat([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0]], dtype=np.double) >>> x = W1[:, 0] >>> get_child(x, W2) [0, 1] """ if x is not np.matrixlib.defmatrix.matrix: vec = np.mat(x).T else: vec = x if vec.shape[1] != 1: vec = vec.T h = decompose_with_dict(vec, dic) return list(filter(lambda x: h[x] > tol, range(len(h))))
def __getRata(self): if os.access('CommonRata.txt', os.F_OK): rate = loadValue('CommonRata.txt') else: value = None m = self.__mixVector.shape[1] k = self.__dictMixVector.shape[1] for i in range(0, m): V = np.transpose(self.__mixVector[:, i]).reshape(-1, 1) dic = self.__dictMixVector[:, k - m - 2:k] wigth = decompose_with_dict(V, dic) if value is None: value = wigth else: value = np.column_stack([value, wigth]) COUNT = 10**(-10) rate = (np.sum(value[:, 0:self.__sigMixLen], axis=1) + COUNT) / (np.sum(value, axis=1) + 2 * COUNT) dumpValue(rate, 'CommonRata.txt') return rate
def fit_noise(self, noise_spec): """ 对已知的字典,使用字典分解,来学习公共字典对特征字典的对应关系 Example: >>> import librosa >>> from utils import get_spec, norm_signal >>> noise, _ = librosa.load('data/noise/factory1.wav', sr=SAMPLE_RATE) >>> speech, _ = librosa.load('data/train/sa1.wav', sr=SAMPLE_RATE) >>> speech_spec = get_spec(norm_signal(speech)) >>> noise_spec = get_spec(norm_signal(noise)) >>> speech_dic, _ = decompose(np.abs(speech_spec), k=60) >>> noise_dic, _ = decompose(np.abs(noise_spec), k=20) >>> cs = CommonSpace(speech_dic, noise_dic) >>> cs.fit_noise(np.abs(noise_spec)) """ H = decompose_with_dict(noise_spec, self._noise_dic) for i in range(H.shape[1]): data = H[:, i] x = np.delete(data, self._noise_list) y = data[self._noise_list] self._learning_table.append((x, y))