示例#1
0
    def runTest(self):
        print "------------------ Test3  Populate from MP coeffs ---------"
        fileIndex = 2
        RandomAudioFilePath = file_names[fileIndex]
        print 'Working on %s' % RandomAudioFilePath
        sizes = [2**j for j in range(7, 15)]
        segDuration = 5
        nbAtom = 20

        pySig = signals.Signal(op.join(audio_files_path, RandomAudioFilePath),
                               mono=True,
                               normalize=True)

        segmentLength = ((segDuration * pySig.fs) / sizes[-1]) * sizes[-1]
        nbSeg = floor(pySig.length / segmentLength)
        # cropping
        pySig.crop(0, segmentLength)

        # create dictionary
        pyDico = Dico(sizes)

        approx, decay = mp.mp(pySig, pyDico, 20, nbAtom, pad=True, debug=0)

        ppdb = XMDCTBDB('MPdb.db', load=False)
        #        ppdb.keyformat = None
        ppdb.populate(approx, None, fileIndex)

        nKeys = ppdb.get_stats()['ndata']
        # compare the number of keys in the base to the number of atoms

        print ppdb.get_stats()
        self.assertEqual(nKeys, approx.atom_number)

        # now try to recover the fileIndex knowing one of the atoms
        Key = [
            log(approx.atoms[0].length, 2),
            approx.atoms[0].reduced_frequency * pySig.fs
        ]
        T, fileI = ppdb.get(Key)
        Treal = (float(approx.atoms[0].time_position) / float(pySig.fs))
        print T, Treal
        self.assertEqual(fileI[0], fileIndex)
        Tpy = np.array(T)
        self.assertTrue((np.abs(Tpy - Treal)).min() < 0.1)

        # last check: what does a request for non-existing atom in base return?
        T, fileI = ppdb.get((11, 120.0))
        self.assertEqual(T, [])
        self.assertEqual(fileI, [])

        # now let's just retrieve the atoms from the base and see if they are
        # the same
        histograms = ppdb.retrieve(approx, None, offset=0)
        #        plt.figure()
        #        plt.imshow(histograms[0:10,:])
        #        plt.show()
        del ppdb
示例#2
0
    def runTest(self):
        ''' take the base previously constructed and retrieve the song index based on 10 atoms
        '''
        print "------------------ Test6  recognition ---------"

        nbCandidates = 8
        ppdb = XMDCTBDB('LargeMPdb.db', load=True)

        print 'Large Db of ' + str(ppdb.get_stats()['nkeys']) + ' and ' + str(
            ppdb.get_stats()['ndata'])
        # Now take a song, decompose it and try to retrieve it
        fileIndex = 6
        RandomAudioFilePath = file_names[fileIndex]
        print 'Working on ' + str(RandomAudioFilePath)
        pySig = signals.Signal(op.join(audio_files_path, RandomAudioFilePath),
                               mono=True)

        pyDico = LODico(sizes)
        segDuration = 5
        offsetDuration = 7
        offset = offsetDuration * pySig.fs
        nbAtom = 50
        segmentLength = ((segDuration * pySig.fs) / sizes[-1]) * sizes[-1]
        pySig.crop(offset, offset + segmentLength)

        approx, decay = mp.mp(pySig, pyDico, 40, nbAtom, pad=True)

        #        plt.figure()
        #        approx.plotTF()
        #        plt.show()
        res = map(ppdb.get, map(ppdb.kform, approx.atoms),
                  [(a.time_position - pyDico.get_pad()) / approx.fs
                   for a in approx.atoms])
        #
        #res = map(bdb.get, map(bdb.kform, approx.atoms))

        histogram = np.zeros((600, nbCandidates))

        for i in range(approx.atom_number):
            print res[i]
            histogram[res[i]] += 1

        max1 = np.argmax(histogram[:])
        Offset1 = max1 / nbCandidates
        estFile1 = max1 % nbCandidates
        #        candidates , offsets = ppdb.retrieve(approx);
        #        print approx.atom_number
        histograms = ppdb.retrieve(approx, None, offset=0, nbCandidates=8)
        # print histograms , np.max(histograms) , np.argmax(histograms, axis=0) ,
        # np.argmax(histograms, axis=1)

        #        plt.figure()
        #        plt.imshow(histograms[0:20,:],interpolation='nearest')
        #        plt.show()

        maxI = np.argmax(histograms[:])
        OffsetI = maxI / nbCandidates
        estFileI = maxI % nbCandidates

        print fileIndex, offsetDuration, estFileI, OffsetI, estFile1, Offset1, max1, maxI
        import matplotlib.pyplot as plt
        #        plt.figure(figsize=(12,6))
        #        plt.subplot(121)
        #        plt.imshow(histograms,aspect='auto',interpolation='nearest')
        #        plt.subplot(122)
        #        plt.imshow(histogram,aspect='auto',interpolation='nearest')
        ##        plt.imshow(histograms,aspect='auto',interpolation='nearest')
        ##        plt.colorbar()
        #        plt.show()

        print maxI, OffsetI, estFileI
        self.assertEqual(histograms[OffsetI, estFileI], np.max(histograms))

        self.assertEqual(fileIndex, estFileI)
        self.assertTrue(abs(offsetDuration - OffsetI) <= 2.5)
示例#3
0
    def runTest(self):
        ppdb = XMDCTBDB('tempdb.db',
                        load=False,
                        persistent=True,
                        time_max=500.0)

        pySig = signals.LongSignal(op.join(audio_files_path, file_names[0]),
                                   frame_duration=5,
                                   mono=False,
                                   Noverlap=0)

        self.assertEqual(pySig.segment_size, 5.0 * pySig.fs)

        max_nb_seg = 10
        nb_atoms = 150

        scales = SpreadDico([8192], penalty=0.1, mask_time=2, mask_freq=20)

        #        scales = Dico([8192])
        for segIdx in range(min(max_nb_seg, pySig.n_seg)):
            pySigLocal = pySig.get_sub_signal(segIdx,
                                              1,
                                              mono=True,
                                              normalize=False,
                                              channel=0,
                                              pad=scales.get_pad())
            print "MP on segment %d" % segIdx
            # run the decomposition
            approx, decay = mp.mp(pySigLocal, scales, 2, nb_atoms, pad=False)

            print "Populating database with offset " + str(
                segIdx * pySig.segment_size / pySig.fs)
            ppdb.populate(approx,
                          None,
                          0,
                          offset=float((segIdx * pySig.segment_size) -
                                       scales.get_pad()) / float(pySig.fs))

        # ok we have a DB with only 1 file and different segments, now
        nb_test_seg = 15
        long_sig_test = signals.LongSignal(op.join(audio_files_path,
                                                   file_names[0]),
                                           frame_duration=5,
                                           mono=False,
                                           Noverlap=0.5)
        count = 0
        for segIdx in range(min(nb_test_seg, long_sig_test.n_seg)):
            pySigLocal = long_sig_test.get_sub_signal(segIdx,
                                                      1,
                                                      mono=True,
                                                      normalize=False,
                                                      channel=0,
                                                      pad=scales.get_pad())
            #            print "MP on segment %d" % segIdx
            # run the decomposition
            approx, decay = mp.mp(pySigLocal, scales, 2, nb_atoms, pad=False)
            print approx.atom_number

            histograms = ppdb.retrieve(approx, None, nbCandidates=1)
            maxI = np.argmax(histograms[:])
            OffsetI = maxI / 1
            estFileI = maxI % 1

            oracle_value = segIdx * long_sig_test.segment_size * (
                1 - long_sig_test.overlap) / long_sig_test.fs
            print "Seg %d Oracle: %1.1f - found %1.1f" % (segIdx, oracle_value,
                                                          OffsetI)
            if abs(OffsetI - oracle_value) < 5:
                count += 1

        glob = float(count) / float(min(nb_test_seg, long_sig_test.n_seg))
        print "Global Score of %1.3f" % glob
        self.assertGreater(glob, 0.8)