annotate src/Scripts/Strobes_test.py @ 169:78cbca1cf218

- AWS fixes (part 1)
author tomwalters
date Wed, 11 Aug 2010 08:55:29 +0000
parents c5f5e9569863
children
rev   line source
tomwalters@32 1 #!/usr/bin/env python
tomwalters@32 2 # encoding: utf-8
tomwalters@32 3 #
tomwalters@32 4 # AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@32 5 # http://www.acousticscale.org/AIMC
tomwalters@32 6 #
tomwalters@45 7 # Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@45 8 # you may not use this file except in compliance with the License.
tomwalters@45 9 # You may obtain a copy of the License at
tomwalters@32 10 #
tomwalters@45 11 # http://www.apache.org/licenses/LICENSE-2.0
tomwalters@32 12 #
tomwalters@45 13 # Unless required by applicable law or agreed to in writing, software
tomwalters@45 14 # distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@45 15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@45 16 # See the License for the specific language governing permissions and
tomwalters@45 17 # limitations under the License.
tomwalters@32 18 """
tomwalters@32 19 Profiles_test.py
tomwalters@32 20
tomwalters@32 21 Created by Thomas Walters on 2010-02-22.
tomwalters@32 22 Copyright 2010 Thomas Walters <tom@acousticscale.org>
tomwalters@32 23 Test the AIM-C model from filterbank to SSI profiles
tomwalters@32 24 """
tomwalters@32 25
tomwalters@32 26 import aimc
tomwalters@32 27 from scipy.io import wavfile
tomwalters@32 28 from scipy import io
tomwalters@32 29 import scipy
tomwalters@32 30 import pylab
tomwalters@32 31 import numpy
tomwalters@32 32 from itertools import izip, chain, repeat
tomwalters@32 33
tomwalters@32 34 def grouper(n, iterable, padvalue=None):
tomwalters@32 35 "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
tomwalters@32 36 return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
tomwalters@32 37
tomwalters@32 38 def BankToArray(out_bank):
tomwalters@32 39 channel_count = out_bank.channel_count()
tomwalters@32 40 out_buffer_length = out_bank.buffer_length()
tomwalters@32 41 out = scipy.zeros((channel_count,out_buffer_length))
tomwalters@32 42 for ch in range(0, channel_count):
tomwalters@32 43 for i in range(0, out_buffer_length):
tomwalters@32 44 out[ch, i] = out_bank.sample(ch, i)
tomwalters@32 45 return out
tomwalters@32 46
tomwalters@32 47 def StrobesToList(bank):
tomwalters@32 48 channel_count = bank.channel_count()
tomwalters@32 49 out = scipy.zeros((channel_count,), dtype=numpy.object)
tomwalters@32 50 for ch in range(0, channel_count):
tomwalters@32 51 s = []
tomwalters@32 52 for i in range(0, bank.strobe_count(ch)):
tomwalters@32 53 s.append(bank.strobe(ch, i))
tomwalters@32 54 out[ch] = s
tomwalters@32 55 return out
tomwalters@32 56
tomwalters@32 57 def main():
tomwalters@32 58 wave_path = "/Users/Tom/Documents/Work/PhD/HTK-AIM/Sounds/"
tomwalters@32 59
tomwalters@32 60 file_name = "ii/ii172.5p112.5s100.0t+000itd"
tomwalters@32 61
tomwalters@32 62 wave_suffix = ".wav"
tomwalters@32 63
tomwalters@32 64 frame_period_ms = 10;
tomwalters@32 65
tomwalters@32 66 wave_filename = wave_path + file_name + wave_suffix
tomwalters@32 67
tomwalters@32 68 (sample_rate, input_wave) = wavfile.read(wave_filename)
tomwalters@32 69 wave_length = input_wave.size
tomwalters@32 70 buffer_length = int(frame_period_ms * sample_rate / 1000)
tomwalters@32 71
tomwalters@32 72
tomwalters@32 73 input_sig = aimc.SignalBank()
tomwalters@32 74 input_sig.Initialize(1, buffer_length, sample_rate)
tomwalters@32 75 parameters = aimc.Parameters()
tomwalters@32 76 parameters.SetInt("input.buffersize", 480)
tomwalters@32 77
tomwalters@32 78 mod_gt = aimc.ModuleGammatone(parameters)
tomwalters@32 79 mod_hl = aimc.ModuleHCL(parameters)
tomwalters@32 80 mod_strobes = aimc.ModuleLocalMax(parameters)
tomwalters@32 81
tomwalters@32 82 mod_gt.AddTarget(mod_hl)
tomwalters@32 83 mod_hl.AddTarget(mod_strobes)
tomwalters@32 84
tomwalters@32 85 mod_gt.Initialize(input_sig)
tomwalters@32 86
tomwalters@32 87 correct_count = 0;
tomwalters@32 88 incorrect_count = 0;
tomwalters@32 89
tomwalters@32 90 scaled_wave = []
tomwalters@32 91 for sample in input_wave:
tomwalters@32 92 scaled_wave.append(float(sample / float(pow(2,15) - 1)))
tomwalters@32 93 i = 0
tomwalters@32 94
tomwalters@32 95 wave_chunks = grouper(buffer_length, scaled_wave, 0)
tomwalters@32 96
tomwalters@32 97 out_nap = []
tomwalters@32 98 out_strobes = []
tomwalters@32 99
tomwalters@32 100 for chunk in wave_chunks:
tomwalters@32 101 i = 0
tomwalters@32 102 for sample in chunk:
tomwalters@32 103 input_sig.set_sample(0, i, float(sample))
tomwalters@32 104 i += 1
tomwalters@32 105 mod_gt.Process(input_sig)
tomwalters@32 106 out_nap.append(BankToArray(mod_hl.GetOutputBank()))
tomwalters@32 107 out_strobes.append(StrobesToList(mod_strobes.GetOutputBank()))
tomwalters@32 108
tomwalters@32 109 outmat = dict(nap=out_nap, strobes=out_strobes)
tomwalters@32 110 io.savemat("src/Scripts/strobes_out.mat", outmat, oned_as='column')
tomwalters@32 111
tomwalters@32 112 pass
tomwalters@32 113
tomwalters@32 114
tomwalters@32 115 if __name__ == '__main__':
tomwalters@32 116 main()