annotate src/Modules/Features/ModuleGaussians_test.py @ 2:e91769e84be1

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