annotate trunk/src/Modules/Features/ModuleGaussians_test.py @ 274:3640d25b65ab

- Fixed the gaussian fitting to use doubles internally (like MATLAB) - Fixed an irritating bug that was causing the Gaussian fitting to be incorrectly initialized, leading to small differences between the AIM-C and MATLAB code - Finalised the Gaussian fitting test
author tomwalters
date Tue, 16 Feb 2010 13:23:23 +0000
parents c26222c51fb7
children ce2bab04f155
rev   line source
tomwalters@273 1 #!/usr/bin/env python
tomwalters@273 2 # encoding: utf-8
tomwalters@273 3 #
tomwalters@273 4 # AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@273 5 # http://www.acousticscale.org/AIMC
tomwalters@273 6 #
tomwalters@273 7 # This program is free software: you can redistribute it and/or modify
tomwalters@273 8 # it under the terms of the GNU General Public License as published by
tomwalters@273 9 # the Free Software Foundation, either version 3 of the License, or
tomwalters@273 10 # (at your option) any later version.
tomwalters@273 11 #
tomwalters@273 12 # This program is distributed in the hope that it will be useful,
tomwalters@273 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@273 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@273 15 # GNU General Public License for more details.
tomwalters@273 16 #
tomwalters@273 17 # You should have received a copy of the GNU General Public License
tomwalters@273 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@273 19 """
tomwalters@273 20 ModuleGaussians_test.py
tomwalters@273 21
tomwalters@273 22 Created by Thomas Walters on 2010-02-15.
tomwalters@273 23 Copyright 2010 Thomas Walters <tom@acousticscale.org>
tomwalters@273 24 Test for the Gaussians module. Runs a number of pre-computed SAI profiles
tomwalters@273 25 through the module, and tests them against the equivalent output from the
tomwalters@273 26 MATLAB rubber_GMM code.
tomwalters@273 27 """
tomwalters@273 28
tomwalters@273 29 import aimc
tomwalters@273 30 import matplotlib
tomwalters@273 31 import pylab
tomwalters@274 32 from scipy import io
tomwalters@273 33
tomwalters@273 34 def main():
tomwalters@273 35 data_file = "src/Modules/Features/testdata/aa153.0p108.1s100.0t+000itd.mat"
tomwalters@274 36 data = io.loadmat(data_file)
tomwalters@274 37
tomwalters@274 38 # The margin of error allowed between the returned values from AIM-C and
tomwalters@274 39 # the stored MATLAB values.
tomwalters@274 40 epsilon = 0.000001;
tomwalters@273 41
tomwalters@273 42 given_profiles = data["Templates"]
tomwalters@273 43 matlab_features = data["feature"]
tomwalters@273 44
tomwalters@274 45 (profile_count, channel_count) = given_profiles.shape
tomwalters@273 46
tomwalters@274 47 profile_sig = aimc.SignalBank()
tomwalters@274 48 profile_sig.Initialize(channel_count, 1, 44100)
tomwalters@274 49 parameters = aimc.Parameters()
tomwalters@274 50 mod_gauss = aimc.ModuleGaussians(parameters)
tomwalters@274 51 mod_gauss.Initialize(profile_sig)
tomwalters@273 52
tomwalters@274 53 correct_count = 0;
tomwalters@274 54 incorrect_count = 0;
tomwalters@274 55 for p in range(0, profile_count):
tomwalters@274 56 profile = given_profiles[p]
tomwalters@274 57 features = matlab_features[p]
tomwalters@274 58 for i in range(0, channel_count):
tomwalters@274 59 profile_sig.set_sample(i, 0, profile[i])
tomwalters@274 60 mod_gauss.Process(profile_sig)
tomwalters@274 61 out_sig = mod_gauss.GetOutputBank()
tomwalters@274 62 error = False;
tomwalters@274 63 for j in range(0, out_sig.channel_count()):
tomwalters@274 64 if (abs(out_sig.sample(j, 0) - features[j]) > epsilon):
tomwalters@274 65 error = True;
tomwalters@274 66 incorrect_count += 1;
tomwalters@274 67 else:
tomwalters@274 68 correct_count += 1;
tomwalters@274 69 if error:
tomwalters@274 70 print("Mismatch at profile %d" % (p))
tomwalters@274 71 print("AIM-C values: %f %f %f %f" % (out_sig.sample(0, 0), out_sig.sample(1, 0), out_sig.sample(2, 0), out_sig.sample(3, 0)))
tomwalters@274 72 print("MATLAB values: %f %f %f %f" % (features[0], features[1], features[2], features[3]))
tomwalters@274 73 print("")
tomwalters@274 74 percent_correct = 100 * correct_count / (correct_count + incorrect_count)
tomwalters@274 75 print("Total correct: %f percent" % (percent_correct))
tomwalters@274 76 if percent_correct == 100:
tomwalters@274 77 print("=== TEST PASSED ===")
tomwalters@274 78 else:
tomwalters@274 79 print("=== TEST FAILED! ===")
tomwalters@274 80
tomwalters@273 81 pass
tomwalters@273 82
tomwalters@273 83
tomwalters@273 84 if __name__ == '__main__':
tomwalters@273 85 main()