tomwalters@273: #!/usr/bin/env python
tomwalters@273: # encoding: utf-8
tomwalters@273: #
tomwalters@273: # AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@273: # http://www.acousticscale.org/AIMC
tomwalters@273: #
tomwalters@273: # This program is free software: you can redistribute it and/or modify
tomwalters@273: # it under the terms of the GNU General Public License as published by
tomwalters@273: # the Free Software Foundation, either version 3 of the License, or
tomwalters@273: # (at your option) any later version.
tomwalters@273: #
tomwalters@273: # This program is distributed in the hope that it will be useful,
tomwalters@273: # but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@273: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@273: # GNU General Public License for more details.
tomwalters@273: #
tomwalters@273: # You should have received a copy of the GNU General Public License
tomwalters@273: # along with this program. If not, see .
tomwalters@273: """
tomwalters@273: ModuleGaussians_test.py
tomwalters@273:
tomwalters@273: Created by Thomas Walters on 2010-02-15.
tomwalters@273: Copyright 2010 Thomas Walters
tomwalters@273: Test for the Gaussians module. Runs a number of pre-computed SAI profiles
tomwalters@273: through the module, and tests them against the equivalent output from the
tomwalters@273: MATLAB rubber_GMM code.
tomwalters@273: """
tomwalters@273:
tomwalters@273: import aimc
tomwalters@273: import matplotlib
tomwalters@273: import pylab
tomwalters@274: from scipy import io
tomwalters@273:
tomwalters@273: def main():
tomwalters@273: data_file = "src/Modules/Features/testdata/aa153.0p108.1s100.0t+000itd.mat"
tomwalters@274: data = io.loadmat(data_file)
tomwalters@274:
tomwalters@274: # The margin of error allowed between the returned values from AIM-C and
tomwalters@274: # the stored MATLAB values.
tomwalters@274: epsilon = 0.000001;
tomwalters@273:
tomwalters@273: given_profiles = data["Templates"]
tomwalters@273: matlab_features = data["feature"]
tomwalters@273:
tomwalters@274: (profile_count, channel_count) = given_profiles.shape
tomwalters@273:
tomwalters@274: profile_sig = aimc.SignalBank()
tomwalters@274: profile_sig.Initialize(channel_count, 1, 44100)
tomwalters@274: parameters = aimc.Parameters()
tomwalters@274: mod_gauss = aimc.ModuleGaussians(parameters)
tomwalters@274: mod_gauss.Initialize(profile_sig)
tomwalters@273:
tomwalters@274: correct_count = 0;
tomwalters@274: incorrect_count = 0;
tomwalters@274: for p in range(0, profile_count):
tomwalters@274: profile = given_profiles[p]
tomwalters@274: features = matlab_features[p]
tomwalters@274: for i in range(0, channel_count):
tomwalters@274: profile_sig.set_sample(i, 0, profile[i])
tomwalters@274: mod_gauss.Process(profile_sig)
tomwalters@274: out_sig = mod_gauss.GetOutputBank()
tomwalters@274: error = False;
tomwalters@274: for j in range(0, out_sig.channel_count()):
tomwalters@274: if (abs(out_sig.sample(j, 0) - features[j]) > epsilon):
tomwalters@274: error = True;
tomwalters@274: incorrect_count += 1;
tomwalters@274: else:
tomwalters@274: correct_count += 1;
tomwalters@274: if error:
tomwalters@274: print("Mismatch at profile %d" % (p))
tomwalters@274: 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: print("MATLAB values: %f %f %f %f" % (features[0], features[1], features[2], features[3]))
tomwalters@274: print("")
tomwalters@274: percent_correct = 100 * correct_count / (correct_count + incorrect_count)
tomwalters@274: print("Total correct: %f percent" % (percent_correct))
tomwalters@274: if percent_correct == 100:
tomwalters@274: print("=== TEST PASSED ===")
tomwalters@274: else:
tomwalters@274: print("=== TEST FAILED! ===")
tomwalters@274:
tomwalters@273: pass
tomwalters@273:
tomwalters@273:
tomwalters@273: if __name__ == '__main__':
tomwalters@273: main()