Package timeside :: Package analyzer :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module timeside.analyzer.utils

  1  # -*- coding: utf-8 -*- 
  2  # 
  3  # Copyright (c) 2013 Paul Brossier <piem@piem.org> 
  4   
  5  # This file is part of TimeSide. 
  6   
  7  # TimeSide is free software: you can redistribute it and/or modify 
  8  # it under the terms of the GNU General Public License as published by 
  9  # the Free Software Foundation, either version 2 of the License, or 
 10  # (at your option) any later version. 
 11   
 12  # TimeSide is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15  # GNU General Public License for more details. 
 16   
 17  # You should have received a copy of the GNU General Public License 
 18  # along with TimeSide.  If not, see <http://www.gnu.org/licenses/>. 
 19   
 20  # Author: Paul Brossier <piem@piem.org> 
 21   
 22  import numpy 
 23   
24 -def downsample_blocking(frames, hop_s, dtype='float32'):
25 # downmixing to one channel 26 if len(frames.shape) != 1: 27 downsampled = frames.sum(axis = -1) / frames.shape[-1] 28 else: 29 downsampled = frames 30 # zero padding to have a multiple of hop_s 31 if downsampled.shape[0] % hop_s != 0: 32 pad_length = hop_s + downsampled.shape[0] / hop_s * hop_s - downsampled.shape[0] 33 downsampled = numpy.hstack([downsampled, numpy.zeros(pad_length, dtype = dtype)]) 34 # blocking 35 return downsampled.reshape(downsampled.shape[0] / hop_s, hop_s)
36
37 -def computeModulation(serie,wLen,withLog=True):
38 ''' 39 Compute the modulation of a parameter centered. Extremums are set to zero. 40 41 Args : 42 - serie : list or numpy array containing the serie. 43 - wLen : Length of the analyzis window in samples. 44 - withLog : Whether compute the var() or log(var()) . 45 46 Returns : 47 - modul : Modulation of the serie. 48 49 ''' 50 51 modul = numpy.zeros((1,len(serie)))[0]; 52 w = int(wLen/2) 53 54 for i in range(w,len(serie)-w): 55 56 d = serie[i-w:i+w] 57 if withLog: 58 d = numpy.log(d) 59 modul[i] = numpy.var(d) 60 61 modul[:w] = modul[w] 62 63 modul[-w:] = modul[-w-1] 64 65 return modul;
66
67 -def segmentFromValues(values,offset=0):
68 ''' 69 70 ''' 71 72 seg = [offset,-1,values[0]] 73 segList = [] 74 for i,v in enumerate(values) : 75 76 if not (v == seg[2]) : 77 seg[1] = i+offset-1 78 segList.append(tuple(seg)) 79 seg = [i+offset,-1,v] 80 81 seg[1] = i+offset 82 segList.append(tuple(seg)) 83 84 return segList
85 86 87 # Attention 88 # --------- 89 # 90 # Double emploi avec le calcul mfcc d'aubio. Voir pour la fusion... 91 # Maxime 92
93 -def melFilterBank(nbFilters,fftLen,sr) :
94 ''' 95 Grenerate a Mel Filter-Bank 96 97 Args : 98 - nbFilters : Number of filters. 99 - fftLen : Length of the frequency range. 100 - sr : Sampling rate of the signal to filter. 101 Returns : 102 - filterbank : fftLen x nbFilters matrix containing one filter by column. 103 The filter bank can be applied by matrix multiplication 104 (Use numpy *dot* function). 105 ''' 106 107 fh = float(sr)/2.0 108 mh = 2595*numpy.log10(1+fh/700) 109 110 step = mh/nbFilters; 111 112 mcenter = numpy.arange(step,mh,step) 113 114 fcenter = 700*(10**(mcenter/2595)-1) 115 116 filterbank = numpy.zeros((fftLen,nbFilters)); 117 118 for i,_ in enumerate(fcenter) : 119 120 if i == 0 : 121 fmin = 0.0 122 else : 123 fmin = fcenter[i-1] 124 125 if i == len(fcenter)-1 : 126 fmax = fh 127 else : 128 fmax = fcenter[i+1] 129 130 imin = numpy.ceil(fmin/fh*fftLen) 131 imax = numpy.ceil(fmax/fh*fftLen) 132 133 filterbank[imin:imax,i] = triangle(imax-imin) 134 135 return filterbank
136 137
138 -def triangle(length):
139 ''' 140 Generate a triangle filter. 141 142 Args : 143 - length : length of the filter. 144 returns : 145 - triangle : triangle filter. 146 147 ''' 148 triangle = numpy.zeros((1,length))[0] 149 climax= numpy.ceil(length/2) 150 151 triangle[0:climax] = numpy.linspace(0,1,climax) 152 triangle[climax:length] = numpy.linspace(1,0,length-climax) 153 return triangle
154 155
156 -def entropy(serie,nbins=10,base=numpy.exp(1),approach='unbiased'):
157 ''' 158 Compute entropy of a serie using the histogram method. 159 160 Args : 161 - serie : Serie on witch compute the entropy 162 - nbins : Number of bins of the histogram 163 - base : Base used for normalisation 164 - approach : String in the following set : {unbiased,mmse} 165 for un-biasing value. 166 167 Returns : 168 - estimate : Entropy value 169 - nbias : N-bias of the estimate 170 - sigma : Estimated standard error 171 172 Raises : 173 A warning in case of unknown 'approach' value. 174 No un-biasing is then performed 175 176 ''' 177 178 estimate = 0 179 sigma = 0 180 bins,edges = numpy.histogram(serie,nbins); 181 ncell = len(bins) 182 norm = (numpy.max(edges)-numpy.min(edges))/len(bins) 183 184 185 for b in bins : 186 if b == 0 : 187 logf = 0 188 else : 189 logf = numpy.log(b) 190 estimate = estimate - b*logf 191 sigma = sigma + b * logf**2 192 193 count = numpy.sum(bins) 194 estimate=estimate/count; 195 sigma=numpy.sqrt( (sigma/count-estimate**2)/float(count-1) ); 196 estimate=estimate+numpy.log(count)+numpy.log(norm); 197 nbias=-(ncell-1)/(2*count); 198 199 if approach =='unbiased' : 200 estimate=estimate-nbias; 201 nbias=0; 202 203 elif approach =='mmse' : 204 estimate=estimate-nbias; 205 nbias=0; 206 lambda_value=estimate^2/(estimate^2+sigma^2); 207 nbias =(1-lambda_value)*estimate; 208 estimate=lambda_value*estimate; 209 sigma =lambda_value*sigma; 210 else : 211 return 0 212 213 estimate=estimate/numpy.log(base); 214 nbias =nbias /numpy.log(base); 215 sigma =sigma /numpy.log(base); 216 return estimate
217