annotate trunk/src/Scripts/Profiles_test.py @ 294:921575ec87a3

- Added scripts directory with a few basic scripts for testing modules and interfacting with matlab
author tomwalters
date Mon, 22 Feb 2010 18:17:14 +0000
parents
children 30dde71d0230
rev   line source
tomwalters@294 1 #!/usr/bin/env python
tomwalters@294 2 # encoding: utf-8
tomwalters@294 3 #
tomwalters@294 4 # AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@294 5 # http://www.acousticscale.org/AIMC
tomwalters@294 6 #
tomwalters@294 7 # This program is free software: you can redistribute it and/or modify
tomwalters@294 8 # it under the terms of the GNU General Public License as published by
tomwalters@294 9 # the Free Software Foundation, either version 3 of the License, or
tomwalters@294 10 # (at your option) any later version.
tomwalters@294 11 #
tomwalters@294 12 # This program is distributed in the hope that it will be useful,
tomwalters@294 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@294 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@294 15 # GNU General Public License for more details.
tomwalters@294 16 #
tomwalters@294 17 # You should have received a copy of the GNU General Public License
tomwalters@294 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@294 19 """
tomwalters@294 20 Profiles_test.py
tomwalters@294 21
tomwalters@294 22 Created by Thomas Walters on 2010-02-22.
tomwalters@294 23 Copyright 2010 Thomas Walters <tom@acousticscale.org>
tomwalters@294 24 Test the AIM-C model from filterbank to SSI profiles
tomwalters@294 25 """
tomwalters@294 26
tomwalters@294 27 import aimc
tomwalters@294 28 from scipy.io import wavfile
tomwalters@294 29 from scipy import io
tomwalters@294 30 import scipy
tomwalters@294 31 import pylab
tomwalters@294 32 from itertools import izip, chain, repeat
tomwalters@294 33
tomwalters@294 34 def grouper(n, iterable, padvalue=None):
tomwalters@294 35 "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
tomwalters@294 36 return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
tomwalters@294 37
tomwalters@294 38 def main():
tomwalters@294 39 wave_path = "/Users/Tom/Documents/Work/PhD/HTK-AIM/Sounds/"
tomwalters@294 40 features_path = "/Users/Tom/Documents/Work/PhD/HTK-AIM/work08-jess-original-rec_rubber/features/"
tomwalters@294 41
tomwalters@294 42 file_name = "aa/aa161.1p119.4s100.0t+000itd"
tomwalters@294 43
tomwalters@294 44 wave_suffix = ".wav"
tomwalters@294 45 features_suffix = ".mat"
tomwalters@294 46
tomwalters@294 47 frame_period_ms = 10;
tomwalters@294 48
tomwalters@294 49 wave_filename = wave_path + file_name + wave_suffix
tomwalters@294 50 features_filename = features_path + file_name + features_suffix
tomwalters@294 51
tomwalters@294 52 (sample_rate, input_wave) = wavfile.read(wave_filename)
tomwalters@294 53 wave_length = input_wave.size
tomwalters@294 54 buffer_length = int(frame_period_ms * sample_rate / 1000)
tomwalters@294 55
tomwalters@294 56 #pylab.plot(input_wave)
tomwalters@294 57 #pylab.show()
tomwalters@294 58
tomwalters@294 59 input_sig = aimc.SignalBank()
tomwalters@294 60 input_sig.Initialize(1, buffer_length, sample_rate)
tomwalters@294 61 parameters = aimc.Parameters()
tomwalters@294 62 parameters.Load("src/Scripts/profile_features.cfg")
tomwalters@294 63 mod_gt = aimc.ModuleGammatone(parameters)
tomwalters@294 64 mod_hl = aimc.ModuleHCL(parameters)
tomwalters@294 65 mod_profile = aimc.ModuleSlice(parameters)
tomwalters@294 66 mod_scaler = aimc.ModuleScaler(parameters)
tomwalters@294 67 mod_gt.AddTarget(mod_hl)
tomwalters@294 68 mod_hl.AddTarget(mod_profile)
tomwalters@294 69 mod_profile.AddTarget(mod_scaler)
tomwalters@294 70 mod_gt.Initialize(input_sig)
tomwalters@294 71
tomwalters@294 72 correct_count = 0;
tomwalters@294 73 incorrect_count = 0;
tomwalters@294 74
tomwalters@294 75 scaled_wave = []
tomwalters@294 76 for sample in input_wave:
tomwalters@294 77 scaled_wave.append(float(sample / float(pow(2,15) - 1)))
tomwalters@294 78 i = 0
tomwalters@294 79
tomwalters@294 80 wave_chunks = grouper(buffer_length, scaled_wave, 0)
tomwalters@294 81
tomwalters@294 82 out_frames = []
tomwalters@294 83 for chunk in wave_chunks:
tomwalters@294 84 i = 0
tomwalters@294 85 for sample in chunk:
tomwalters@294 86 input_sig.set_sample(0, i, float(sample))
tomwalters@294 87 i += 1
tomwalters@294 88 mod_gt.Process(input_sig)
tomwalters@294 89 out_sig = mod_scaler.GetOutputBank()
tomwalters@294 90
tomwalters@294 91 channel_count = out_sig.channel_count()
tomwalters@294 92 out_buffer_length = out_sig.buffer_length()
tomwalters@294 93 cfs = scipy.zeros((channel_count))
tomwalters@294 94 out = scipy.zeros((channel_count, out_buffer_length))
tomwalters@294 95
tomwalters@294 96 for ch in range(0, channel_count):
tomwalters@294 97 for i in range(0, out_buffer_length):
tomwalters@294 98 out[ch, i] = out_sig.sample(ch, i)
tomwalters@294 99 out_frames.append(out)
tomwalters@294 100
tomwalters@294 101 outmat = dict(profile_out=out_frames)
tomwalters@294 102 io.savemat("src/Scripts/profile_out.mat", outmat)
tomwalters@294 103
tomwalters@294 104 pass
tomwalters@294 105
tomwalters@294 106
tomwalters@294 107 if __name__ == '__main__':
tomwalters@294 108 main()