Python scipy.cluster.vq.vq() Examples

The following are code examples for showing how to use scipy.cluster.vq.vq(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: att   Author: Centre-Alt-Rendiment-Esportiu   File: vq_test.py    GNU General Public License v3.0 7 votes vote down vote up
def python_vq(all_data,code_book):
    import time
    t1 = time.time()
    codes1,dist1 = vq.vq(all_data,code_book)
    t2 = time.time()
    #print 'fast (double):', t2 - t1
    #print '  first codes:', codes1[:5]
    #print '  first dist:', dist1[:5]
    #print '  last codes:', codes1[-5:]
    #print '  last dist:', dist1[-5:]
    float_obs = all_data.astype(np.float32)
    float_code = code_book.astype(np.float32)
    t1 = time.time()
    codes1,dist1 = vq.vq(float_obs,float_code)
    t2 = time.time()
    #print 'fast (float):', t2 - t1
    #print '  first codes:', codes1[:5]
    #print '  first dist:', dist1[:5]
    #print '  last codes:', codes1[-5:]
    #print '  last dist:', dist1[-5:]

    return codes1,dist1 
Example 2
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 6 votes vote down vote up
def quantize(fname, palette):
    """
    quantize an image with a given color palette
    """
    
    # read image and resize
    img = nd.imread(fname)
    
    # reshape to array of points
    pixels = np.reshape(img, (img.shape[0] * img.shape[1], 3))
    
    # quantize
    qnt, _ = vq(pixels, palette)
    
    # reshape back to image
    centers_idx = np.reshape(qnt, (img.shape[0], img.shape[1])) 
    clustered = palette[centers_idx]
    
    # return quantized image and histogram
    return clustered 
Example 3
Project: DominantColor   Author: michaelrhyndress   File: DominantColor.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_dominant(self):
        """
        ensures that we don't throw out too dark, or light of images
        which are usually in the background of photos. This comes in
        handy when images are transparent or are a logo
        """
        stop = False
        while stop == False:
            vecs, dist = vq(self.ar, self.codes) # assign codes
            counts, bins = histogram(vecs, len(self.codes))  # count occurrences
            index_max = argmax(counts)
            peak = self.codes[index_max]
            lumin = self.get_luminosity(peak)
            if lumin > 200.0 or lumin < 35.0: # too light, too dark
                self.codes = np_delete(self.codes,(index_max), axis=0)  #retry without that instance
            else:
                stop = True
                rgb = ''.join(chr(int(c)) for index, c in enumerate(peak) if not index == 3)
                dom_hex = "#%s" % rgb.encode('hex')
                rgb = tuple(peak[:3])
        return dom_hex, rgb 
Example 4
Project: GenefyHMM   Author: adit-39   File: train.py    MIT License 6 votes vote down vote up
def vector_quantize(data_dict, vs, bins):
	codebooks = {}
	vq_data = {}
	for size in vs.keys():
		all_size_data = []
		for disease in vs[size]:
			all_size_data.extend(data_dict[disease])
		#whitened = sp.whiten(all_size_data)
		#codebooks[size] = sp.kmeans(whitened, bins)[0]
		codebooks[size] = sp.kmeans(np.asarray(all_size_data), bins)[0]
	pickle.dump(codebooks,open("all_codebooks.pkl","wb"))
	for dis in data_dict.keys():
		n = len(data_dict[dis])
		m = len(data_dict[dis][0])
		vq_data[dis] = map(str,sp.vq(np.reshape(data_dict[dis],(n,m)), codebooks[len(data_dict[dis][0])])[0])
	return vq_data 
Example 5
Project: Computable   Author: ktraunmueller   File: test_vq.py    MIT License 6 votes vote down vote up
def test_vq(self):
        initc = np.concatenate(([[X[0]], [X[1]], [X[2]]]))
        if TESTC:
            label1, dist = _vq.vq(X, initc)
            assert_array_equal(label1, LABEL1)
            tlabel1, tdist = vq(X, initc)
        else:
            print("== not testing C imp of vq ==")

    #def test_py_vq_1d(self):
    #    """Test special rank 1 vq algo, python implementation."""
    #    data = X[:, 0]
    #    initc = data[:3]
    #    a, b = _py_vq_1d(data, initc)
    #    ta, tb = py_vq(data[:, np.newaxis], initc[:, np.newaxis])
    #    assert_array_equal(a, ta)
    #    assert_array_equal(b, tb) 
Example 6
Project: Computable   Author: ktraunmueller   File: vq_test.py    MIT License 6 votes vote down vote up
def python_vq(all_data,code_book):
    import time
    t1 = time.time()
    codes1,dist1 = vq.vq(all_data,code_book)
    t2 = time.time()
    #print 'fast (double):', t2 - t1
    #print '  first codes:', codes1[:5]
    #print '  first dist:', dist1[:5]
    #print '  last codes:', codes1[-5:]
    #print '  last dist:', dist1[-5:]
    float_obs = all_data.astype(np.float32)
    float_code = code_book.astype(np.float32)
    t1 = time.time()
    codes1,dist1 = vq.vq(float_obs,float_code)
    t2 = time.time()
    #print 'fast (float):', t2 - t1
    #print '  first codes:', codes1[:5]
    #print '  first dist:', dist1[:5]
    #print '  last codes:', codes1[-5:]
    #print '  last dist:', dist1[-5:]

    return codes1,dist1 
Example 7
Project: sign-language-character-recognition   Author: hthuwal   File: learn.py    MIT License 6 votes vote down vote up
def computeHistograms(codebook, descriptors, imagePath):
    code, dist = vq.vq(descriptors, codebook)
    print "Computing histogram of "+imagePath
    # print "Descriptors of " + imagePath
    # print descriptors
    # print ""
    # print "Code"
    # print code

    # to save histogram of each file
    plt.hist(code, bins=range(codebook.shape[0] + 1))
    histFolderPath = dirname(imagePath)+"/hist"
    if not exists(histFolderPath):
        makedirs(histFolderPath)
    plt.savefig(histFolderPath+"/"+basename(imagePath))
    plt.clf()
    ##

    histogram_of_words, bin_edges = histogram(code,
                                              bins=range(
                                                  codebook.shape[0] + 1),
                                              normed=True)
    return histogram_of_words 
Example 8
Project: senior-design   Author: james-tate   File: test_vq.py    GNU General Public License v2.0 6 votes vote down vote up
def test_vq(self):
        initc = np.concatenate(([[X[0]], [X[1]], [X[2]]]))
        code = initc.copy()
        if TESTC:
            label1, dist = _vq.vq(X, initc)
            assert_array_equal(label1, LABEL1)
            tlabel1, tdist = vq(X, initc)
        else:
            print "== not testing C imp of vq =="

    #def test_py_vq_1d(self):
    #    """Test special rank 1 vq algo, python implementation."""
    #    data = X[:, 0]
    #    initc = data[:3]
    #    code = initc.copy()
    #    a, b = _py_vq_1d(data, initc)
    #    ta, tb = py_vq(data[:, np.newaxis], initc[:, np.newaxis])
    #    assert_array_equal(a, ta)
    #    assert_array_equal(b, tb) 
Example 9
Project: senior-design   Author: james-tate   File: vq_test.py    GNU General Public License v2.0 6 votes vote down vote up
def python_vq(all_data,code_book):
    import time
    t1 = time.time()
    codes1,dist1 = vq.vq(all_data,code_book)
    t2 = time.time()
    #print 'fast (double):', t2 - t1
    #print '  first codes:', codes1[:5]
    #print '  first dist:', dist1[:5]
    #print '  last codes:', codes1[-5:]
    #print '  last dist:', dist1[-5:]
    float_obs = all_data.astype(np.float32)
    float_code = code_book.astype(np.float32)
    t1 = time.time()
    codes1,dist1 = vq.vq(float_obs,float_code)
    t2 = time.time()
    #print 'fast (float):', t2 - t1
    #print '  first codes:', codes1[:5]
    #print '  first dist:', dist1[:5]
    #print '  last codes:', codes1[-5:]
    #print '  last dist:', dist1[-5:]

    return codes1,dist1 
Example 10
Project: senior-design   Author: james-tate   File: ex1.py    GNU General Public License v2.0 6 votes vote down vote up
def cluster_data(data,cluster_cnt,iter=20,thresh=1e-5):
    """ Group data into a number of common clusters

        data -- 2D array of data points.  Each point is a row in the array.
        cluster_cnt -- The number of clusters to use
        iter -- number of iterations to use for kmeans algorithm
        thresh -- distortion threshold for kmeans algorithm

        return -- list of 2D arrays.  Each array contains the data points
                  that belong to a specific cluster.

        Uses kmeans algorithm to find the clusters.
    """
    wh_data = vq.whiten(data)
    code_book,dist = vq.kmeans(wh_data,cluster_cnt,iter,thresh)
    code_ids, distortion = vq.vq(wh_data,code_book)
    clusters = []
    for i in range(len(code_book)):
        cluster = compress(code_ids == i,data,0)
        clusters.append(cluster)
    return clusters 
Example 11
Project: ColorRecognition   Author: bijilap   File: ColorDetector.py    Apache License 2.0 5 votes vote down vote up
def analyzeImage(self, image):
        color_band = scipy.misc.fromimage(image)
        shape = color_band.shape
        color_band = color_band.reshape(scipy.product(shape[:2]), shape[2])

        self.log('generating clusters')
        codes, dist = kmeans(color_band, self.NUM_OF_CLUSTERS)
        self.log('Here are the cluster centres:')
        self.log(codes)

        vecs, dist = vq(color_band, codes)         # assign codes
        counts, bins = scipy.histogram(vecs, len(codes))    # count occurrences

        return (codes, counts) 
Example 12
Project: peri   Author: peri-source   File: tiling.py    MIT License 5 votes vote down vote up
def cluster_tiles_by_volume(tiles, volumes, nclusters, nattempts, max_pix=None):
    max_pix = max_pix or np.inf

    # cluster the tiles by volume
    logvol = np.log10(volumes)
    centers = vq.kmeans(logvol, nclusters)[0]
    labels = vq.vq(logvol, centers)[0]
    ids = np.arange(labels.max())

    # get the centers in order so we can walk down the list
    centers, ids = (
        list(t) for t in zip(*sorted(zip(centers, ids), reverse=True))
    )

    # get the groups that are viable based on memory constraints
    grouped_labels = [
        labels[labels==i] for i in ids if volumes[labels==i].max() < max_pix
    ]
    return grouped_labels
    """

    # do hierarchical clustering starting with the largest sizes
    for 
    grp = groups[centers.argmax()]
    return tiles, volumes
    """ 
Example 13
Project: GenefyHMM   Author: adit-39   File: test.py    MIT License 5 votes vote down vote up
def testHMMs(inp_file="test_input.txt"):
	inp_vals = map(float, open("test_input.txt","r").read().strip().split(','))
	HMMs = pickle.load(open("trained_HMMs_saved.pkl","rb"))
	codebooks = pickle.load(open("all_codebooks.pkl","rb"))
	vs = pickle.load(open("size_mapping.pkl","rb"))
	
	results = {}
	
	for size in vs.keys():
		# organize input data into vectors of particular size
		c = 0
		vecs = []
		for i in range(len(inp_vals)/int(size)):
			vecs.append(inp_vals[c:c+size])
			c += size
		#print vecs
		# Vector Quantizing
		n = len(vecs)
		vq_seq = map(str,sp.vq(np.reshape(vecs,(n,size)), codebooks[size])[0])
		if len(vq_seq) > 0:
			diseases = vs[size]
			for disease in diseases:
				HMM_obj = HMM("initial.json")
				HMM_obj.A = HMMs[disease]["A"]
				HMM_obj.B = HMMs[disease]["B"]
				HMM_obj.pi = HMMs[disease]["pi"]
				prob = HMM_obj.forward_scaled(vq_seq)
				results[disease] = math.exp(prob)
	
	for i in HMMs.keys():
		if not results.has_key(i):
			results[i] = "Not Enough Data to Predict"
	
	print "RESULTS:"		
	for dis in results.keys():
		print dis.capitalize()+": "+str(results[dis]) 
Example 14
Project: Computable   Author: ktraunmueller   File: test_vq.py    MIT License 5 votes vote down vote up
def test_vq_1d(self):
        """Test special rank 1 vq algo, python implementation."""
        data = X[:, 0]
        initc = data[:3]
        if TESTC:
            a, b = _vq.vq(data, initc)
            ta, tb = py_vq(data[:, np.newaxis], initc[:, np.newaxis])
            assert_array_equal(a, ta)
            assert_array_equal(b, tb)
        else:
            print("== not testing C imp of vq (rank 1) ==") 
Example 15
Project: Computable   Author: ktraunmueller   File: test_vq.py    MIT License 5 votes vote down vote up
def test__vq_sametype(self):
        if TESTC:
            a = np.array([1, 2])
            b = a.astype(float)
            assert_raises(ValueError, _vq.vq, a, b) 
Example 16
Project: simple-linear-regression   Author: williamd4112   File: preprocess.py    MIT License 5 votes vote down vote up
def kmeans(x, k):
    centroids, dist = _kmeans(x, k)
    idx, _ = vq(x,centroids)
    return idx, centroids, dist 
Example 17
Project: NoteShrinker   Author: ghandic   File: noteshrinker.py    MIT License 5 votes vote down vote up
def apply_palette(self):

        bg_color = self.palette[0]
        fg_mask = get_fg_mask(bg_color, self.original_image, self.value_threshold, self.sat_threshold)

        pixels = self.original_image.reshape((-1, 3))
        fg_mask = fg_mask.flatten()

        num_pixels = pixels.shape[0]
        labels = np.zeros(num_pixels, dtype=np.uint8)

        labels[fg_mask], _ = vq(pixels[fg_mask], self.palette)

        self.labels = labels.reshape(self.original_image_shape[:-1]) 
Example 18
Project: sebaPhD   Author: sebalander   File: synthDataGen.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def distortionNodes(indSel):
    mu10 = mu[indSel]
    code, dist = vq(data, mu10)
    return mu10, dist.sum()

# indexes to select from 20 to 10 
Example 19
Project: mmfeat   Author: douwekiela   File: bow.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def quantize(self):
        clusters = range(self.centroids.shape[0] + 1)
        histograms = {}
        for fname in sorted(self.data.keys()):
            if self.data[fname] is None: continue
            idx,_ = vq(self.data[fname], self.centroids)
            histograms[fname], _ = np.histogram(idx, bins=clusters, normed=self.normalize)
        return histograms 
Example 20
Project: mmfeat   Author: douwekiela   File: bow.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def sequences(self):
        sequences = {}
        for fname in sorted(self.data.keys()):
            if self.data[fname] is None: continue
            idx,_ = vq(self.data[fname], self.centroids)
            sequences[fname] = idx
        return sequences 
Example 21
Project: senior-design   Author: james-tate   File: test_vq.py    GNU General Public License v2.0 5 votes vote down vote up
def test_vq_1d(self):
        """Test special rank 1 vq algo, python implementation."""
        data = X[:, 0]
        initc = data[:3]
        code = initc.copy()
        if TESTC:
            a, b = _vq.vq(data, initc)
            ta, tb = py_vq(data[:, np.newaxis], initc[:, np.newaxis])
            assert_array_equal(a, ta)
            assert_array_equal(b, tb)
        else:
            print "== not testing C imp of vq (rank 1) ==" 
Example 22
Project: tomominer   Author: alberlab   File: kmeans.py    GNU General Public License v3.0 5 votes vote down vote up
def kmeans_clustering(data, k):
  """
  TODO: add docs

  :param data:
    :param k:
  """

  from scipy.cluster.vq import kmeans, vq, whiten

  data = whiten(data)
  centroids, _ = kmeans(data, k)
  labels,  _ = vq(data, centroids)

  return labels 
Example 23
Project: tools   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def run_phase_vq_example():
    def _pre(list_of_data):
        # Temporal window setting is crucial! - 512 seems OK for music, 256
        # fruit perhaps due to samplerates
        n_fft = 256
        step = 32
        f_r = np.vstack([np.abs(stft(dd, n_fft, step=step, real=False,
                                compute_onesided=False))
                         for dd in list_of_data])
        return f_r, n_fft, step

    def preprocess_train(list_of_data, random_state):
        f_r, n_fft, step = _pre(list_of_data)
        clusters = copy.deepcopy(f_r)
        return clusters

    def apply_preprocess(list_of_data, clusters):
        f_r, n_fft, step = _pre(list_of_data)
        f_clust = f_r
        # Nondeterministic ?
        memberships, distances = vq(f_clust, clusters)
        vq_r = clusters[memberships]
        d_k = iterate_invert_spectrogram(vq_r, n_fft, step, verbose=True)
        return d_k

    random_state = np.random.RandomState(1999)

    fs, d = fetch_sample_speech_fruit()
    d1 = d[::9]
    d2 = d[7::8][:5]
    # make sure d1 and d2 aren't the same!
    assert [len(di) for di in d1] != [len(di) for di in d2]

    clusters = preprocess_train(d1, random_state)
    fix_d1 = np.concatenate(d1)
    fix_d2 = np.concatenate(d2)
    vq_d2 = apply_preprocess(d2, clusters)

    wavfile.write("phase_train_no_agc.wav", fs, soundsc(fix_d1))
    wavfile.write("phase_vq_test_no_agc.wav", fs, soundsc(vq_d2))

    agc_d1, freq_d1, energy_d1 = time_attack_agc(fix_d1, fs, .5, 5)
    agc_d2, freq_d2, energy_d2 = time_attack_agc(fix_d2, fs, .5, 5)
    agc_vq_d2, freq_vq_d2, energy_vq_d2 = time_attack_agc(vq_d2, fs, .5, 5)

    """
    import matplotlib.pyplot as plt
    plt.specgram(agc_vq_d2, cmap="gray")
    #plt.title("Fake")
    plt.figure()
    plt.specgram(agc_d2, cmap="gray")
    #plt.title("Real")
    plt.show()
    """

    wavfile.write("phase_train_agc.wav", fs, soundsc(agc_d1))
    wavfile.write("phase_test_agc.wav", fs, soundsc(agc_d2))
    wavfile.write("phase_vq_test_agc.wav", fs, soundsc(agc_vq_d2)) 
Example 24
Project: irasl2018   Author: jfsantos   File: audio_tools.py    MIT License 4 votes vote down vote up
def run_phase_vq_example():
    def _pre(list_of_data):
        # Temporal window setting is crucial! - 512 seems OK for music, 256
        # fruit perhaps due to samplerates
        n_fft = 256
        step = 32
        f_r = np.vstack([np.abs(stft(dd, n_fft, step=step, real=False,
                                compute_onesided=False))
                         for dd in list_of_data])
        return f_r, n_fft, step

    def preprocess_train(list_of_data, random_state):
        f_r, n_fft, step = _pre(list_of_data)
        clusters = copy.deepcopy(f_r)
        return clusters

    def apply_preprocess(list_of_data, clusters):
        f_r, n_fft, step = _pre(list_of_data)
        f_clust = f_r
        # Nondeterministic ?
        memberships, distances = vq(f_clust, clusters)
        vq_r = clusters[memberships]
        d_k = iterate_invert_spectrogram(vq_r, n_fft, step, verbose=True)
        return d_k

    random_state = np.random.RandomState(1999)

    fs, d = fetch_sample_speech_fruit()
    d1 = d[::9]
    d2 = d[7::8][:5]
    # make sure d1 and d2 aren't the same!
    assert [len(di) for di in d1] != [len(di) for di in d2]

    clusters = preprocess_train(d1, random_state)
    fix_d1 = np.concatenate(d1)
    fix_d2 = np.concatenate(d2)
    vq_d2 = apply_preprocess(d2, clusters)

    wavfile.write("phase_train_no_agc.wav", fs, soundsc(fix_d1))
    wavfile.write("phase_vq_test_no_agc.wav", fs, soundsc(vq_d2))

    agc_d1, freq_d1, energy_d1 = time_attack_agc(fix_d1, fs, .5, 5)
    agc_d2, freq_d2, energy_d2 = time_attack_agc(fix_d2, fs, .5, 5)
    agc_vq_d2, freq_vq_d2, energy_vq_d2 = time_attack_agc(vq_d2, fs, .5, 5)

    """
    import matplotlib.pyplot as plt
    plt.specgram(agc_vq_d2, cmap="gray")
    #plt.title("Fake")
    plt.figure()
    plt.specgram(agc_d2, cmap="gray")
    #plt.title("Real")
    plt.show()
    """

    wavfile.write("phase_train_agc.wav", fs, soundsc(agc_d1))
    wavfile.write("phase_test_agc.wav", fs, soundsc(agc_d2))
    wavfile.write("phase_vq_test_agc.wav", fs, soundsc(agc_vq_d2)) 
Example 25
Project: attributeBasedClustering   Author: eduard-kazakov   File: attribute_based_clustering_dialog.py    GNU General Public License v2.0 4 votes vote down vote up
def kmeansClustering(self, vectorLayer, attributesList, normalize, clusterNumber, iterations, threshold, outputFieldName):
        from scipy.cluster.vq import kmeans, vq
        from numpy import array

        fullObjectsList = []
        features = vectorLayer.getFeatures()

        for feature in features:
            fullObjectsList.append([])
            for attribute in attributesList:
                if feature[attribute[0]]:
                    fullObjectsList[len(fullObjectsList) - 1].append(feature[attribute[0]])
                else:
                    fullObjectsList[len(fullObjectsList) - 1].append(0)

        # NORMALIZING
        if normalize:
            i = 0
            maxValues = []
            while i < len(attributesList):
                maxValues.append(max(abs(item[i]) for item in fullObjectsList))
                i += 1

            j = 0
            while j < len(fullObjectsList):
                i = 0
                while i < len(fullObjectsList[j]):
                    fullObjectsList[j][i] = (fullObjectsList[j][i] * 1.0) / (maxValues[i] * 1.0)
                    i += 1
                j += 1

        data = array(fullObjectsList)

        centroids, _ = kmeans(data, clusterNumber, iter=iterations, thresh=threshold)
        idx, _ = vq(data, centroids)
        idx = idx.tolist()

        vectorLayerDataProvider = vectorLayer.dataProvider()

        # Create field of not exist
        if vectorLayer.fields().indexFromName(outputFieldName) == -1:
            vectorLayerDataProvider.addAttributes([QgsField(outputFieldName, QVariant.Int)])

        vectorLayer.updateFields()
        vectorLayer.startEditing()
        attrIdx = vectorLayer.fields().indexFromName(outputFieldName)
        features = vectorLayer.getFeatures()

        i = 0
        for feature in features:
            vectorLayer.changeAttributeValue(feature.id(), attrIdx, int(idx[i]))
            i += 1

        vectorLayer.updateFields()
        vectorLayer.commitChanges()

        # Performs K-Means clustering. Updates vector layer field. 
Example 26
Project: crikey   Author: kastnerkyle   File: kmedians.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def minibatch_kmedians(X, M=None, n_components=10, n_iter=100,
                       minibatch_size=100, random_state=None):
    n_clusters = n_components
    if M is not None:
        assert M.shape[0] == n_components
        assert M.shape[1] == X.shape[1]
    if random_state is None:
        random_state = np.random.RandomState(random_state)
    elif not hasattr(random_state, 'shuffle'):
        # Assume integer passed
        random_state = np.random.RandomState(int(random_state))
    if M is None:
        ind = np.arange(len(X)).astype('int32')
        random_state.shuffle(ind)
        M = X[ind[:n_clusters]]

    center_counts = np.zeros(n_clusters)
    pts = list(np.arange(len(X), minibatch_size)) + [len(X)]
    if len(pts) == 1:
        # minibatch size > dataset size case
        pts = [0, None]
    minibatch_indices = zip(pts[:-1], pts[1:])
    for i in range(n_iter):
        for mb_s, mb_e in minibatch_indices:
            Xi = X[mb_s:mb_e]
            # Broadcasted Manhattan distance
            # Could be made faster with einsum perhaps
            centers = np.abs(Xi[:, None, :] - M[None]).sum(
                axis=-1).argmin(axis=1)

            def count_update(c):
                center_counts[c] += 1
            [count_update(c) for c in centers]
            scaled_lr = 1. / center_counts[centers]
            Mi = M[centers]
            scaled_lr = scaled_lr[:, None]
            # Gradient of abs
            Mi = Mi - scaled_lr * ((Xi - Mi) / np.sqrt((Xi - Mi) ** 2 + 1E-9))
            M[centers] = Mi
    # Reassign centers to nearest datapoint
    mem, _ = vq(M, X)
    M = X[mem]
    return M 
Example 27
Project: dagbldr   Author: dagbldr   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def run_phase_vq_example():
    def _pre(list_of_data):
        # Temporal window setting is crucial! - 512 seems OK for music, 256
        # fruit perhaps due to samplerates
        n_fft = 256
        step = 32
        f_r = np.vstack([np.abs(stft(dd, n_fft, step=step, real=False,
                                compute_onesided=False))
                         for dd in list_of_data])
        return f_r, n_fft, step

    def preprocess_train(list_of_data, random_state):
        f_r, n_fft, step = _pre(list_of_data)
        clusters = copy.deepcopy(f_r)
        return clusters

    def apply_preprocess(list_of_data, clusters):
        f_r, n_fft, step = _pre(list_of_data)
        f_clust = f_r
        # Nondeterministic ?
        memberships, distances = vq(f_clust, clusters)
        vq_r = clusters[memberships]
        d_k = iterate_invert_spectrogram(vq_r, n_fft, step, verbose=True)
        return d_k

    random_state = np.random.RandomState(1999)

    fs, d = fetch_sample_speech_fruit()
    d1 = d[::9]
    d2 = d[7::8][:5]
    # make sure d1 and d2 aren't the same!
    assert [len(di) for di in d1] != [len(di) for di in d2]

    clusters = preprocess_train(d1, random_state)
    fix_d1 = np.concatenate(d1)
    fix_d2 = np.concatenate(d2)
    vq_d2 = apply_preprocess(d2, clusters)

    wavfile.write("phase_train_no_agc.wav", fs, soundsc(fix_d1))
    wavfile.write("phase_vq_test_no_agc.wav", fs, soundsc(vq_d2))

    agc_d1, freq_d1, energy_d1 = time_attack_agc(fix_d1, fs, .5, 5)
    agc_d2, freq_d2, energy_d2 = time_attack_agc(fix_d2, fs, .5, 5)
    agc_vq_d2, freq_vq_d2, energy_vq_d2 = time_attack_agc(vq_d2, fs, .5, 5)

    """
    import matplotlib.pyplot as plt
    plt.specgram(agc_vq_d2, cmap="gray")
    #plt.title("Fake")
    plt.figure()
    plt.specgram(agc_d2, cmap="gray")
    #plt.title("Real")
    plt.show()
    """

    wavfile.write("phase_train_agc.wav", fs, soundsc(agc_d1))
    wavfile.write("phase_test_agc.wav", fs, soundsc(agc_d2))
    wavfile.write("phase_vq_test_agc.wav", fs, soundsc(agc_vq_d2)) 
Example 28
Project: tfbldr   Author: kastnerkyle   File: audio_tools.py    BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def run_phase_vq_example():
    def _pre(list_of_data):
        # Temporal window setting is crucial! - 512 seems OK for music, 256
        # fruit perhaps due to samplerates
        n_fft = 256
        step = 32
        f_r = np.vstack([np.abs(stft(dd, fftsize=n_fft, step=step, real=False,
                                compute_onesided=False))
                         for dd in list_of_data])
        return f_r, n_fft, step

    def preprocess_train(list_of_data, random_state):
        f_r, n_fft, step = _pre(list_of_data)
        clusters = copy.deepcopy(f_r)
        return clusters

    def apply_preprocess(list_of_data, clusters):
        f_r, n_fft, step = _pre(list_of_data)
        f_clust = f_r
        # Nondeterministic ?
        memberships, distances = vq(f_clust, clusters)
        vq_r = clusters[memberships]
        d_k = iterate_invert_spectrogram(vq_r, n_fft, step, verbose=True)
        return d_k

    random_state = np.random.RandomState(1999)

    fs, d = fetch_sample_speech_fruit()
    d1 = d[::9]
    d2 = d[7::8][:5]
    # make sure d1 and d2 aren't the same!
    assert [len(di) for di in d1] != [len(di) for di in d2]

    clusters = preprocess_train(d1, random_state)
    fix_d1 = np.concatenate(d1)
    fix_d2 = np.concatenate(d2)
    vq_d2 = apply_preprocess(d2, clusters)

    wavfile.write("phase_train_no_agc.wav", fs, soundsc(fix_d1))
    wavfile.write("phase_vq_test_no_agc.wav", fs, soundsc(vq_d2))

    agc_d1, freq_d1, energy_d1 = time_attack_agc(fix_d1, fs, .5, 5)
    agc_d2, freq_d2, energy_d2 = time_attack_agc(fix_d2, fs, .5, 5)
    agc_vq_d2, freq_vq_d2, energy_vq_d2 = time_attack_agc(vq_d2, fs, .5, 5)

    """
    import matplotlib.pyplot as plt
    plt.specgram(agc_vq_d2, cmap="gray")
    #plt.title("Fake")
    plt.figure()
    plt.specgram(agc_d2, cmap="gray")
    #plt.title("Real")
    plt.show()
    """

    wavfile.write("phase_train_agc.wav", fs, soundsc(agc_d1))
    wavfile.write("phase_test_agc.wav", fs, soundsc(agc_d2))
    wavfile.write("phase_vq_test_agc.wav", fs, soundsc(agc_vq_d2))