def realTest(self): pySig = signals.InitFromFile('../../../data/glocs.wav',forceMono=True,doNormalize=True); pySig2 = pySig.copy() # pySig3 = pySig.copy() decalage = 500; pySig.crop(0, 4.5*pySig.samplingFrequency); pySig2.crop(decalage, decalage + 4.5*pySig.samplingFrequency); # pySig3.crop(decalage, decalage + 4.5*pySig.samplingFrequency); pySig.pad(16384) pySig2.pad(16384) # pySig3.pad(16384) dico = [128,1024,8192]; tol = [2]*len(dico); parallelProjections.initialize_plans(np.array(dico),np.array(tol)) classicDIco = mdct_dico.LODico(dico); jointDico = joint_dico.SetDico(dico , selectNature='sum') nbAtoms = 50; print "%%%%%%%%%%%%%%% Testing mp with one signal %%%%%%%%%%%%%%%" print "" approxClassic, decayClassic = mp.mp(pySig, classicDIco, 20, nbAtoms, padSignal=False, debug=0); print "" # print "%%%%%%%%%%%%%%% Testing Joint mp with one signal %%%%%%%%%%%%%%%" # print "" # approxCommon, approxSpecList, decayList,residualSignalList = mp.mp_joint((pySig,), jointDico, 20, nbAtoms, debug=0); # print "" # plt.plot() print "%%%%%%%%%%%%%%% Testing Joint mp with two signal %%%%%%%%%%%%%%%" approxCommon, approxSpecList, decayList, residualSignalList = mp.mp_joint((pySig,pySig2), jointDico, 20, nbAtoms, debug=2); print [abs(atom.getAmplitude()) for atom in approxSpecList[0].atoms]
def runTest(self): ApproxPath = "../Approxs/" # ext = ".png" pyRandomDico = random_dico.RandomDico([256 , 2048 , 8192] , 'scale') pyDico = mdct_dico.Dico([256 , 2048 , 8192]) pySigOriginal = signals.InitFromFile(audioFilePath +"ClocheB.wav" , True , True); pySigOriginal.crop(0 , 1*16384); #intentionally non aligned atom -> objective is to fit it completely through time shift calculation pyAtom = mdct_atom.Atom(2048 , 1, 8192 - 512 , 128 , 8000, 0.57) pyAtom.synthesize() pySignal_oneAtom = signals.Signal(0.0001*random.random(pySigOriginal.length), pySigOriginal.samplingFrequency, False) pySignal_oneAtom.add(pyAtom) fig_1Atom = plt.figure() print "Testing one Aligned Atom with Random" approximant = mp.mp(pySignal_oneAtom, pyRandomDico, 10, 10, False, False)[0] # approximant.plotTF() # plt.subplot(211) plt.plot(pySignal_oneAtom.dataVec) plt.plot(approximant.recomposedSignal.dataVec) plt.plot(pySignal_oneAtom.dataVec - approximant.recomposedSignal.dataVec) plt.legend(("original", "approximant", "resisual")) plt.title("Aligned Atom with LOmp") # plt.show() plt.xlabel("samples") # plt.savefig(figPath +"LOmp_aligned" +ext) approximant.writeToXml(ApproxPath+"LOmp_aligned") print " Approx Reached : " , approximant.computeSRR() , " dB in " , approximant.atomNumber , " iteration" del pyAtom , approximant , pySignal_oneAtom print "Testing one Non-Aligned Atom with mp - should not be perfect" pyAtom = mdct_atom.Atom(2048 , 1, 7500 , 128 , 8000 , 0.57) pyAtom.synthesize() pySignal_oneAtom = signals.Signal(0.0001*random.random(pySigOriginal.length), pySigOriginal.samplingFrequency, False) pySignal_oneAtom.add(pyAtom) approximant , decay = mp.mp(pySignal_oneAtom, pyDico, 10, 10, False, False) # approximant.plotTF() plt.figure() plt.subplot(211) plt.plot(pySignal_oneAtom.dataVec) plt.plot(approximant.recomposedSignal.dataVec) plt.plot(pySignal_oneAtom.dataVec - approximant.recomposedSignal.dataVec) plt.legend(("original", "approximant", "resisual")) plt.title("Non aligned atom with mp : SRR of " + str(int(approximant.computeSRR())) + " dB in " + str(approximant.atomNumber) + " iteration") # plt.show() print " Approx Reached : " , int(approximant.computeSRR()) , " dB in " , approximant.atomNumber , " iteration" del approximant print "Testing one Non-Aligned Atom with Random" pyAtom = mdct_atom.Atom(2048 , 1, 7500 , 128 , 8000 , 0.57) pyAtom.synthesize() pySignal_oneAtom = signals.Signal(0.0001*random.random(pySigOriginal.length), pySigOriginal.samplingFrequency, False) pySignal_oneAtom.add(pyAtom) approximant, decay = mp.mp(pySignal_oneAtom, pyRandomDico, 10, 10, False, False) # approximant.plotTF() plt.subplot(212) plt.plot(pySignal_oneAtom.dataVec) plt.plot(approximant.recomposedSignal.dataVec) plt.plot(pySignal_oneAtom.dataVec - approximant.recomposedSignal.dataVec) plt.legend(("original", "approximant", "resisual")) plt.title("Non aligned atom with LOmp : SRR of " + str(int(approximant.computeSRR())) + " dB in " + str(approximant.atomNumber) + " iteration") plt.xlabel("samples") # plt.savefig(figPath+"nonAlignedAtom_mp_vs_LOmp" + ext) # plt.show() print " Approx Reached : " , approximant.computeSRR() , " dB in " , approximant.atomNumber , " iteration" del approximant print "Testing Real signals with mp" approximant, decay = mp.mp(pySigOriginal, pyDico, 10, 10, False) # approximant.plotTF() plt.figure() plt.subplot(211) plt.plot(pySigOriginal.dataVec[4096:-4096]) plt.plot(approximant.recomposedSignal.dataVec[4096:-4096]) plt.plot(pySigOriginal.dataVec[4096:-4096] - approximant.recomposedSignal.dataVec[4096:-4096]) plt.legend(("original", "approximant", "resisual")) plt.title("Bell signals with mp : SRR of " + str(int(approximant.computeSRR()))+ " dB in " + str(approximant.atomNumber) + " iteration") # plt.show() print " Approx Reached : " , approximant.computeSRR() , " dB in " , approximant.atomNumber , " iteration" del approximant print "Testing Real signals with Randommp" # pyCCDico = approximant = mp.mp(pySigOriginal, pyRandomDico, 10, 10, False, False)[0] # approximant.plotTF() plt.subplot(212) plt.plot(pySigOriginal.dataVec[4096:-4096]) plt.plot(approximant.recomposedSignal.dataVec[4096:-4096]) plt.plot(pySigOriginal.dataVec[4096:-4096] - approximant.recomposedSignal.dataVec[4096:-4096]) plt.legend(("original", "approximant", "resisual")) plt.title("Bell signals with LOmp : SRR of " + str(int(approximant.computeSRR())) + " dB in " + str(approximant.atomNumber) + " iteration") plt.xlabel("samples") # plt.savefig(figPath+"RealSignal_mp_vs_LOmp_nbIt10"+ext) print " Approx Reached : " , approximant.computeSRR() , " dB in " , approximant.atomNumber , " iteration" del approximant print "comparing results and processing times for long decompositions" t0 = time.clock() approx1, decay = mp.mp(pySigOriginal, pyDico, 20, 500, False) t1 = time.clock() plt.figure() plt.subplot(211) plt.plot(pySigOriginal.dataVec[12288:-12288]) plt.plot(approx1.recomposedSignal.dataVec[12288:-12288]) plt.plot(pySigOriginal.dataVec[12288:-12288] - approx1.recomposedSignal.dataVec[12288:-12288]) plt.legend(("original", "approximant", "resisual")) plt.title("Bell signals with mp : SRR of " + str(int(approx1.computeSRR())) + " dB in " + str(approx1.atomNumber) + " iteration and " + str(t1-t0) + "s") # plt.show() print " Approx Reached : " , approx1.computeSRR() , " dB in " , approx1.atomNumber , " iteration and: " , t1-t0 , " seconds" t2 = time.clock() approx2 = mp.mp(pySigOriginal, pyRandomDico, 20, 500, False, False)[0] t3 = time.clock() plt.subplot(212) plt.plot(pySigOriginal.dataVec[12288:-12288]) plt.plot(approx2.recomposedSignal.dataVec[12288:-12288]) plt.plot(pySigOriginal.dataVec[12288:-12288] - approx2.recomposedSignal.dataVec[12288:-12288]) plt.legend(("original", "approximant", "resisual")) plt.title("Bell signals with LOmp : SRR of " + str(int(approx2.computeSRR())) + " dB in " + str(approx2.atomNumber) + " iteration and " + str(t3-t2) + "s") # plt.savefig(figPath+"RealSignal_mp_vs_LOmp_SRR20"+ext) print " Approx Reached : " , approx2.computeSRR() , " dB in " , approx2.atomNumber , " iteration and: " , t3-t2 , " seconds" del approx1 , approx2 del pySigOriginal print "comparing results and processing times for long decompositions of white gaussian noise" pyNoiseSignal = signals.Signal(0.5*random.random(5*16384), 44100, False) pyNoiseSignal.pad(16384) t0 = time.clock() approx1, decay = mp.mp(pyNoiseSignal, pyDico, 10, 500, False, True) t1 = time.clock() plt.figure() plt.subplot(211) plt.plot(pyNoiseSignal.dataVec[16384:-16384]) plt.plot(approx1.recomposedSignal.dataVec[16384:-16384]) plt.plot(pyNoiseSignal.dataVec[16384:-16384] - approx1.recomposedSignal.dataVec[16384:-16384]) plt.legend(("original", "approximant", "resisual")) plt.title("White Noise signals with mp : SRR of " + str(int(approx1.computeSRR())) + " dB in " + str(approx1.atomNumber) + " iteration and " + str(t1-t0) + "s") # plt.show() print " Approx Reached : " , approx1.computeSRR() , " dB in " , approx1.atomNumber , " iteration and: " , t1-t0 , " seconds" t2 = time.clock() approx2 = mp.mp(pyNoiseSignal, pyRandomDico, 10, 500, False, True)[0] t3 = time.clock() plt.subplot(212) plt.plot(pyNoiseSignal.dataVec[16384:-16384]) plt.plot(approx2.recomposedSignal.dataVec[16384:-16384]) plt.plot(pyNoiseSignal.dataVec[16384:-16384] - approx2.recomposedSignal.dataVec[16384:-16384]) plt.legend(("original", "approximant", "resisual")) plt.title("Noise signals with LOmp : SRR of " + str(int(approx2.computeSRR())) + " dB in " + str(approx2.atomNumber) + " iteration and " + str(t3-t2) + "s") plt.xlabel("samples") # plt.savefig(figPath+"NoiseSignal_mp_vs_LOmp_SRR20"+ext) print " Approx Reached : " , approx2.computeSRR() , " dB in " , approx2.atomNumber , " iteration and: " , t3-t2 , " seconds"
def writeXmlTest(self): pySigOriginal = signals.InitFromFile(audioFilePath +"ClocheB.wav" , True , True, debugLevel=0); pySigOriginal.crop(0 , 1*16384); pyDico = mdct_dico.Dico([256 , 2048 , 8192] , debugLevel=2 ) # first compute an approximant using mp approximant = mp.mp(pySigOriginal, pyDico, 10, 10, debug=2)[0] outputXmlPath = "approx_test.xml"; doc = approximant.writeToXml(outputXmlPath) # Test reading from the xml flow newApprox = approx.readFromXml('', doc) self.assertEqual(newApprox.dico.sizes , approximant.dico.sizes) self.assertEqual(newApprox.atomNumber , approximant.atomNumber) self.assertEqual(newApprox.length , approximant.length) # now the hardest test newApprox.synthesize() newApprox.recomposedSignal.plot() self.assertAlmostEquals(sum(approximant.recomposedSignal.dataVec - newApprox.recomposedSignal.dataVec) , 0) # test reading from the xml file del newApprox newApprox = approx.readFromXml(outputXmlPath) self.assertEqual(newApprox.dico.sizes , approximant.dico.sizes) self.assertEqual(newApprox.atomNumber , approximant.atomNumber) self.assertEqual(newApprox.length , approximant.length) # now the hardest test newApprox.synthesize() newApprox.recomposedSignal.plot() self.assertAlmostEquals(sum(approximant.recomposedSignal.dataVec - newApprox.recomposedSignal.dataVec) , 0) mdctOrig = approximant.toArray()[0] mdctRead = newApprox.toArray()[0] del doc , newApprox # test writing with LOmp atoms pyCCDico = mdct_dico.LODico([256 , 2048 , 8192] ) approx_LOmp = mp.mp(pySigOriginal, pyCCDico, 10, 10, False)[0] outputXmlPath = "approxLOmp_test.xml"; doc = approx_LOmp.writeToXml(outputXmlPath) # Test reading from the xml flow newApprox = approx.readFromXml('', doc) self.assertEqual(newApprox.dico.sizes , approx_LOmp.dico.sizes) self.assertEqual(newApprox.atomNumber , approx_LOmp.atomNumber) self.assertEqual(newApprox.length , approx_LOmp.length) self.assertAlmostEqual(sum(newApprox.toArray()[0] - approx_LOmp.toArray()[0]), 0) plt.figure() plt.plot(newApprox.toArray()[0]) plt.plot(approx_LOmp.toArray()[0] , 'r:') # plt.show() # test reading from the xml file del newApprox newApprox = approx.readFromXml(outputXmlPath) self.assertEqual(newApprox.dico.sizes , approx_LOmp.dico.sizes) self.assertEqual(newApprox.atomNumber , approx_LOmp.atomNumber) self.assertEqual(newApprox.length , approx_LOmp.length) self.assertAlmostEqual(sum(newApprox.toArray()[0] - approx_LOmp.toArray()[0]), 0)