comparison Example VamPy plugins/PySpectralFeatures.py @ 26:ba3686eb697c

examples now pass all tests
author fazekasgy
date Wed, 19 Aug 2009 15:21:17 +0000
parents 7d28bed0864e
children
comparison
equal deleted inserted replaced
25:7648f3f2fa14 26:ba3686eb697c
8 def __init__(self,inputSampleRate): 8 def __init__(self,inputSampleRate):
9 self.m_inputSampleRate = inputSampleRate 9 self.m_inputSampleRate = inputSampleRate
10 self.m_stepSize = 0 10 self.m_stepSize = 0
11 self.m_blockSize = 0 11 self.m_blockSize = 0
12 self.m_channels = 0 12 self.m_channels = 0
13 self.threshold = 0.00 13 self.threshold = 0.05
14 self.r = 2.0 14 self.r = 2.0
15 return None
15 16
16 def initialise(self,channels,stepSize,blockSize): 17 def initialise(self,channels,stepSize,blockSize):
17 self.m_channels = channels 18 self.m_channels = channels
18 self.m_stepSize = stepSize 19 self.m_stepSize = stepSize
19 self.m_blockSize = blockSize 20 self.m_blockSize = blockSize
20 #self.prevMag = ones((blockSize/2)-1) / ((blockSize/2)-1)
21 self.prevMag = zeros((blockSize/2)-1) 21 self.prevMag = zeros((blockSize/2)-1)
22 self.prevMag[0] = 1 22 self.prevMag[0] = 1
23
24 return True 23 return True
24
25 def reset(self):
26 # reset any initial conditions
27 self.prevMag = zeros((self.m_blockSize/2)-1)
28 self.prevMag[0] = 1
29 return None
25 30
26 def getMaker(self): 31 def getMaker(self):
27 return 'VamPy Example Plugins' 32 return 'VamPy Example Plugins'
28 33
29 def getName(self): 34 def getName(self):
107 112
108 def getParameterDescriptors(self): 113 def getParameterDescriptors(self):
109 threshold={ 114 threshold={
110 'identifier':'threshold', 115 'identifier':'threshold',
111 'name':'Noise threshold: ', 116 'name':'Noise threshold: ',
112 'description':'', 117 'description':'Noise threshold',
113 'unit':'v', 118 'unit':'v',
114 'minValue':0.0, 119 'minValue':0.0,
115 'maxValue':0.5, 120 'maxValue':0.5,
116 'defaultValue':0.05, 121 'defaultValue':0.05,
117 'isQuantized':False 122 'isQuantized':False
118 } 123 }
119 124
120 renyicoeff={ 125 renyicoeff={
121 'identifier':'r', 126 'identifier':'r',
122 'name':'Renyi entropy coeff: ', 127 'name':'Renyi entropy coeff: ',
123 'description':'', 128 'description':'Renyi entropy coeff',
124 'unit':'', 129 'unit':'',
125 'minValue':0.0, 130 'minValue':0.0,
126 'maxValue':10.0, 131 'maxValue':10.0,
127 'defaultValue':2, 132 'defaultValue':2.0,
128 'isQuantized':False 133 'isQuantized':False
129 } 134 }
130 135
131 return [threshold,renyicoeff] 136 return [threshold,renyicoeff]
132 137
149 fftsize = self.m_blockSize 154 fftsize = self.m_blockSize
150 sampleRate = self.m_inputSampleRate 155 sampleRate = self.m_inputSampleRate
151 156
152 #for time domain plugins use the following line: 157 #for time domain plugins use the following line:
153 #audioSamples = frombuffer(membuffer[0],float32) 158 #audioSamples = frombuffer(membuffer[0],float32)
154 #-1: do till the end, skip DC 2*32bit / 8bit = 8byte 159 #for frequency domain plugins use the following line:
155 complexSpectrum = frombuffer(membuffer[0],complex64,-1,8) 160 complexSpectrum = frombuffer(membuffer[0],complex64,-1,8)
161 #meaning of the parameters above:
162 #-1: do until the end, skip DC 2*32bit / 8bit = 8byte
156 magnitudeSpectrum = abs(complexSpectrum) / (fftsize/2) 163 magnitudeSpectrum = abs(complexSpectrum) / (fftsize/2)
157 tpower = sum(magnitudeSpectrum) 164 tpower = sum(magnitudeSpectrum)
158 #phaseSpectrum = angle(complexSpectrum) 165 #phaseSpectrum = angle(complexSpectrum)
159 166
160 freq = array(range(1,len(complexSpectrum)+1)) \ 167 freq = array(range(1,len(complexSpectrum)+1)) \
163 centroid = 0.0 170 centroid = 0.0
164 crest = 0.0 171 crest = 0.0
165 bw = 0.0 172 bw = 0.0
166 shannon = 0.0 173 shannon = 0.0
167 renyi = 0.0 174 renyi = 0.0
168 r = self.r
169 KLdiv = 0.0 175 KLdiv = 0.0
170 flatness = 0.0
171 exp=1.0 / (fftsize/2) 176 exp=1.0 / (fftsize/2)
172 #print exp
173 177
174 #declare outputs 178 #declare outputs
175 output0=[] 179 output0=[]
176 output1=[] 180 output1=[]
177 output2=[] 181 output2=[]
182 if tpower > self.threshold : 186 if tpower > self.threshold :
183 187
184 centroid = sum(freq * magnitudeSpectrum) / tpower 188 centroid = sum(freq * magnitudeSpectrum) / tpower
185 crest = max(magnitudeSpectrum) / tpower 189 crest = max(magnitudeSpectrum) / tpower
186 bw = sum( abs(freq - centroid) * magnitudeSpectrum ) / tpower 190 bw = sum( abs(freq - centroid) * magnitudeSpectrum ) / tpower
187 #flatness = prod(abs(complexSpectrum))
188 #print flatness
189 normMag = magnitudeSpectrum / tpower #make it sum to 1 191 normMag = magnitudeSpectrum / tpower #make it sum to 1
190 shannon = - sum( normMag * log2(normMag) ) 192 shannon = - sum( normMag * log2(normMag) )
191 renyi = (1/1-r) * log10( sum( power(normMag,r))) 193 renyi = (1/1-self.r) * log10( sum( power(normMag,self.r)))
192 KLdiv = sum( normMag * log2(normMag / self.prevMag) ) 194 KLdiv = sum( normMag * log2(normMag / self.prevMag) )
193 self.prevMag = normMag 195 self.prevMag = normMag
194 196
195 output0.append({ 197 output0.append({
196 'hasTimestamp':False, 198 'hasTimestamp':False,
228 #'label':str(renyi) 230 #'label':str(renyi)
229 }) 231 })
230 232
231 #return a LIST of list of dictionaries 233 #return a LIST of list of dictionaries
232 return [output0,output1,output2,output3,output4,output5] 234 return [output0,output1,output2,output3,output4,output5]
235