diff --git a/test/src/unittests/tonal/pitchyinfft/vignesh_confidance.npy b/test/src/unittests/tonal/pitchyinfft/vignesh_confidence.npy similarity index 100% rename from test/src/unittests/tonal/pitchyinfft/vignesh_confidance.npy rename to test/src/unittests/tonal/pitchyinfft/vignesh_confidence.npy diff --git a/test/src/unittests/tonal/test_pitchyin.py b/test/src/unittests/tonal/test_pitchyin.py index 59b6ea826..d1d31cd04 100644 --- a/test/src/unittests/tonal/test_pitchyin.py +++ b/test/src/unittests/tonal/test_pitchyin.py @@ -18,10 +18,10 @@ # version 3 along with this program. If not, see http://www.gnu.org/licenses/ - from essentia_test import * from numpy import sin, pi, mean, random + class TestPitchYin(TestCase): def testEmpty(self): @@ -34,65 +34,65 @@ def testZero(self): def testSine(self): sr = 44100 - size = sr*1; + size = sr * 1 freq = 440 - signal = [sin(2.0*pi*freq*i/sr) for i in range(size)] + signal = [sin(2.0 * pi * freq * i / sr) for i in range(size)] self.runTest(signal, sr, freq) def testBandLimitedSquare(self): sr = 44100 - size = sr*1; + size = sr * 1 freq = 660 - w = 2.0*pi*freq + w = 2.0 * pi * freq nharms = 10 signal = zeros(size) for i in range(size): for harm in range(nharms): - signal[i] += .5/(2.*harm+1)*sin((2*harm+1)*i*w/sr) + signal[i] += 0.5 / (2.0 * harm + 1) * sin((2 * harm + 1) * i * w / sr) self.runTest(signal, sr, freq) def testBandLimitedSaw(self): sr = 44100 - size = sr*1; + size = sr * 1 freq = 660 - w = 2.0*pi*freq + w = 2.0 * pi * freq nharms = 10 signal = zeros(size) - for i in range(1,size): - for harm in range(1,nharms+1): - signal[i] += 1./harm*sin(harm*i*w/sr) + for i in range(1, size): + for harm in range(1, nharms + 1): + signal[i] += 1.0 / harm * sin(harm * i * w / sr) self.runTest(signal, sr, freq, 1.1, 0.1) def testBandLimitedSawMasked(self): sr = 44100 - size = sr*1; + size = sr * 1 freq = 440 - w = 2.0*pi*freq - subw = 2.0*pi*(freq-100) + w = 2.0 * pi * freq + subw = 2.0 * pi * (freq - 100) nharms = 10 signal = zeros(size) - for i in range(1,size): + for i in range(1, size): # masking noise: - whitenoise = 2*(random.rand(1)-0.5) - signal[i] += 2*whitenoise - for harm in range(1,nharms): - signal[i] += 1./harm*sin(i*harm*w/sr) - signal = 5*LowPass()(signal) - for i in range(1,size): - for harm in range(1,nharms+1): - signal[i] += .1/harm*sin(i*harm*w/sr) - signal[i] += 0.5*sin(i*subw/sr) + whitenoise = 2 * (random.rand(1) - 0.5) + signal[i] += 2 * whitenoise + for harm in range(1, nharms): + signal[i] += 1.0 / harm * sin(i * harm * w / sr) + signal = 5 * LowPass()(signal) + for i in range(1, size): + for harm in range(1, nharms + 1): + signal[i] += 0.1 / harm * sin(i * harm * w / sr) + signal[i] += 0.5 * sin(i * subw / sr) max_signal = max(signal) + 1 - signal = signal/max_signal + signal = signal / max_signal self.runTest(signal, sr, freq, 1.5, 0.3) - def runTest(self, signal, sr, freq, pitch_precision = 1, conf_precision = 0.1): + def runTest(self, signal, sr, freq, pitch_precision=1, conf_precision=0.1): frameSize = 1024 hopsize = frameSize frames = FrameGenerator(signal, frameSize=frameSize, hopSize=hopsize) - pitchDetect = PitchYin(frameSize=frameSize, sampleRate = sr) + pitchDetect = PitchYin(frameSize=frameSize, sampleRate=sr) pitch = [] confidence = [] for frame in frames: @@ -103,8 +103,8 @@ def runTest(self, signal, sr, freq, pitch_precision = 1, conf_precision = 0.1): self.assertAlmostEqual(mean(confidence), 1, conf_precision) def testInvalidParam(self): - self.assertConfigureFails(PitchYin(), {'frameSize' : 1}) - self.assertConfigureFails(PitchYin(), {'sampleRate' : 0}) + self.assertConfigureFails(PitchYin(), {"frameSize": 1}) + self.assertConfigureFails(PitchYin(), {"sampleRate": 0}) def testARealCase(self): # The expected values were recomputed from commit @@ -119,10 +119,10 @@ def testARealCase(self): frameSize = 1024 sr = 44100 hopSize = 512 - filename = join(testdata.audio_dir, 'recorded', 'vignesh.wav') + filename = join(testdata.audio_dir, "recorded", "vignesh.wav") audio = MonoLoader(filename=filename, sampleRate=44100)() frames = FrameGenerator(audio, frameSize=frameSize, hopSize=hopSize) - pitchDetect = PitchYin(frameSize=frameSize, sampleRate = sr) + pitchDetect = PitchYin(frameSize=frameSize, sampleRate=sr) pitch = [] confidence = [] for frame in frames: @@ -130,8 +130,8 @@ def testARealCase(self): pitch += [f] confidence += [conf] - expected_pitch = numpy.load(join(filedir(), 'pitchyin/vignesh_pitch.npy')) - expected_conf = numpy.load(join(filedir(), 'pitchyin/vignesh_confidance.npy')) + expected_pitch = numpy.load(join(filedir(), "pitchyin/vignesh_pitch.npy")) + expected_conf = numpy.load(join(filedir(), "pitchyin/vignesh_confidence.npy")) self.assertAlmostEqualVector(pitch, expected_pitch) self.assertAlmostEqualVector(confidence, expected_conf, 5e-6) @@ -144,15 +144,16 @@ def testARealCaseVampComparison(self): frameSize = 2048 sr = 44100 hopSize = 256 - filename = join(testdata.audio_dir, 'recorded', 'vignesh.wav') + filename = join(testdata.audio_dir, "recorded", "vignesh.wav") audio = MonoLoader(filename=filename, sampleRate=44100)() frames = FrameGenerator(audio, frameSize=frameSize, hopSize=hopSize) - pitchDetect = PitchYin(frameSize=frameSize, sampleRate=sr, - minFrequency=40, maxFrequency=1600) + pitchDetect = PitchYin( + frameSize=frameSize, sampleRate=sr, minFrequency=40, maxFrequency=1600 + ) pitch = array([pitchDetect(frame)[0] for frame in frames]) - expected_pitch = numpy.load(join(filedir(), 'pitchyin/vignesh_pitch_vamp.npy')) + expected_pitch = numpy.load(join(filedir(), "pitchyin/vignesh_pitch_vamp.npy")) # The VAMP implementation provides voiced/unvoiced information # while our system does not. Thus set to 0 unvoiced frames in @@ -171,5 +172,5 @@ def testARealCaseVampComparison(self): suite = allTests(TestPitchYin) -if __name__ == '__main__': +if __name__ == "__main__": TextTestRunner(verbosity=2).run(suite) diff --git a/test/src/unittests/tonal/test_pitchyinfft.py b/test/src/unittests/tonal/test_pitchyinfft.py index e22ca1eb6..49065bba2 100644 --- a/test/src/unittests/tonal/test_pitchyinfft.py +++ b/test/src/unittests/tonal/test_pitchyinfft.py @@ -18,10 +18,10 @@ # version 3 along with this program. If not, see http://www.gnu.org/licenses/ - from essentia_test import * from numpy import sin, pi, mean, random + class TestPitchYinFFT(TestCase): def testEmpty(self): @@ -32,70 +32,68 @@ def testZero(self): self.assertEqual(pitch, 0) self.assertEqual(confidence, 0) - def testSine(self): sr = 44100 - size = sr*1; + size = sr * 1 freq = 440 - signal = [sin(2.0*pi*freq*i/sr) for i in range(size)] + signal = [sin(2.0 * pi * freq * i / sr) for i in range(size)] self.runTest(signal, sr, freq) def testBandLimitedSquare(self): sr = 44100 - size = sr*1; + size = sr * 1 freq = 660 - w = 2.0*pi*freq + w = 2.0 * pi * freq nharms = 10 signal = zeros(size) for i in range(size): for harm in range(nharms): - signal[i] += .5/(2.*harm+1)*sin((2*harm+1)*i*w/sr) + signal[i] += 0.5 / (2.0 * harm + 1) * sin((2 * harm + 1) * i * w / sr) self.runTest(signal, sr, freq) def testBandLimitedSaw(self): sr = 44100 - size = sr*1; + size = sr * 1 freq = 660 - w = 2.0*pi*freq + w = 2.0 * pi * freq nharms = 10 signal = zeros(size) - for i in range(1,size): - for harm in range(1,nharms+1): - signal[i] += 1./harm*sin(harm*i*w/sr) + for i in range(1, size): + for harm in range(1, nharms + 1): + signal[i] += 1.0 / harm * sin(harm * i * w / sr) self.runTest(signal, sr, freq, 1.1, 0.1) def testBandLimitedSawMasked(self): sr = 44100 - size = sr*1; + size = sr * 1 freq = 440 - w = 2.0*pi*freq - subw = 2.0*pi*(freq-100) + w = 2.0 * pi * freq + subw = 2.0 * pi * (freq - 100) nharms = 10 signal = zeros(size) - for i in range(1,size): + for i in range(1, size): # masking noise: - whitenoise = 2*(random.rand(1)-0.5) - signal[i] += 2*whitenoise - for harm in range(1,nharms): - signal[i] += 1./harm*sin(i*harm*w/sr) - signal = 5*LowPass()(signal) - for i in range(1,size): - for harm in range(1,nharms+1): - signal[i] += .1/harm*sin(i*harm*w/sr) - signal[i] += 0.5*sin(i*subw/sr) + whitenoise = 2 * (random.rand(1) - 0.5) + signal[i] += 2 * whitenoise + for harm in range(1, nharms): + signal[i] += 1.0 / harm * sin(i * harm * w / sr) + signal = 5 * LowPass()(signal) + for i in range(1, size): + for harm in range(1, nharms + 1): + signal[i] += 0.1 / harm * sin(i * harm * w / sr) + signal[i] += 0.5 * sin(i * subw / sr) max_signal = max(signal) + 1 - signal = signal/max_signal + signal = signal / max_signal self.runTest(signal, sr, freq, 1.5, 0.3) - - def runTest(self, signal, sr, freq, pitch_precision = 1, conf_precision = 0.1): + def runTest(self, signal, sr, freq, pitch_precision=1, conf_precision=0.1): frameSize = 1024 hopsize = frameSize frames = FrameGenerator(signal, frameSize=frameSize, hopSize=hopsize) - win = Windowing(type='hann') - pitchDetect = PitchYinFFT(frameSize=frameSize, sampleRate = sr) + win = Windowing(type="hann") + pitchDetect = PitchYinFFT(frameSize=frameSize, sampleRate=sr) pitch = [] confidence = [] for frame in frames: @@ -107,8 +105,8 @@ def runTest(self, signal, sr, freq, pitch_precision = 1, conf_precision = 0.1): self.assertAlmostEqual(mean(confidence), 1, conf_precision) def testInvalidParam(self): - self.assertConfigureFails(PitchYinFFT(), {'frameSize' : 1}) - self.assertConfigureFails(PitchYinFFT(), {'sampleRate' : 0}) + self.assertConfigureFails(PitchYinFFT(), {"frameSize": 1}) + self.assertConfigureFails(PitchYinFFT(), {"sampleRate": 0}) def testARealCase(self): # The expected values were recomputed from commit @@ -123,11 +121,11 @@ def testARealCase(self): frameSize = 1024 sr = 44100 hopSize = 512 - filename = join(testdata.audio_dir, 'recorded', 'vignesh.wav') + filename = join(testdata.audio_dir, "recorded", "vignesh.wav") audio = MonoLoader(filename=filename, sampleRate=44100)() frames = FrameGenerator(audio, frameSize=frameSize, hopSize=hopSize) - win = Windowing(type='hann') - pitchDetect = PitchYinFFT(frameSize=frameSize, sampleRate = sr) + win = Windowing(type="hann") + pitchDetect = PitchYinFFT(frameSize=frameSize, sampleRate=sr) pitch = [] confidence = [] for frame in frames: @@ -135,8 +133,10 @@ def testARealCase(self): f, conf = pitchDetect(spec) pitch += [f] confidence += [conf] - expected_pitch = numpy.load(join(filedir(), 'pitchyinfft/vignesh_pitch.npy')) - expected_conf = numpy.load(join(filedir(), 'pitchyinfft/vignesh_confidance.npy')) + expected_pitch = numpy.load(join(filedir(), "pitchyinfft/vignesh_pitch.npy")) + expected_conf = numpy.load( + join(filedir(), "pitchyinfft/vignesh_confidence.npy") + ) self.assertAlmostEqualVector(pitch, expected_pitch) self.assertAlmostEqualVector(confidence, expected_conf, 5e-5) @@ -148,16 +148,20 @@ def testARealCaseAubioComparison(self): frameSize = 4096 sr = 44100 hopSize = 512 - filename = join(testdata.audio_dir, 'recorded', 'vignesh.wav') + filename = join(testdata.audio_dir, "recorded", "vignesh.wav") audio = MonoLoader(filename=filename, sampleRate=44100)() - frames = FrameGenerator(audio, frameSize=frameSize, hopSize=hopSize, startFromZero=True) + frames = FrameGenerator( + audio, frameSize=frameSize, hopSize=hopSize, startFromZero=True + ) win = Windowing(normalized=False, zeroPhase=False) spec = Spectrum() pitchDetect = PitchYinFFT(frameSize=frameSize, sampleRate=sr) pitch = array([pitchDetect(spec(win(frame)))[0] for frame in frames]) - expected_pitch = numpy.load(join(filedir(), 'pitchyinfft/vignesh_pitch_aubio.npy')) + expected_pitch = numpy.load( + join(filedir(), "pitchyinfft/vignesh_pitch_aubio.npy") + ) # Trim the first and last frames as the # system behavior is unestable. @@ -171,16 +175,20 @@ def testARealCaseAubioWithToleranceComparison(self): frameSize = 4096 sr = 44100 hopSize = 512 - filename = join(testdata.audio_dir, 'recorded', 'vignesh.wav') + filename = join(testdata.audio_dir, "recorded", "vignesh.wav") audio = MonoLoader(filename=filename, sampleRate=44100)() - frames = FrameGenerator(audio, frameSize=frameSize, hopSize=hopSize, startFromZero=True) + frames = FrameGenerator( + audio, frameSize=frameSize, hopSize=hopSize, startFromZero=True + ) win = Windowing(normalized=False, zeroPhase=False) spec = Spectrum() pitchDetect = PitchYinFFT(frameSize=frameSize, sampleRate=sr, tolerance=0.4) pitch = array([pitchDetect(spec(win(frame)))[0] for frame in frames]) - expected_pitch = numpy.load(join(filedir(), 'pitchyinfft/vignesh_pitch_aubio_with_tolerance.npy')) + expected_pitch = numpy.load( + join(filedir(), "pitchyinfft/vignesh_pitch_aubio_with_tolerance.npy") + ) # Trim the first and last frames as the # system behavior is unestable. @@ -192,5 +200,5 @@ def testARealCaseAubioWithToleranceComparison(self): suite = allTests(TestPitchYinFFT) -if __name__ == '__main__': +if __name__ == "__main__": TextTestRunner(verbosity=2).run(suite)