<commit_before>#!/usr/bin/env python

# Copyright (C) 2006-2016  Music Technology Group - Universitat Pompeu Fabra
#
# This file is part of Essentia
#
# Essentia is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the Affero GNU General Public License
# version 3 along with this program. If not, see http://www.gnu.org/licenses/



from essentia_test import *
from math import *
import numpy as np

class TestGFCC(TestCase):

    def InitGFCC(self, numCoeffs, logType):
        return GFCC(inputSize = 1025,
                    sampleRate = 44100,
                    numberBands = 40,
                    numberCoefficients = numCoeffs,
                    lowFrequencyBound = 0,
                    highFrequencyBound = 11000,
                    logType = logType)


    def testRegression(self):
        # only testing that it yields valid result, but still need to check for
        # correct results.. no ground truth provided
        size = 20
        logType = 'natural'
        while (size > 0) :
            bands, gfcc = self.InitGFCC(size, logType)(ones(1025))
            self.assertEqual(len(gfcc), size )
            self.assertEqual(len(bands), 40 )
            self.assert_(not any(numpy.isnan(gfcc)))
            self.assert_(not any(numpy.isinf(gfcc)))
            size -= 1


    def testZero(self):
        # zero input should return dct(lin2db(0)). Try with different sizes
        size = 1025
        val = amp2db(-90.0)
        expected = DCT(inputSize=40, outputSize=13)([val for x in range(40)])
        while (size > 256 ):
            bands, gfcc = GFCC(inputSize = size)(zeros(size))
            self.assertEqualVector(gfcc, expected)
            size /= 2


    def testInvalidInput(self):
        # mel bands should fail for a spectrum with less than 2 bins
        self.assertComputeFails(GFCC(), [])
        self.assertComputeFails(GFCC(), [0.5])


    def testInvalidParam(self):
        self.assertConfigureFails(GFCC(), { 'numberBands': 0 })
        self.assertConfigureFails(GFCC(), { 'numberBands': 1 })
        self.assertConfigureFails(GFCC(), { 'numberCoefficients': 0 })
        # number of coefficients cannot be larger than number of bands, otherwise dct
        # will throw and exception
        self.assertConfigureFails(GFCC(), { 'numberBands': 10, 'numberCoefficients':13 })
        self.assertConfigureFails(GFCC(), { 'lowFrequencyBound': -100 })
        # low freq bound cannot be larger than high freq bound
        self.assertConfigureFails(GFCC(), { 'lowFrequencyBound': 100,
                                            'highFrequencyBound': 50 })
        # high freq bound cannot be larger than half the sr
        self.assertConfigureFails(GFCC(), { 'highFrequencyBound': 30000,
                                            'sampleRate': 22050} )
        self.assertConfigureFails(GFCC(), { 'logType': 'linear'} )



    def testRealCase(self):
        from numpy import mean
        filename = join(testdata.audio_dir, 'recorded','musicbox.wav')
        audio = MonoLoader(filename=filename, sampleRate=44100)()
        fftSize = 2**11
        frameSize = 1025
        paddingSize = fftSize - frameSize
        frameGenerator = FrameGenerator(audio, frameSize=frameSize, hopSize=512)
        window = Windowing(size = frameSize,
                           zeroPadding = paddingSize,
                           type="blackmanharris62")
        spectrum = Spectrum()
        pool=Pool()
        gfccAlgo = self.InitGFCC(13,'natural')

        for frame in frameGenerator:
            bands, gfcc = gfccAlgo(spectrum(window(frame)))
            pool.add("bands", bands)
            pool.add("gfcc", gfcc)

        expected = [ 398.78009033,  272.66052246, -192.00653076, -328.17059326,
       -180.25398254,  -52.98754883,    0.61827064,   98.50186157,
        151.67419434,   50.76211166,  -60.87889099,  -28.52173042,
        33.24774933]

        self.assertAlmostEqualVector(mean(pool['gfcc'], 0), expected, 1.0e-5)<commit_msg>GFCC: testing the former configuration #503<commit_after>#!/usr/bin/env python

# Copyright (C) 2006-2016  Music Technology Group - Universitat Pompeu Fabra
#
# This file is part of Essentia
#
# Essentia is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the Affero GNU General Public License
# version 3 along with this program. If not, see http://www.gnu.org/licenses/



from essentia_test import *
from math import *
import numpy as np

class TestGFCC(TestCase):

    def InitGFCC(self, numCoeffs, logType):
        return GFCC(inputSize = 1025,
                    sampleRate = 44100,
                    numberBands = 40,
                    numberCoefficients = numCoeffs,
                    lowFrequencyBound = 0,
                    highFrequencyBound = 11000,
                    logType = logType)


    def testRegression(self):
        # only testing that it yields valid result, but still need to check for
        # correct results.. no ground truth provided
        size = 20
        logType = 'natural'
        while (size > 0) :
            bands, gfcc = self.InitGFCC(size, logType)(ones(1025))
            self.assertEqual(len(gfcc), size )
            self.assertEqual(len(bands), 40 )
            self.assert_(not any(numpy.isnan(gfcc)))
            self.assert_(not any(numpy.isinf(gfcc)))
            size -= 1


    def testZero(self):
        # zero input should return dct(lin2db(0)). Try with different sizes
        size = 1025
        val = amp2db(-90.0)
        expected = DCT(inputSize=40, outputSize=13)([val for x in range(40)])
        while (size > 256 ):
            bands, gfcc = GFCC(inputSize = size)(zeros(size))
            self.assertEqualVector(gfcc, expected)
            size /= 2


    def testInvalidInput(self):
        # mel bands should fail for a spectrum with less than 2 bins
        self.assertComputeFails(GFCC(), [])
        self.assertComputeFails(GFCC(), [0.5])


    def testInvalidParam(self):
        self.assertConfigureFails(GFCC(), { 'numberBands': 0 })
        self.assertConfigureFails(GFCC(), { 'numberBands': 1 })
        self.assertConfigureFails(GFCC(), { 'numberCoefficients': 0 })
        # number of coefficients cannot be larger than number of bands, otherwise dct
        # will throw and exception
        self.assertConfigureFails(GFCC(), { 'numberBands': 10, 'numberCoefficients':13 })
        self.assertConfigureFails(GFCC(), { 'lowFrequencyBound': -100 })
        # low freq bound cannot be larger than high freq bound
        self.assertConfigureFails(GFCC(), { 'lowFrequencyBound': 100,
                                            'highFrequencyBound': 50 })
        # high freq bound cannot be larger than half the sr
        self.assertConfigureFails(GFCC(), { 'highFrequencyBound': 30000,
                                            'sampleRate': 22050} )
        self.assertConfigureFails(GFCC(), { 'logType': 'linear'} )



    def testRealCase(self):
        from numpy import mean
        filename = join(testdata.audio_dir, 'recorded','musicbox.wav')
        audio = MonoLoader(filename=filename, sampleRate=44100)()
        fftSize = 2**11
        frameSize = 1025
        paddingSize = fftSize - frameSize
        frameGenerator = FrameGenerator(audio, frameSize=frameSize, hopSize=512)
        window = Windowing(size = frameSize,
                           zeroPadding = paddingSize,
                           type="blackmanharris62")
        spectrum = Spectrum()
        pool=Pool()
        gfccAlgo = self.InitGFCC(13,'dbamp')

        for frame in frameGenerator:
            bands, gfcc = gfccAlgo(spectrum(window(frame)))
            pool.add("bands", bands)
            pool.add("gfcc", gfcc)

        expected = [ -32.09788513,  108.49235535, -118.94884491,  -97.3769455 ,
        -84.4560318 ,  -42.14659119,  -53.62257767,  -33.75775909,
        -12.94090176,  -16.27241516,  -27.33692932,   -5.6728158 ,
         -3.05162358]

        self.assertAlmostEqualVector(mean(pool['gfcc'], 0), expected, 1.0e-5)
{'fraction_non_alphanumeric': 0.09829107561711159, 'fraction_numerical': 0.05372500837708031, 'mean_word_length': 2.34478894284647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6331412', 'n_tokens_mistral': 2928, 'n_tokens_neox': 2524, 'n_words': 756}