tomwalters@1: #!/usr/bin/env python tomwalters@1: # encoding: utf-8 tomwalters@1: # tomwalters@1: # AIM-C: A C++ implementation of the Auditory Image Model tomwalters@1: # http://www.acousticscale.org/AIMC tomwalters@1: # tomwalters@45: # Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@45: # you may not use this file except in compliance with the License. tomwalters@45: # You may obtain a copy of the License at tomwalters@1: # tomwalters@45: # http://www.apache.org/licenses/LICENSE-2.0 tomwalters@1: # tomwalters@45: # Unless required by applicable law or agreed to in writing, software tomwalters@45: # distributed under the License is distributed on an "AS IS" BASIS, tomwalters@45: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@45: # See the License for the specific language governing permissions and tomwalters@45: # limitations under the License. tomwalters@1: """ tomwalters@1: ModuleGaussians_test.py tomwalters@1: tomwalters@1: Created by Thomas Walters on 2010-02-15. tomwalters@1: Copyright 2010 Thomas Walters tomwalters@1: Test for the Gaussians module. Runs a number of pre-computed SAI profiles tomwalters@3: through the module, and tests them against the saved output from the tomwalters@1: MATLAB rubber_GMM code. tomwalters@1: """ tomwalters@1: tomwalters@1: import aimc tomwalters@2: from scipy import io tomwalters@1: tomwalters@1: def main(): tomwalters@1: data_file = "src/Modules/Features/testdata/aa153.0p108.1s100.0t+000itd.mat" tomwalters@2: data = io.loadmat(data_file) tomwalters@2: tomwalters@2: # The margin of error allowed between the returned values from AIM-C and tomwalters@2: # the stored MATLAB values. tomwalters@11: epsilon = 0.00001; tomwalters@1: tomwalters@1: given_profiles = data["Templates"] tomwalters@1: matlab_features = data["feature"] tomwalters@1: tomwalters@2: (profile_count, channel_count) = given_profiles.shape tomwalters@1: tomwalters@2: profile_sig = aimc.SignalBank() tomwalters@2: profile_sig.Initialize(channel_count, 1, 44100) tomwalters@2: parameters = aimc.Parameters() tomwalters@2: mod_gauss = aimc.ModuleGaussians(parameters) tomwalters@2: mod_gauss.Initialize(profile_sig) tomwalters@1: tomwalters@2: correct_count = 0; tomwalters@2: incorrect_count = 0; tomwalters@2: for p in range(0, profile_count): tomwalters@2: profile = given_profiles[p] tomwalters@2: features = matlab_features[p] tomwalters@2: for i in range(0, channel_count): tomwalters@2: profile_sig.set_sample(i, 0, profile[i]) tomwalters@2: mod_gauss.Process(profile_sig) tomwalters@2: out_sig = mod_gauss.GetOutputBank() tomwalters@2: error = False; tomwalters@2: for j in range(0, out_sig.channel_count()): tomwalters@2: if (abs(out_sig.sample(j, 0) - features[j]) > epsilon): tomwalters@2: error = True; tomwalters@2: incorrect_count += 1; tomwalters@2: else: tomwalters@2: correct_count += 1; tomwalters@2: if error: tomwalters@2: print("Mismatch at profile %d" % (p)) tomwalters@2: 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: print("MATLAB values: %f %f %f %f" % (features[0], features[1], features[2], features[3])) tomwalters@2: print("") tomwalters@2: percent_correct = 100 * correct_count / (correct_count + incorrect_count) tomwalters@2: print("Total correct: %f percent" % (percent_correct)) tomwalters@2: if percent_correct == 100: tomwalters@2: print("=== TEST PASSED ===") tomwalters@2: else: tomwalters@2: print("=== TEST FAILED! ===") tomwalters@2: tomwalters@1: pass tomwalters@1: tomwalters@1: tomwalters@1: if __name__ == '__main__': tomwalters@1: main()