Ejemplo n.º 1
0
def clustering(args):
    ''' run clustering on a single k'''
    print "Feature Analysis/Clustering Mode: single k"

    feature_holder = featurevector.feature_holder(filename=FEATURE_VECTOR_FILENAME)
    sones_holder = featurevector.feature_holder(filename=SONE_VECTOR_FILENAME)
    k = args.k

    print feature_holder
    mfccs = feature_holder.get_feature('mfcc')

    print sones_holder
    sones = sones_holder.get_feature('sones')

    centroids, distortion = Get_Best_Centroids(k, 1)
    print "Distortion for this run: %0.3f" % (distortion)

    classes,dist = kmeans.scipy_vq(mfccs, centroids)

    # Get the inter class dist matrix
    inter_class_dist_matrix = mir_utils.GetSquareDistanceMatrix(centroids)

    eventBeginnings = feature_holder.get_event_start_indecies()
    # write audio if given -w
    if args.plot_segments:
        PlotWaveformWClasses(k, feature_holder,classes)
    if args.write_audio_results:
        WriteAudioFromClasses(k, feature_holder, classes)

    plot.plot(mfccs, sones, eventBeginnings, centroids, inter_class_dist_matrix, classes)
Ejemplo n.º 2
0
def clustering(args):
    ''' run clustering on a single k'''
    print "Feature Analysis/Clustering Mode: single k"

    feature_holder = featurevector.feature_holder(
        filename=FEATURE_VECTOR_FILENAME)
    sones_holder = featurevector.feature_holder(filename=SONE_VECTOR_FILENAME)
    k = args.k

    print feature_holder
    mfccs = feature_holder.get_feature('mfcc')

    print sones_holder
    sones = sones_holder.get_feature('sones')

    centroids, distortion = Get_Best_Centroids(k, 1)
    print "Distortion for this run: %0.3f" % (distortion)

    classes, dist = kmeans.scipy_vq(mfccs, centroids)

    # Get the inter class dist matrix
    inter_class_dist_matrix = mir_utils.GetSquareDistanceMatrix(centroids)

    eventBeginnings = feature_holder.get_event_start_indecies()
    # write audio if given -w
    if args.plot_segments:
        PlotWaveformWClasses(k, feature_holder, classes)
    if args.write_audio_results:
        WriteAudioFromClasses(k, feature_holder, classes)

    plot.plot(mfccs, sones, eventBeginnings, centroids,
              inter_class_dist_matrix, classes)
Ejemplo n.º 3
0
def Get_Best_Centroids(k, iterations):
    print "Feature Analysis/Clustering Mode - feature selection from multiple k's"

    feature_holder = featurevector.feature_holder(
        filename=FEATURE_VECTOR_FILENAME)

    mfccs = feature_holder.get_feature('mfcc')

    j_measures = np.zeros(iterations)
    max = 0
    bestCentroids = 0
    bestDistortion = 0

    for i in range(iterations):
        centroids, distortion = kmeans.scipy_kmeans(mfccs, k)

        classes, dist = kmeans.scipy_vq(mfccs, centroids)

        j_measures[i] = calcJ(mfccs, classes, centroids, k)

        if j_measures[i] > max:
            max = j_measures[i]
            bestCentroids = centroids
            bestDistortion = distortion

    return bestCentroids, bestDistortion
Ejemplo n.º 4
0
def feature_selection(args):
    ''' run clustering on a range of k's'''
    print "Feature Analysis/Clustering Mode - feature selection from multiple k's"

    feature_holder = featurevector.feature_holder(
        filename=FEATURE_VECTOR_FILENAME)
    kMin = args.k_min
    kMax = args.k_max
    kHop = args.k_hop

    mfccs = feature_holder.get_feature('mfcc')
    nmfcc = len(mfccs)
    print "N MFCCS:", nmfcc

    results = []
    for k in range(kMin, kMax, kHop):
        print "Running k-Means with k=%d" % (k)

        if k >= nmfcc:
            print "WARNING! k is greater than the number of samples!"

        centroids, distortion = kmeans.scipy_kmeans(mfccs, k)

        classes, dist = kmeans.scipy_vq(mfccs, centroids)

        J0 = calcJ(mfccs, classes, centroids, k)
        results.append((k, distortion, dist, J0))

    plot.plot_feature_selection(kMin, kMax, kHop, results)
Ejemplo n.º 5
0
    def setUp(self):
        self.nMFCC = 20
        self.vector_template = [('sones', 0, 1),
                           ('mfcc', 1, self.nMFCC)]

        self.fvh = fv.feature_holder(self.vector_template)
        self.check_nfeats = sum([z for x,y,z in self.vector_template])
Ejemplo n.º 6
0
def Get_Best_Centroids(k, iterations):

    feature_holder = featurevector.feature_holder(
        filename=FEATURE_VECTOR_FILENAME)

    mfccs = feature_holder.get_feature('mfcc')

    j_measures = np.zeros(iterations)
    max = 0
    bestCentroids = 0
    bestDistortion = 0

    for i in range(iterations):
        centroids, distortion = kmeans.scipy_kmeans(mfccs, k)

        classes, dist = kmeans.scipy_vq(mfccs, centroids)

        j_measures[i] = calcJ(mfccs, classes, centroids, k)

        if j_measures[i] > max:
            max = j_measures[i]
            bestCentroids = centroids
            bestDistortion = distortion
    '''
    plt.close()
    fig, (ax1) = plt.subplots(1)
    ax1.plot(j_measures)
    ax1.set_title("J measures over multiple iterations of k")
    ax1.set_xlabel("iterations")
    ax1.set_ylabel("J-measure values")
    plt.show()
    '''
    return bestCentroids, bestDistortion
Ejemplo n.º 7
0
def Get_Best_Centroids(k, iterations):

    feature_holder = featurevector.feature_holder(filename=FEATURE_VECTOR_FILENAME)
    
    mfccs = feature_holder.get_feature('mfcc')

    j_measures = np.zeros(iterations)
    max = 0;
    bestCentroids = 0
    bestDistortion = 0
    
    for i in range(iterations):
        centroids, distortion = kmeans.scipy_kmeans(mfccs, k)

        classes, dist = kmeans.scipy_vq(mfccs, centroids)

        j_measures[i] = calcJ(mfccs, classes, centroids, k)

        if j_measures[i] > max:
            max = j_measures[i]
            bestCentroids = centroids
            bestDistortion = distortion

    '''
    plt.close()
    fig, (ax1) = plt.subplots(1)
    ax1.plot(j_measures)
    ax1.set_title("J measures over multiple iterations of k")
    ax1.set_xlabel("iterations")
    ax1.set_ylabel("J-measure values")
    plt.show()
    '''
    return bestCentroids, bestDistortion
Ejemplo n.º 8
0
def feature_selection(args):
    ''' run clustering on a range of k's'''
    print "Feature Analysis/Clustering Mode - feature selection from multiple k's"

    feature_holder = featurevector.feature_holder(filename=FEATURE_VECTOR_FILENAME)
    kMin = args.k_min
    kMax = args.k_max
    kHop = args.k_hop

    mfccs = feature_holder.get_feature('mfcc')
    nmfcc = len(mfccs)
    print "N MFCCS:", nmfcc

    results = []
    for k in range(kMin, kMax, kHop):
        print "Running k-Means with k=%d" % (k)

        if k >= nmfcc:
            print "WARNING! k is greater than the number of samples!"
        centroids, distortion = Get_Best_Centroids(k,20)

        classes, dist = kmeans.scipy_vq(mfccs, centroids)

        J0 = calcJ(mfccs, classes, centroids, k)
        results.append( (k, distortion, dist, J0) )

    plot.plot_feature_selection(kMin, kMax, kHop, results)
Ejemplo n.º 9
0
def Get_Best_Centroids(k, iterations):
    print "Feature Analysis/Clustering Mode - feature selection from multiple k's"

    feature_holder = featurevector.feature_holder(filename=FEATURE_VECTOR_FILENAME)
    
    mfccs = feature_holder.get_feature('mfcc')

    j_measures = np.zeros(iterations)
    max = 0;
    bestCentroids = 0
    bestDistortion = 0
    
    for i in range(iterations):
        centroids, distortion = kmeans.scipy_kmeans(mfccs, k)

        classes, dist = kmeans.scipy_vq(mfccs, centroids)

        j_measures[i] = calcJ(mfccs, classes, centroids, k)

        if j_measures[i] > max:
            max = j_measures[i]
            bestCentroids = centroids
            bestDistortion = distortion

    return bestCentroids, bestDistortion
Ejemplo n.º 10
0
    def test_loadsave(self):
        # Starts cleared
        self.assertEquals(self.fvh.get_nvectors(), 0)

        # Add a new vector at 3
        newVect = np.random.rand(self.check_nfeats)
        self.fvh.add_vector(newVect, 3)
        self.assertEquals(self.fvh.get_nvectors(), 4)
        self.assertTrue((self.fvh.vector[3] == newVect).all())

        feature = 'mfcc'
        label = (10, 15)
        newFeats = np.random.randn(15, self.nMFCC)
        self.fvh.add_feature(feature, newFeats, timelabel=label)
        self.assertEquals(self.fvh.get_nvectors(), 19)
        storedFeatures1 = self.fvh.get_feature(feature)
        self.assertTrue( (self.fvh.vector[:, 1:] == storedFeatures1).all() )
        storedFeatures2 = self.fvh.get_feature_by_label(label, feature)
        self.assertTrue( (newFeats == storedFeatures2).all() )
        

        file = 'features_test.npy'
        self.fvh.save(file)
        self.assertTrue(os.path.exists(file))

        fvh2 = fv.feature_holder(self.vector_template)
        fvh2.load(file)
        self.assertEquals(self.fvh.vector_name_map, fvh2.vector_name_map)
        self.assertEquals(self.fvh.vector_index_map, fvh2.vector_index_map)
        self.assertEquals(self.fvh.vector_length, fvh2.vector_length)
        self.assertTrue( (self.fvh.vector == fvh2.vector).all() )

        fvh3 = fv.feature_holder(filename=file)
        self.assertEquals(self.fvh.vector_name_map, fvh3.vector_name_map)
        self.assertEquals(self.fvh.vector_index_map, fvh3.vector_index_map)
        self.assertEquals(self.fvh.vector_length, fvh3.vector_length)
        self.assertTrue( (self.fvh.vector == fvh3.vector).all() )

        self.assertEquals(self.fvh.get_nvectors(), 19)
        storedFeatures1 = self.fvh.get_feature(feature)
        self.assertTrue( (self.fvh.vector[:, 1:] == storedFeatures1).all() )
        storedFeatures2 = self.fvh.get_feature_by_label(label, feature)
        self.assertTrue( (newFeats == storedFeatures2).all() )
Ejemplo n.º 11
0
    def test_loadsave(self):
        # Starts cleared
        self.assertEquals(self.fvh.get_nvectors(), 0)

        # Add a new vector at 3
        newVect = np.random.rand(self.check_nfeats)
        self.fvh.add_vector(newVect, 3)
        self.assertEquals(self.fvh.get_nvectors(), 4)
        self.assertTrue((self.fvh.vector[3] == newVect).all())

        feature = 'mfcc'
        label = (10, 15)
        newFeats = np.random.randn(15, self.nMFCC)
        self.fvh.add_feature(feature, newFeats, timelabel=label)
        self.assertEquals(self.fvh.get_nvectors(), 19)
        storedFeatures1 = self.fvh.get_feature(feature)
        self.assertTrue((self.fvh.vector[:, 1:] == storedFeatures1).all())
        storedFeatures2 = self.fvh.get_feature_by_label(label, feature)
        self.assertTrue((newFeats == storedFeatures2).all())

        file = 'features_test.npy'
        self.fvh.save(file)
        self.assertTrue(os.path.exists(file))

        fvh2 = fv.feature_holder(self.vector_template)
        fvh2.load(file)
        self.assertEquals(self.fvh.vector_name_map, fvh2.vector_name_map)
        self.assertEquals(self.fvh.vector_index_map, fvh2.vector_index_map)
        self.assertEquals(self.fvh.vector_length, fvh2.vector_length)
        self.assertTrue((self.fvh.vector == fvh2.vector).all())

        fvh3 = fv.feature_holder(filename=file)
        self.assertEquals(self.fvh.vector_name_map, fvh3.vector_name_map)
        self.assertEquals(self.fvh.vector_index_map, fvh3.vector_index_map)
        self.assertEquals(self.fvh.vector_length, fvh3.vector_length)
        self.assertTrue((self.fvh.vector == fvh3.vector).all())

        self.assertEquals(self.fvh.get_nvectors(), 19)
        storedFeatures1 = self.fvh.get_feature(feature)
        self.assertTrue((self.fvh.vector[:, 1:] == storedFeatures1).all())
        storedFeatures2 = self.fvh.get_feature_by_label(label, feature)
        self.assertTrue((newFeats == storedFeatures2).all())
Ejemplo n.º 12
0
def getfeatures(args):
    ''' write the extracted features from the input audio file into numpy files for reading in the other steps. '''
    debug = args.debug
    filepath = args.audiofile
    chunk_len = args.audio_seg_length

    afm = af.audiofile_manager(filepath, chunk_len)

    # FFT Parameters
    fs = afm.afReader.samplerate()
    N = 2048
    hopDenom = 2
    hopSize = N / hopDenom
    zp = 0
    winfunc = np.hamming
    fftParams = fftparams.FFTParams(fs, N, hopDenom, zp, winfunc)

    # MFCC Paramters
    nFilters = 40
    nDCTCoefs = 20
    minFreq = 50
    maxFreq = 8000
    nIndexSkip = 2
    seglen = 1
    mfccParams = fftparams.MFCCParams(nFilters, nDCTCoefs, minFreq, maxFreq,
                                      nIndexSkip)

    # Feature Vector parameters
    # Template : ('name', order index, length)
    vector_template = [('sones', 0, 1), ('mfcc', 1, nDCTCoefs - nIndexSkip)]
    sone_template = [('sones', 0, 1)]
    # Initialize the feature vector holder
    feature_holder = featurevector.feature_holder(vector_template, filepath)
    sone_holder = featurevector.feature_holder(sone_template, filepath)
    envelopeHolder = []
    audioHolder = []
    sonesHolder = []
    maxEnvelope = 0
    count = 0

    print "Feature Extraction Mode\n"
    print datetime.now()
    # For each chunk of audio
    while afm.HasMoreData():
        count += 1
        audioChunk, chunkIndex = afm.GetNextSegment()

        if debug:
            print "Read %d sample chunk of audio (%0.2fs)" % (
                len(audioChunk), len(audioChunk) / fs)

        # Get Events
        eventTimes, envelope = GetEvents(audioChunk, fftParams, debug)
        if maxEnvelope < envelope.max():
            maxEnvelope = envelope.max()

        if debug: print "EVENTTIMES:", eventTimes

        envelopeHolder.append(envelope)

        eventTimesSamps = np.asarray(np.multiply(eventTimes, fs), dtype=int)

        # Get event audio segments
        eventSegments = GetEventAudioSegments(eventTimesSamps, audioChunk,
                                              debug)

        #get sones

        eventSegmentSones = GetEventSones(eventSegments, fftParams, debug)

        # Get the MFCCs for each segment / event
        eventSegmentMFCCs = GetEventMFCCs(eventSegments, fftParams, mfccParams,
                                          debug)

        # Time-average for each segment / event
        averagedEventSegmentMFCCs, averagedEventSegmentSones = AverageEventFeatures(
            eventSegmentMFCCs, eventSegmentSones, seglen, fftParams, debug)

        # Store these vectors in the feature_holder, labelled with their time
        StoreFeatureVector(feature_holder, sone_holder,
                           averagedEventSegmentMFCCs,
                           averagedEventSegmentSones, chunkIndex, chunk_len,
                           eventTimes, debug)

    # Write features to disk
    print datetime.now()

    fileSize = feature_holder.save(FEATURE_VECTOR_FILENAME)
    print "Wrote", fileSize, "bytes to disk. (%s)" % (FEATURE_VECTOR_FILENAME)

    fileSize = sone_holder.save(SONE_VECTOR_FILENAME)
    print "Wrote", fileSize, "bytes to disk. (%s)" % (SONE_VECTOR_FILENAME)
Ejemplo n.º 13
0
    def setUp(self):
        self.nMFCC = 20
        self.vector_template = [('sones', 0, 1), ('mfcc', 1, self.nMFCC)]

        self.fvh = fv.feature_holder(self.vector_template)
        self.check_nfeats = sum([z for x, y, z in self.vector_template])
Ejemplo n.º 14
0
def getfeatures(args):
    ''' write the extracted features from the input audio file into numpy files for reading in the other steps. '''
    debug = args.debug
    filepath = args.audiofile
    chunk_len = args.audio_seg_length

    afm = af.audiofile_manager(filepath, chunk_len)

    # FFT Parameters
    fs = afm.afReader.samplerate()
    N = 2048
    hopDenom = 2
    hopSize = N/hopDenom
    zp = 0
    winfunc=np.hamming
    fftParams = fftparams.FFTParams(fs, N, hopDenom, zp, winfunc)

    # MFCC Paramters
    nFilters = 40
    nDCTCoefs = 20
    minFreq = 50
    maxFreq = 8000
    nIndexSkip = 2
    seglen = 1
    mfccParams = fftparams.MFCCParams(nFilters, nDCTCoefs, minFreq, maxFreq, nIndexSkip)

    # Feature Vector parameters
    # Template : ('name', order index, length)
    vector_template = [('sones', 0, 1),
                        ('mfcc', 1, nDCTCoefs - nIndexSkip)]
    sone_template = [('sones', 0, 1)]
    # Initialize the feature vector holder
    feature_holder = featurevector.feature_holder(vector_template, filepath)
    sone_holder = featurevector.feature_holder(sone_template, filepath)
    envelopeHolder = []
    audioHolder = []
    sonesHolder = []
    maxEnvelope = 0;
    count =0
    
    print "Feature Extraction Mode\n"
    print datetime.now()
    # For each chunk of audio
    while afm.HasMoreData():
        count +=1
        audioChunk, chunkIndex = afm.GetNextSegment()
        
        
        if debug: print "Read %d sample chunk of audio (%0.2fs)" % (len(audioChunk), len(audioChunk) / fs)

        # Get Events
        eventTimes, envelope = GetEvents(audioChunk, fftParams, debug)
        if maxEnvelope < envelope.max():
            maxEnvelope = envelope.max()
        
        if debug: print "EVENTTIMES:", eventTimes
                
        envelopeHolder.append(envelope)
        
        eventTimesSamps = np.asarray(np.multiply(eventTimes,fs),dtype=int)

        # Get event audio segments
        eventSegments = GetEventAudioSegments(eventTimesSamps, audioChunk, debug)
        
        #get sones
        
        eventSegmentSones = GetEventSones(eventSegments, fftParams, debug)
        
        # Get the MFCCs for each segment / event
        eventSegmentMFCCs = GetEventMFCCs(eventSegments, fftParams, mfccParams, debug)

        # Time-average for each segment / event
        averagedEventSegmentMFCCs, averagedEventSegmentSones = AverageEventFeatures(eventSegmentMFCCs, eventSegmentSones, seglen, fftParams, debug)

        # Store these vectors in the feature_holder, labelled with their time
        StoreFeatureVector(feature_holder, sone_holder, averagedEventSegmentMFCCs, averagedEventSegmentSones, chunkIndex, chunk_len, eventTimes, debug)
        
    # Write features to disk
    print datetime.now()
    
    fileSize = feature_holder.save(FEATURE_VECTOR_FILENAME)
    print "Wrote", fileSize, "bytes to disk. (%s)" % (FEATURE_VECTOR_FILENAME)

    fileSize = sone_holder.save(SONE_VECTOR_FILENAME)
    print "Wrote", fileSize, "bytes to disk. (%s)" % (SONE_VECTOR_FILENAME)