def MCC_calc(dataPath_true, dataPath_pred, NFFT): STFT_OVERLAP = 0.5 feat_pred, fs = utils.wavToSamples(dataPath_pred) #print(len(feat_pred)) feat_true, fs = utils.wavToSamples(dataPath_true) #print(len(feat_true)) lenPred = len(feat_pred) lenRef = len(feat_true) if lenRef < lenPred: feat_true = np.concatenate((feat_true, np.zeros(lenPred - lenRef))) elif lenPred < lenRef: feat_pred = np.concatenate((feat_pred, np.zeros(lenRef - lenPred))) feat_pred, _, _, _ = utils.STFT(feat_pred, fs, NFFT, int(NFFT * STFT_OVERLAP)) feat_pred = np.float32(feat_pred) feat_true, _, _, _ = utils.STFT(feat_true, fs, NFFT, int(NFFT * STFT_OVERLAP)) feat_true = np.float32(feat_true) #feat_true = np.array(feat_true) #feat_pred = np.array(feat_pred) # Calculate the spectral MCC xx_true = np.subtract(feat_true, np.mean(feat_true, 1).reshape(feat_true.shape[0], 1)) denom_true = np.sqrt(np.sum(xx_true**2, 1)).reshape(feat_true.shape[0], 1) xx_true = np.divide(xx_true, denom_true) xx_pred = np.subtract(feat_pred, np.mean(feat_pred, 1).reshape(feat_pred.shape[0], 1)) denom_pred = np.sqrt(np.sum(xx_pred**2, 1)).reshape(feat_pred.shape[0], 1) xx_pred = np.divide(xx_pred, denom_pred) segMCC = np.sum(np.multiply(xx_true, xx_pred), 1) segMCC = segMCC.reshape(feat_pred.shape[0], 1) MCC = np.mean(segMCC) freq = np.linspace(0, fs / 2, np.size(segMCC, 0)) # plt.plot(freq,segMCC, linewidth=2) # plt.xlabel("Frequency [Hz]") # plt.ylabel("Magnitude Correlation") # plt.title("MCC") # plt.grid(linestyle='--') return MCC, segMCC, freq
def featureExtraction(filePathFeat, AUDIO_dB_SPL, NFFT, STFT_OVERLAP, numBin, featMean, featStd): # Feature Extraction function # filePathFeat : filepath name [string] # AUDIO_dB_SPL : dB SPL value to adjust SNR level # NFFT : Number of frequency bins # STFT_OVERLAP : frame overlab [0 - 1] # numBin : number of frequency bins you want to use. [0 Hz - ..] # featMean : features mean value for standardization # featStd : features standard deviation value for standardization x, fs = utils.wavToSamples(filePathFeat) x = utils.adjustSNR(x, AUDIO_dB_SPL) features, feature_phi, _, _ = utils.STFT(x, fs, NFFT, int(NFFT * STFT_OVERLAP)) features = np.float32(features) features = np.log10(features + 1e-7) features = features - featMean features = features / featStd features = np.transpose(features) features = features[:, 0:numBin] return features, feature_phi
def calcMeanAndStd(dataPath,NUM_FILES,NFFT,STFT_OVERLAP,BIN_WIZE): allFiles = os.listdir(dataPath) allFiles = allFiles[:NUM_FILES] #averageOld = 0 #stdOld = 0 count = 0 featMean = 0 featStd = 0 for file in allFiles: count += 1 filePath = dataPath + file x, fs = utils.wavToSamples(filePath) x = utils.adjustSNR(x,60) features,feature_phi,_,_ = utils.STFT(x,fs,NFFT,int(NFFT*STFT_OVERLAP)) features = np.float32(features) features = np.log10(features + 1e-7) if BIN_WIZE: featMean = np.asarray(np.mean(features,1)) featStd = np.asarray(np.std(features,1)) else: featMean = featMean + np.mean(features) featStd = featStd + np.std(features) #print((featMean + averageOld)/count) #averageOld = averageOld + (featMean + averageOld)/count #print(averageOld) #stdOld = stdOld + (featStd + stdOld)/count featMean = featMean/count featStd = featStd/count #averageNew = averageOld #stdNew = stdOld return featMean, featStd
x = x[0:int(fs * 6)] #x = signal.decimate(x,3) #fs = 16000 #x = x/np.max(np.abs(x)) fs2, x_ref = wavfile.read(filePath2) #x_ref, fs2 = utils.wavToSamples(filePath2) x_ref = x_ref[0:int(fs2 * 6)] #x_ref = signal.decimate(x_ref,3) #fs2 = 16000 #x_ref = x_ref/np.max(np.abs(x_ref)) wL = 512 features, X_phi, t, f = utils.STFT(x, fs, wL, int(wL * 0.75)) features.shape X_phi.shape labels, L_phi, t2, f2 = utils.STFT(x_ref, fs2, wL, int(wL * 0.75)) labels.shape #### MODEL #### features = np.float32(features) labels = np.float32(labels) #cond1 = np.std(features,axis=1) > 0 #features = features[cond1,:] #cond2 = features == 0 #features[cond2] += np.finfo(float).eps