annotate src/Scripts/Strobes_test.py @ 32:9122efd2b227

-New AIMCopy main for the SSI features (temporary hack till I get a working module load system) -LocalMax strobe criterion. This is faster and better than the parabola version, which still seems buggy. -Noise generator module. Adds noise to a signal. Uses boost for the random number generator. -New options for the SSI -Slice now respects all its flags (oops!). -MATLAB functions for visualisation -Scripts for generating data to view in MATLAB -Script to download and build HTK - useful for running experiments
author tomwalters
date Thu, 25 Feb 2010 22:02:00 +0000
parents
children c5f5e9569863
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@32 7 # This program is free software: you can redistribute it and/or modify
tomwalters@32 8 # it under the terms of the GNU General Public License as published by
tomwalters@32 9 # the Free Software Foundation, either version 3 of the License, or
tomwalters@32 10 # (at your option) any later version.
tomwalters@32 11 #
tomwalters@32 12 # This program is distributed in the hope that it will be useful,
tomwalters@32 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
tomwalters@32 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tomwalters@32 15 # GNU General Public License for more details.
tomwalters@32 16 #
tomwalters@32 17 # You should have received a copy of the GNU General Public License
tomwalters@32 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
tomwalters@32 19 """
tomwalters@32 20 Profiles_test.py
tomwalters@32 21
tomwalters@32 22 Created by Thomas Walters on 2010-02-22.
tomwalters@32 23 Copyright 2010 Thomas Walters <tom@acousticscale.org>
tomwalters@32 24 Test the AIM-C model from filterbank to SSI profiles
tomwalters@32 25 """
tomwalters@32 26
tomwalters@32 27 import aimc
tomwalters@32 28 from scipy.io import wavfile
tomwalters@32 29 from scipy import io
tomwalters@32 30 import scipy
tomwalters@32 31 import pylab
tomwalters@32 32 import numpy
tomwalters@32 33 from itertools import izip, chain, repeat
tomwalters@32 34
tomwalters@32 35 def grouper(n, iterable, padvalue=None):
tomwalters@32 36 "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
tomwalters@32 37 return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
tomwalters@32 38
tomwalters@32 39 def BankToArray(out_bank):
tomwalters@32 40 channel_count = out_bank.channel_count()
tomwalters@32 41 out_buffer_length = out_bank.buffer_length()
tomwalters@32 42 out = scipy.zeros((channel_count,out_buffer_length))
tomwalters@32 43 for ch in range(0, channel_count):
tomwalters@32 44 for i in range(0, out_buffer_length):
tomwalters@32 45 out[ch, i] = out_bank.sample(ch, i)
tomwalters@32 46 return out
tomwalters@32 47
tomwalters@32 48 def StrobesToList(bank):
tomwalters@32 49 channel_count = bank.channel_count()
tomwalters@32 50 out = scipy.zeros((channel_count,), dtype=numpy.object)
tomwalters@32 51 for ch in range(0, channel_count):
tomwalters@32 52 s = []
tomwalters@32 53 for i in range(0, bank.strobe_count(ch)):
tomwalters@32 54 s.append(bank.strobe(ch, i))
tomwalters@32 55 out[ch] = s
tomwalters@32 56 return out
tomwalters@32 57
tomwalters@32 58 def main():
tomwalters@32 59 wave_path = "/Users/Tom/Documents/Work/PhD/HTK-AIM/Sounds/"
tomwalters@32 60
tomwalters@32 61 file_name = "ii/ii172.5p112.5s100.0t+000itd"
tomwalters@32 62
tomwalters@32 63 wave_suffix = ".wav"
tomwalters@32 64
tomwalters@32 65 frame_period_ms = 10;
tomwalters@32 66
tomwalters@32 67 wave_filename = wave_path + file_name + wave_suffix
tomwalters@32 68
tomwalters@32 69 (sample_rate, input_wave) = wavfile.read(wave_filename)
tomwalters@32 70 wave_length = input_wave.size
tomwalters@32 71 buffer_length = int(frame_period_ms * sample_rate / 1000)
tomwalters@32 72
tomwalters@32 73
tomwalters@32 74 input_sig = aimc.SignalBank()
tomwalters@32 75 input_sig.Initialize(1, buffer_length, sample_rate)
tomwalters@32 76 parameters = aimc.Parameters()
tomwalters@32 77 parameters.SetInt("input.buffersize", 480)
tomwalters@32 78
tomwalters@32 79 mod_gt = aimc.ModuleGammatone(parameters)
tomwalters@32 80 mod_hl = aimc.ModuleHCL(parameters)
tomwalters@32 81 mod_strobes = aimc.ModuleLocalMax(parameters)
tomwalters@32 82
tomwalters@32 83 mod_gt.AddTarget(mod_hl)
tomwalters@32 84 mod_hl.AddTarget(mod_strobes)
tomwalters@32 85
tomwalters@32 86 mod_gt.Initialize(input_sig)
tomwalters@32 87
tomwalters@32 88 correct_count = 0;
tomwalters@32 89 incorrect_count = 0;
tomwalters@32 90
tomwalters@32 91 scaled_wave = []
tomwalters@32 92 for sample in input_wave:
tomwalters@32 93 scaled_wave.append(float(sample / float(pow(2,15) - 1)))
tomwalters@32 94 i = 0
tomwalters@32 95
tomwalters@32 96 wave_chunks = grouper(buffer_length, scaled_wave, 0)
tomwalters@32 97
tomwalters@32 98 out_nap = []
tomwalters@32 99 out_strobes = []
tomwalters@32 100
tomwalters@32 101 for chunk in wave_chunks:
tomwalters@32 102 i = 0
tomwalters@32 103 for sample in chunk:
tomwalters@32 104 input_sig.set_sample(0, i, float(sample))
tomwalters@32 105 i += 1
tomwalters@32 106 mod_gt.Process(input_sig)
tomwalters@32 107 out_nap.append(BankToArray(mod_hl.GetOutputBank()))
tomwalters@32 108 out_strobes.append(StrobesToList(mod_strobes.GetOutputBank()))
tomwalters@32 109
tomwalters@32 110 outmat = dict(nap=out_nap, strobes=out_strobes)
tomwalters@32 111 io.savemat("src/Scripts/strobes_out.mat", outmat, oned_as='column')
tomwalters@32 112
tomwalters@32 113 pass
tomwalters@32 114
tomwalters@32 115
tomwalters@32 116 if __name__ == '__main__':
tomwalters@32 117 main()