Mercurial > hg > aimc
comparison swig/audio_example.py @ 155:5f51427130f8
small bug in /swig/audio_example.py
author | hamel.phil |
---|---|
date | Thu, 13 Jan 2011 18:49:40 +0000 |
parents | 60ac86f6add2 |
children | 0a8fac5bbfeb |
comparison
equal
deleted
inserted
replaced
154:2abaf1a47a37 | 155:5f51427130f8 |
---|---|
7 # | 7 # |
8 # Script to extract SAIs from an audio file using python | 8 # Script to extract SAIs from an audio file using python |
9 | 9 |
10 import aimc | 10 import aimc |
11 import numpy | 11 import numpy |
12 from copy import copy | |
12 | 13 |
13 do_plot=True | 14 def get_strobe_matrix(strobes,buf_len = 1024): |
15 n_channel = len(strobes) | |
16 strobe_mat = numpy.zeros((n_channel,buf_len)) | |
17 for ch in range(n_channel): | |
18 for s in strobes[ch]: | |
19 strobe_mat[ch,s] = 1. | |
20 return strobe_mat | |
21 | |
22 | |
23 do_plot=False | |
14 # Example wav file | 24 # Example wav file |
15 wavfile = '../test_data/short_example.wav' | 25 wavfile = '../test_data/short_example.wav' |
16 | 26 |
17 #Loading audio file | 27 #Loading audio file |
18 try: | 28 try: |
24 from scipy.io import wavfile as WF | 34 from scipy.io import wavfile as WF |
25 (sr,x) = WF.read(wavfile) | 35 (sr,x) = WF.read(wavfile) |
26 x=x.astype('float')/float(pow(2,15) - 1) | 36 x=x.astype('float')/float(pow(2,15) - 1) |
27 | 37 |
28 if x.ndim <= 1: | 38 if x.ndim <= 1: |
29 x=reshape(x,(x.shape[0],1)) | 39 x=numpy.reshape(x,(x.shape[0],1)) |
30 sr = 1.*sr | 40 sr = 1.*sr |
31 | 41 |
32 nChannels = x.shape[1] | 42 nChannels = x.shape[1] |
33 | 43 |
34 nSamples = x.shape[0] | 44 nSamples = x.shape[0] |
61 | 71 |
62 global_params = aimc.Parameters() | 72 global_params = aimc.Parameters() |
63 pzfc.Initialize(sig,global_params) | 73 pzfc.Initialize(sig,global_params) |
64 | 74 |
65 output_list = [] | 75 output_list = [] |
66 bank_list =[] | 76 strobe_list = [] |
77 centre_freq_list=[] | |
67 | 78 |
68 for f in range(nFrames): | 79 for f in range(nFrames): |
69 for i in range(nChannels): | 80 for i in range(nChannels): |
70 sig.set_signal(i,x[buffer_length*f:buffer_length*(f+1),i]) | 81 sig.set_signal(i,x[buffer_length*f:buffer_length*(f+1),i]) |
71 | 82 |
72 pzfc.Process(sig) | 83 pzfc.Process(sig) |
73 output_bank = sai.GetOutputBank() | 84 output_bank = sai.GetOutputBank() |
74 bank_list.append(output_bank) | |
75 n_channel = output_bank.channel_count() | 85 n_channel = output_bank.channel_count() |
76 sig_length = output_bank.buffer_length() | 86 sig_length = output_bank.buffer_length() |
77 output_matrix = numpy.zeros((n_channel,sig_length)) | 87 output_matrix = numpy.zeros((n_channel,sig_length)) |
88 strobes=[] | |
89 freqs=[] | |
78 for i in range(n_channel): | 90 for i in range(n_channel): |
79 output_matrix[i] = numpy.array(output_bank.get_signal(i)) | 91 output_matrix[i] = numpy.array(output_bank.get_signal(i)) |
92 freqs.append(output_bank.centre_frequency(i)) | |
93 channel_strobes = [] | |
94 for j in range(output_bank.strobe_count(i)): | |
95 channel_strobes.append(output_bank.strobe(i,j)) | |
96 strobes.append(channel_strobes) | |
97 centre_freq_list.append(freqs) | |
98 strobe_list.append(strobes) | |
80 output_list.append(output_matrix) | 99 output_list.append(output_matrix) |
100 # P.figure() | |
101 # P.imshow(output_matrix, aspect='auto', origin='lower') | |
102 | |
81 | 103 |
82 print 'nFrames, nChannels, nSamples, sample_rate' | 104 print 'nFrames, nChannels, nSamples, sample_rate' |
83 print nFrames, n_channel, sig_length, sr | 105 print nFrames, n_channel, sig_length, sr |
84 | 106 |
85 | 107 |