annotate src/Modules/BMM/ModuleGammatone_test.py @ 16:2a5354042241

-Updated the Slaney IIR gammatone to use a cascase of four second-order filters as per the implementtion in Slaney's auditory toolbox. This is more numerically stable at high sample rates and low centre frequencies.
author tomwalters
date Sat, 20 Feb 2010 17:56:40 +0000
parents 3c782dec2fc0
children c5f5e9569863
rev   line source
tomwalters@5 1 #!/usr/bin/env python
tomwalters@5 2 # encoding: utf-8
tomwalters@5 3 #
tomwalters@5 4 # AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@5 5 # http://www.acousticscale.org/AIMC
tomwalters@5 6 #
tomwalters@5 7 # This program is free software: you can redistribute it and/or modify
tomwalters@5 8 # it under the terms of the GNU General Public License as published by
tomwalters@5 9 # the Free Software Foundation, either version 3 of the License, or
tomwalters@5 10 # (at your option) any later version.
tomwalters@5 11 #
tomwalters@5 12 # This program is distributed in the hope that it will be useful,
tomwalters@5 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@5 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@5 15 # GNU General Public License for more details.
tomwalters@5 16 #
tomwalters@5 17 # You should have received a copy of the GNU General Public License
tomwalters@5 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@5 19 """
tomwalters@5 20 ModuleGammatone_test.py
tomwalters@5 21
tomwalters@5 22 Created by Thomas Walters on 2010-02-15.
tomwalters@5 23 Copyright 2010 Thomas Walters <tom@acousticscale.org>
tomwalters@5 24 Test for the Slaney IIR gammatone.
tomwalters@5 25 """
tomwalters@5 26
tomwalters@5 27 import aimc
tomwalters@5 28 from scipy import io
tomwalters@16 29 import wave
tomwalters@16 30 import scipy
tomwalters@5 31
tomwalters@5 32 def main():
tomwalters@5 33 data_file = "src/Modules/BMM/testdata/gammatone.mat"
tomwalters@5 34 data = io.loadmat(data_file)
tomwalters@5 35
tomwalters@5 36 # The margin of error allowed between the returned values from AIM-C and
tomwalters@5 37 # the stored MATLAB values.
tomwalters@5 38 epsilon = 0.000001;
tomwalters@5 39
tomwalters@5 40 input_wave = data["input_wave"]
tomwalters@5 41 sample_rate = data["sample_rate"]
tomwalters@5 42 centre_frequencies = data["centre_frequencies"]
tomwalters@5 43 expected_output = data["expected_output"]
tomwalters@5 44
tomwalters@16 45 (channel_count, frame_count) = expected_output.shape
tomwalters@16 46 buffer_length = 20000;
tomwalters@5 47
tomwalters@5 48 input_sig = aimc.SignalBank()
tomwalters@16 49 input_sig.Initialize(1, buffer_length, 48000)
tomwalters@5 50 parameters = aimc.Parameters()
tomwalters@16 51 parameters.Load("src/Modules/BMM/testdata/gammatone.cfg")
tomwalters@5 52 mod_gt = aimc.ModuleGammatone(parameters)
tomwalters@5 53 mod_gt.Initialize(input_sig)
tomwalters@5 54
tomwalters@5 55 correct_count = 0;
tomwalters@5 56 incorrect_count = 0;
tomwalters@16 57
tomwalters@16 58 out = scipy.zeros((channel_count, buffer_length))
tomwalters@16 59
tomwalters@16 60 cfs = scipy.zeros((channel_count))
tomwalters@16 61
tomwalters@16 62 for i in range(0, buffer_length):
tomwalters@16 63 input_sig.set_sample(0, i, input_wave[i][0])
tomwalters@16 64 mod_gt.Process(input_sig)
tomwalters@16 65 out_sig = mod_gt.GetOutputBank()
tomwalters@16 66 for ch in range(0, out_sig.channel_count()):
tomwalters@16 67 cfs[ch] = out_sig.centre_frequency(ch);
tomwalters@16 68 for i in range(0, buffer_length):
tomwalters@16 69 out[ch, i] = out_sig.sample(ch, i)
tomwalters@16 70
tomwalters@16 71 outmat = dict(filterbank_out=out, centre_frequencies_out=cfs)
tomwalters@16 72 io.savemat("src/Modules/BMM/testdata/out_v2.mat", outmat)
tomwalters@5 73
tomwalters@5 74 pass
tomwalters@5 75
tomwalters@5 76
tomwalters@5 77 if __name__ == '__main__':
tomwalters@5 78 main()