Skip to content

Commit

Permalink
Typo.
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviliz committed May 14, 2024
1 parent bc566cc commit a6cce40
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 82 deletions.
77 changes: 39 additions & 38 deletions test/src/unittests/tonal/test_pitchyin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -119,19 +119,19 @@ 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:
f, conf = pitchDetect(frame)
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)
Expand All @@ -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
Expand All @@ -171,5 +172,5 @@ def testARealCaseVampComparison(self):

suite = allTests(TestPitchYin)

if __name__ == '__main__':
if __name__ == "__main__":
TextTestRunner(verbosity=2).run(suite)
96 changes: 52 additions & 44 deletions test/src/unittests/tonal/test_pitchyinfft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -123,20 +121,22 @@ 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:
spec = Spectrum()(win(frame))
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)

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -192,5 +200,5 @@ def testARealCaseAubioWithToleranceComparison(self):

suite = allTests(TestPitchYinFFT)

if __name__ == '__main__':
if __name__ == "__main__":
TextTestRunner(verbosity=2).run(suite)

0 comments on commit a6cce40

Please sign in to comment.