Ejemplo n.º 1
0
def plot_mfcc_pca(recording, speaker):
    """ Performs a Principal Components decomposition on the set of Mel-Frequency Cepstral Coefficients extracted
    from the selected speaker's vocalizations.
    :param recording: A Recording object to analyze
    :param speaker: The LENA speaker category to analyze
    """
    index, _, _ = recording.filter_speaker(speaker)
    starts, ends, mfccs = speaker_mfccs(recording, speaker)
    pca = PCA(n_components=2)
    x = pca.fit_transform(mfccs)
    fig, ax = pyplot.subplots()
    comp1_label = 'Comp 1 ({0:.2f}% Variance)'.format(100 * pca.explained_variance_ratio_[0])
    comp2_label = 'Comp 2 ({0:.2f}% Variance)'.format(100 * pca.explained_variance_ratio_[1])

    def fmt(i, x, y):
        import subprocess
        command = 'start {root}/{subject_id}/{speaker}/{voc}.wav'.format(
            root=recording.root, subject_id=recording.recording_id, speaker=speaker, voc=i)
        print(command)
        subprocess.call(command, shell=True)
        return 'Voc: {i:.0f}\nx: {x:0.2f}\ny: {y:0.2f}'.format(i=i, x=x, y=y)

    cursor = FollowDotCursor(ax, index, x[:,0], x[:,1], formatter=fmt, tolerance=20)
    pyplot.scatter(x[:,0], x[:,1], c=starts, cmap=pyplot.cm.get_cmap('hot'))
    pyplot.xlabel(comp1_label)
    pyplot.ylabel(comp2_label)
    pyplot.title('Principal Components of {0} MFCCs'.format(len(index)))
Ejemplo n.º 2
0
                boxstyle='round,pad=0.5', fc='yellow', alpha=0.75),
            arrowprops = dict(
                arrowstyle='->', connectionstyle='arc3,rad=0'))
        return annotation

    def snap(self, x, y):
        """Return the value in self.tree closest to x, y."""
        dist, idx = self.tree.query(self.scaled((x, y)), k=1, p=1)
        try:
            return self._index[idx], self._points[idx][0], self._points[idx][1]
        except IndexError:
            # IndexError: index out of bounds
            return self._points[0]


if __name__ == "__main__":
    recording = rec.Recording('E:/ivfcr', rec.Recording.ids[0])
    index, _, _ = recording.filter_speaker('CHN')
    starts, ends, mfccs = speaker_mfccs(recording, 'CHN')
    ica = FastICA(n_components=2)
    x = ica.fit_transform(mfccs)
    fig, ax = pyplot.subplots()
    comp1_label = 'Component 1'
    comp2_label = 'Component 2'
    cursor = FollowDotCursor(ax, index, x[:,0], x[:,1], formatter=fmt, tolerance=20)
    pyplot.plot(x[:100,0], x[:100,1])
    pyplot.xlabel(comp1_label)
    pyplot.ylabel(comp2_label)

    pyplot.title('Fast ICA of {0} MFCCs'.format(len(index)))