def extractFeatures(input_signal): """extract features from the cleaned signal. :param cleaned signal :return: features list""" # compute mfcc list if len(input_signal) == 0: print("cleaned signal is empty") return input_signal mfcc_list = np.array(mfcc(input_signal, samplerate=prm.params["sample_rate"].get(), winlen=0.032, winstep=0.016, numcep=30, nfilt=55, nfft=2048, lowfreq=0, highfreq=6000, preemph=0.95, ceplifter=22, appendEnergy=True )) extractor = LPCExtractor(prm.params["sample_rate"].get(), 32, 16, 30, 0.95) lpcc = extractor.extract(input_signal) pitch = extract_pitch(input_signal) # Cepstral Mean Normalization @TODO: WHY IS THIS NOT HELPING?? if 0: mean_mfcc = np.mean(mfcc_list.T, 1) std_mfcc = np.std(mfcc_list.T, 1) for i in range(len(mfcc_list)): for j in range(len(mfcc_list[i])): mfcc_list[i][j] = (mfcc_list[i][j]-mean_mfcc[j])/std_mfcc[j] # print np.shape(mfcc_list[i-1]), np.shape(mean_mfcc), np.shape(std_mfcc) N = 2 delta_list = delta(mfcc_list, N) ddelta_list = delta(delta_list, N) # do not keep first coeff (energy) features_list = list() for k in range(len(mfcc_list)): # features_list += [np.hstack((mfcc_list[k][0:], lpcc[k][0:]))] features_list += [mfcc_list[k][0:]] # features_list += [lpcc[k][0:]] # features_list += [np.hstack((mfcc_list[k][0:], delta_list[k][0:], ddelta_list[k][0:]))] # print np.shape(mfcc_list), np.shape(features_list) # dont return nan # @TODO WHY DOES THIS HAPPEN? for row in features_list: for cell in row: if cell != cell: print "Cell is nan (see feature extraction):", str(cell) return [] full_features_list = [] full_features_list = list(np.ravel(features_list)) full_features_list.extend([pitch]*30) # do we need to append this multiple times to ensure that the forest selects it? return full_features_list
def comp_feat(self, sig, rate): ''' compute the features Args: sig: the audio signal as a 1-D numpy array rate: the sampling rate Returns: the features as a [seq_length x feature_dim] numpy array ''' #snip the edges sig = snip(sig, rate, float(self.conf['winlen']), float(self.conf['winstep'])) feat, energy = base.mfcc(sig, rate, self.conf) if self.conf['include_energy'] == 'True': feat = np.append(feat, energy[:, np.newaxis], 1) if self.conf['dynamic'] == 'delta': feat = base.delta(feat) elif self.conf['dynamic'] == 'ddelta': feat = base.ddelta(feat) elif self.conf['dynamic'] != 'nodelta': raise Exception('unknown dynamic type') return feat
def comp_feat(self, sig, rate): ''' compute the features Args: sig: the audio signal as a 1-D numpy array rate: the sampling rate Returns: the features as a [seq_length x feature_dim] numpy array ''' feat, energy = base.logfbank(sig, rate, self.conf) if self.conf['include_energy'] == 'True': feat = np.append(feat, energy[:, np.newaxis], 1) if self.conf['dynamic'] == 'delta': feat = base.delta(feat) elif self.conf['dynamic'] == 'ddelta': feat = base.ddelta(feat) elif self.conf['dynamic'] != 'nodelta': raise Exception('unknown dynamic type') #mean and variance normalize the features if self.conf['mvn'] == 'True': feat = (feat - feat.mean(0)) / feat.std(0) return feat
def main(): auth = login(url, user, password) host_group = {} cc = 0 if auth: file = open(path,'w') file.write(','.join(['Severity','Last change','Age','Host','Name','History Alerts Num','Groups','Templates'])+'\n') params = { #"output": ["triggerid", "status", "type","value", "description", "templateid"], "output": "extend", "only_true": True, #Return only triggers that have recently been in a problem state. "monitored": True, #Return only enabled triggers that belong to monitored hosts and contain only enabled items. "selectItems": "extend", "selectHosts": ["hostid","status","name"], "selectGroups":["name"], "selectLastEvent": "extend", #"filter": { # "trigger": trigger_list, # } } triggers = trigger_get(url, auth, params) triggers.sort(key=lambda x:x['lastchange'], reverse=True) for trigger in triggers: if trigger['value'] == '1' and trigger['status'] == '0': cc += 1 #print trigger #itemids = [item['itemid'] for item in trigger['items']] #print itemids ''' sub_triggers = trigger_get(auth, { "output": "extend", "itemids" : itemids, }) if len(sub_triggers) > 1: print sub_triggers ''' events = event_get(url, auth, { "output": "extend", "triggerids" : [trigger['triggerid']], "value": "1", }) templates = template_get(url, auth, { "output": "extend", "hostids": trigger['hosts'][0]['hostid'], }) print trigger['hosts'][0]['name'], datetime.now() - trigger['lastchange'], delta(datetime.now() - trigger['lastchange']) res = [trigger['priority'].capitalize(), trigger['lastchange'], delta(datetime.now() - trigger['lastchange']), trigger['hosts'][0]['name'], trigger['description'].replace('{HOST.NAME}',trigger['hosts'][0]['name']), len(events)-1, '/'.join([g['name'] for g in trigger['groups']]), '/'.join([t['name'] for t in templates])] line = ','.join([not isinstance(r,str) and str(r) or r for r in res]) file.write(line.decode("utf-8").encode('GBK')+'\n') file.flush() file.close() #print(cc) return cc
def comp_feat(self, sig, rate): ''' compute the features Args: sig: the audio signal as a 1-D numpy array rate: the sampling rate Returns: the features as a [seq_length x feature_dim] numpy array ''' feat, energy = base.mfcc(sig, rate, self.conf) # write the wav to temporary location and invoke external pitch extractor. # make sure 'reaper' is in your $PATH tempdir = os.path.join('/tmp', str(os.getpid())) if not os.path.isdir(tempdir): os.makedirs(tempdir) name = 'mix' wav.write(os.path.join(tempdir, name + '.wav'), rate, np.int16(sig)) os.system('reaper -i ' + os.path.join(tempdir, name + '.wav') + ' -f ' + os.path.join(tempdir, name + '.txt -a -e 0.01')) pitch = np.loadtxt(os.path.join(tempdir, name + '.txt'), skiprows=7)[:, 2] pitch = np.pad(pitch, (0, max(0, feat.shape[0] - pitch.shape[0])), 'edge') # linear interpolation in voiceless regions voiceless = np.where(pitch == -1)[0] jump = np.where((voiceless[1:] - voiceless[:-1]) > 1)[0] segments = np.split(voiceless, jump + 1) for seg in segments: idx1 = seg[0] - 1 idx2 = seg[-1] + 1 val1 = -1 val2 = -1 if idx1 >= 0: val1 = pitch[idx1] if idx2 < pitch.size: val2 = pitch[idx2] if val1 == -1: #segment starts at utterence start val1 = val2 if val2 == -1: # segment ends at utterance end val2 = val1 if val1 == -1: #segment is the whole utterance => make up a value val1 = 150 val2 = 150 #interpolate pitch[seg] = (val2 - val1) * (np.array(seg) - idx1) / float(idx2 - idx1) + val1 feat = np.append(feat, pitch[:feat.shape[0], np.newaxis], 1) if self.conf['include_energy'] == 'True': feat = np.append(feat, energy[:, np.newaxis], 1) if self.conf['dynamic'] == 'delta': feat = base.delta(feat) elif self.conf['dynamic'] == 'ddelta': feat = base.ddelta(feat) elif self.conf['dynamic'] != 'nodelta': raise Exception('unknown dynamic type') #mean and variance normalize the features if self.conf['mvn'] == 'True': feat = (feat - feat.mean(0)) / ( feat.std(0) + 1e-20 ) # features could be constant, e.g. voiceless speech return feat
# you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== #!/usr/bin/env python ''' Example for sigproc.py ''' # pylint: skip-file import scipy.io.wavfile as wav from base import mfcc from base import delta from base import logfbank if __name__ == '__main__': (rate, sig) = wav.read("english.wav") mfcc_feat = mfcc(sig, rate) d_mfcc_feat = delta(mfcc_feat, 2) fbank_feat = logfbank(sig, rate) print(fbank_feat[1:3, :])