def harmonicModelAnal(x, fs, w, N, H, t, nH, minf0, maxf0, f0et, harmDevSlope=0.01, minSineDur=0.02): """ Analysis of a sound using the sinusoidal harmonic model x: input sound; fs: sampling rate, w: analysis window; N: FFT size (minimum 512); t: threshold in negative dB, nH: maximum number of harmonics; minf0: minimum f0 frequency in Hz, maxf0: maximim f0 frequency in Hz; f0et: error threshold in the f0 detection (ex: 5), harmDevSlope: slope of harmonic deviation; minSineDur: minimum length of harmonics returns xhfreq, xhmag, xhphase: harmonic frequencies, magnitudes and phases """ if minSineDur < 0: # raise exception if minSineDur is smaller than 0 raise ValueError("Minimum duration of sine tracks smaller than 0") hN = N / 2 # size of positive spectrum hM1 = int(math.floor((w.size + 1) / 2)) # half analysis window size by rounding hM2 = int(math.floor(w.size / 2)) # half analysis window size by floor x = np.append(np.zeros(hM2), x) # add zeros at beginning to center first window at sample 0 x = np.append(x, np.zeros(hM2)) # add zeros at the end to analyze last sample pin = hM1 # init sound pointer in middle of anal window pend = x.size - hM1 # last sample to start a frame fftbuffer = np.zeros(N) # initialize buffer for FFT w = w / sum(w) # normalize analysis window hfreqp = [] # initialize harmonic frequencies of previous frame f0t = 0 # initialize f0 track f0stable = 0 # initialize f0 stable while pin <= pend: x1 = x[pin - hM1 : pin + hM2] # select frame mX, pX = DFT.dftAnal(x1, w, N) # compute dft ploc = UF.peakDetection(mX, t) # detect peak locations iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc) # refine peak values ipfreq = fs * iploc / N # convert locations to Hz f0t = UF.f0Twm(ipfreq, ipmag, f0et, minf0, maxf0, f0stable) # find f0 if ((f0stable == 0) & (f0t > 0)) or ((f0stable > 0) & (np.abs(f0stable - f0t) < f0stable / 5.0)): f0stable = f0t # consider a stable f0 if it is close to the previous one else: f0stable = 0 hfreq, hmag, hphase = harmonicDetection( ipfreq, ipmag, ipphase, f0t, nH, hfreqp, fs, harmDevSlope ) # find harmonics hfreqp = hfreq if pin == hM1: # first frame xhfreq = np.array([hfreq]) xhmag = np.array([hmag]) xhphase = np.array([hphase]) else: # next frames xhfreq = np.vstack((xhfreq, np.array([hfreq]))) xhmag = np.vstack((xhmag, np.array([hmag]))) xhphase = np.vstack((xhphase, np.array([hphase]))) pin += H # advance sound pointer xhfreq = SM.cleaningSineTracks(xhfreq, round(fs * minSineDur / H)) # delete tracks shorter than minSineDur return xhfreq, xhmag, xhphase
def harmonicModelAnal(x, fs, w, N, H, t, nH, minf0, maxf0, f0et, harmDevSlope=0.01, minSineDur=.02): """ Analysis of a sound using the sinusoidal harmonic model x: input sound; fs: sampling rate, w: analysis window; N: FFT size (minimum 512); t: threshold in negative dB, nH: maximum number of harmonics; minf0: minimum f0 frequency in Hz, maxf0: maximim f0 frequency in Hz; f0et: error threshold in the f0 detection (ex: 5), harmDevSlope: slope of harmonic deviation; minSineDur: minimum length of harmonics returns xhfreq, xhmag, xhphase: harmonic frequencies, magnitudes and phases """ if (minSineDur <0): # raise exception if minSineDur is smaller than 0 raise ValueError("Minimum duration of sine tracks smaller than 0") hN = N/2 # size of positive spectrum hM1 = int(math.floor((w.size+1)/2)) # half analysis window size by rounding hM2 = int(math.floor(w.size/2)) # half analysis window size by floor x = np.append(np.zeros(hM2),x) # add zeros at beginning to center first window at sample 0 x = np.append(x,np.zeros(hM2)) # add zeros at the end to analyze last sample pin = hM1 # init sound pointer in middle of anal window pend = x.size - hM1 # last sample to start a frame fftbuffer = np.zeros(N) # initialize buffer for FFT w = w / sum(w) # normalize analysis window hfreqp = [] # initialize harmonic frequencies of previous frame f0t = 0 # initialize f0 track f0stable = 0 # initialize f0 stable while pin<=pend: x1 = x[pin-hM1:pin+hM2] # select frame mX, pX = DFT.dftAnal(x1, w, N) # compute dft ploc = UF.peakDetection(mX, t) # detect peak locations iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc) # refine peak values ipfreq = fs * iploc/N # convert locations to Hz f0t = UF.f0Twm(ipfreq, ipmag, f0et, minf0, maxf0, f0stable) # find f0 if ((f0stable==0)&(f0t>0)) \ or ((f0stable>0)&(np.abs(f0stable-f0t)<f0stable/5.0)): f0stable = f0t # consider a stable f0 if it is close to the previous one else: f0stable = 0 hfreq, hmag, hphase = harmonicDetection(ipfreq, ipmag, ipphase, f0t, nH, hfreqp, fs, harmDevSlope) # find harmonics hfreqp = hfreq if pin == hM1: # first frame xhfreq = np.array([hfreq]) xhmag = np.array([hmag]) xhphase = np.array([hphase]) else: # next frames xhfreq = np.vstack((xhfreq,np.array([hfreq]))) xhmag = np.vstack((xhmag, np.array([hmag]))) xhphase = np.vstack((xhphase, np.array([hphase]))) pin += H # advance sound pointer xhfreq = SM.cleaningSineTracks(xhfreq, round(fs*minSineDur/H)) # delete tracks shorter than minSineDur return xhfreq, xhmag, xhphase
def sineModelMultiResAnal(x, fs, ws, Ns, Bs, H, t, minSineDur=0.02, maxnSines=150, freqDevOffset=10, freqDevSlope=0.001): hsN = H/2 pend = x.size pin = max([hsN] + [int(math.floor((w.size+1)/2)) for w in ws]) x = np.append(np.zeros(pin),x) x = np.append(x,np.zeros(pin)) def dftAnal(p, w, N, B): hM1 = int(math.floor((w.size+1)/2)) hM2 = int(math.floor(w.size/2)) x1 = x[p-hM1:p+hM2] fftbuffer = np.zeros(N) rw = w / sum(w) mX, pX = DFT.dftAnal(x1, rw, N) upperIndex = Bs.index(B) lower_bin = 1 if upperIndex > 0: lower_bin = int(np.ceil(float(Bs[upperIndex-1])*N/fs)) upper_bin = int(np.ceil(float(B)*N/fs)) ploc = UF.peakDetection(mX, t) # Peak choice ploc = ploc[np.logical_and(ploc > lower_bin, ploc <= upper_bin)] iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc) ipfreq = fs*iploc/float(N) return (ipfreq, ipmag, ipphase) xtfreq = np.array([]) xtmag = np.array([]) xtphase = np.array([]) tfreq = np.array([]) while pin <= pend: pfs = np.array([]) pms = np.array([]) pps = np.array([]) for i, w in enumerate(ws): pf, pm, pp = dftAnal(pin, w, Ns[i], Bs[i]) pfs = np.concatenate((pfs, pf)) pms = np.concatenate((pms, pm)) pps = np.concatenate((pps, pp)) tfreq, tmag, tphase = SM.sineTracking(pfs, pms, pps, tfreq, freqDevOffset, freqDevSlope) tfreq = np.resize(tfreq, min(maxnSines, tfreq.size)) tmag = np.resize(tmag, min(maxnSines, tmag.size)) tphase = np.resize(tphase, min(maxnSines, tphase.size)) jtfreq = np.zeros(maxnSines) jtmag = np.zeros(maxnSines) jtphase = np.zeros(maxnSines) jtfreq[:tfreq.size]=tfreq jtmag[:tmag.size]=tmag jtphase[:tphase.size]=tphase if xtfreq.size == 0: xtfreq = jtfreq xtmag = jtmag xtphase = jtphase else: xtfreq = np.vstack((xtfreq, jtfreq)) xtmag = np.vstack((xtmag, jtmag)) xtphase = np.vstack((xtphase, jtphase)) pin += H xtfreq = SM.cleaningSineTracks(xtfreq, round(fs*minSineDur/H)) return xtfreq, xtmag, xtphase
def sineModelMultiResAnal(x, fs, Ws, Ns, Bs, H, t, maxnSines = 100, minSineDur=.01, freqDevOffset=20, freqDevSlope=0.01): """ Analysis of a sound using the sinusoidal model with sine tracking x: input array sound, w: analysis window, Ns: size of complex spectrums, H: hop-size, Bs: band edges, t: threshold in negative dB, maxnSines: maximum number of sines per frame, minSineDur: minimum duration of sines in seconds, freqDevOffset: minimum frequency deviation at 0Hz, freqDevSlope: slope increase of minimum frequency deviation returns xtfreq, xtmag, xtphase: frequencies, magnitudes and phases of sinusoidal tracks """ if (minSineDur <0): # raise error if minSineDur is smaller than 0 raise ValueError("Minimum duration of sine tracks smaller than 0") hsN = H/2 pend = x.size pin = max([hsN] + [int(math.floor((w.size+1)/2)) for w in Ws]) x = np.append(np.zeros(pin), x) x = np.append(x, np.zeros(pin)) def dftAnalMultiRes(p, w, N, B): hM1 = int(math.floor((w.size+1)/2)) # half analysis window size by rounding hM2 = int(math.floor(w.size/2)) # half analysis window size by floor x1 = x[pin-hM1:pin+hM2] # select frame fftbuffer = np.zeros(N) rw = w / sum(w) # normalize analysis window mX, pX = DFT.dftAnal(x1, w, N) # compute dft upper_index = Bs.index(B) if upper_index > 0: lower_bin = int(np.ceil(float(Bs[upper_index-1]) * N / fs)) else: lower_bin = 1 upper_bin = int(np.ceil(float(B) * N / fs)) ploc = UF.peakDetection(mX, t) # detect locations of peaks ploc = ploc[np.logical_and(ploc>lower_bin, ploc<=upper_bin)] # choose the peaks in band iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc) # refine peak values by interpolation ipfreq = fs*iploc/float(N) # convert peak locations to Hertz return (ipfreq, ipmag, ipphase) xtfreq = np.array([]) xtmag = np.array([]) xtphase = np.array([]) tfreq = np.array([]) while pin<pend: # while input sound pointer is within sound pfs = np.array([]) pms = np.array([]) pps = np.array([]) for i, w in enumerate(Ws): pf, pm, pp = dftAnalMultiRes(pin, w, Ns[i], Bs[i]) pfs = np.concatenate((pfs, pf)) pms = np.concatenate((pms, pm)) pps = np.concatenate((pps, pp)) # perform sinusoidal tracking by adding peaks to trajectories tfreq, tmag, tphase = SM.sineTracking(pfs, pms, pps, tfreq, freqDevOffset, freqDevSlope) tfreq = np.resize(tfreq, min(maxnSines, tfreq.size)) # limit number of tracks to maxnSines tmag = np.resize(tmag, min(maxnSines, tmag.size)) # limit number of tracks to maxnSines tphase = np.resize(tphase, min(maxnSines, tphase.size)) # limit number of tracks to maxnSines jtfreq = np.zeros(maxnSines) # temporary output array jtmag = np.zeros(maxnSines) # temporary output array jtphase = np.zeros(maxnSines) # temporary output array jtfreq[:tfreq.size]=tfreq # save track frequencies to temporary array jtmag[:tmag.size]=tmag # save track magnitudes to temporary array jtphase[:tphase.size]=tphase # save track magnitudes to temporary array if xtfreq.size == 0: # if first frame initialize output sine tracks xtfreq = jtfreq xtmag = jtmag xtphase = jtphase else: # rest of frames append values to sine tracks xtfreq = np.vstack((xtfreq, jtfreq)) xtmag = np.vstack((xtmag, jtmag)) xtphase = np.vstack((xtphase, jtphase)) pin += H # delete sine tracks shorter than minSineDur xtfreq = SM.cleaningSineTracks(xtfreq, round(fs*minSineDur/H)) return xtfreq, xtmag, xtphase
def harmonicFromCsv(infile, fs, w, N, H, t, harmDevSlope=0.01, minSineDur=.1): o = open("/Users/backup/Desktop/sms output.txt", "w") #Analysis of a sound using the sinusoidal harmonic model #x: input sound; fs: sampling rate, w: analysis window; N: FFT size (minimum 512); t: threshold in negative dB, #nH: maximum number of harmonics; minf0: minimum f0 frequency in Hz, #maxf0: maximim f0 frequency in Hz; f0et: error threshold in the f0 detection (ex: 5), #harmDevSlope: slope of harmonic deviation; minSineDur: minimum length of harmonics #returns xhfreq, xhmag, xhphase: harmonic frequencies, magnitudes and phases if (minSineDur < 0): # raise exception if minSineDur is smaller than 0 raise ValueError("Minimum duration of sine tracks smaller than 0") hN = N / 2 # size of positive spectrum hM1 = int(math.floor( (w.size + 1) / 2)) # half analysis window size by rounding hM2 = int(math.floor(w.size / 2)) # half analysis window size by floor #x = np.append(np.zeros(hM2),x) # add zeros at beginning to center first window at sample 0 #x = np.append(x,np.zeros(hM2)) # add zeros at the end to analyze last sample pin = hM1 # init sound pointer in middle of anal window #pend = x.size - hM1 # last sample to start a frame fftbuffer = np.zeros(N) # initialize buffer for FFT w = w / sum(w) # normalize analysis window hfreqp = [] # initialize harmonic frequencies of previous frame f0t = 0 # initialize f0 track f0stable = 0 # initialize f0 stable #print "Enter modified file: " #file_one = askopenfilename() in_file = csv.reader(open(infile, "rU")) #, dialect=csv.excel_tab) hfreq = [] hmag = [] hphase = [] for row in in_file: yu = len(row) - 1 y = yu / 3 dfreq = [] dmag = [] dphase = [] col = 0 for i in range(yu + 1): if i == 0: pass elif 0 < i <= y: dfreq.append(float(row[i])) elif y < i <= (2 * y): dmag.append(float(row[i])) elif (2 * y) < i <= yu + 1: dphase.append(float(row[i])) hfreq.append(dfreq) hmag.append(dmag) hphase.append(dphase) print "Sound length in frames:...." + str(len(hfreq)) bar_length = 40 end_val = len(hfreq) for q in range(end_val): percent = float(q) / end_val hashes = '#' * int(round(percent * bar_length)) spaces = ' ' * (bar_length - len(hashes)) sys.stdout.write("\r[{0}] {1}% Reading sound".format( hashes + spaces, int(round(percent * 100)))) sys.stdout.flush() time.sleep(0.0001) hfreqp = hfreq if pin == hM1: # first frame xhfreq = np.array(hfreq[q]) flistlen = len(hfreq[q]) xhmag = np.array(hmag[q]) mlistlen = len(hmag[q]) xhphase = np.array(hphase[q]) plistlen = len(hphase[q]) else: # next frames hfreq[q] = hfreq[q][:flistlen] while len(hfreq[q]) < flistlen: #print q hfreq[q].append(0) #print "adding zeroes" hmag[q] = hmag[q][:mlistlen] while len(hmag[q]) < mlistlen: #print q hmag[q].append(0) #print "adding zeroes" hphase[q] = hphase[q][:plistlen] while len(hphase[q]) < plistlen: #print q hphase[q].append(0) #print "adding zeroes" xhfreq = np.vstack((xhfreq, np.array(hfreq[q]))) xhmag = np.vstack((xhmag, np.array(hmag[q]))) xhphase = np.vstack((xhphase, np.array(hphase[q]))) pin += H #q += 1 print "" o.write(xhfreq) # advance sound pointer xhfreq = SM.cleaningSineTracks(xhfreq, round( fs * minSineDur / H)) # delete tracks shorter than minSineDur return xhfreq, xhmag, xhphase, end_val
def sinetracks(ffts, phs, w, init, x, H, t, N, fs, freqDevOffset, freqDevSlope, maxnSines, minSineDur): # CREATE SINE TRACKS mXs = ffts[init:, :] pXs = phs[init:, :] if (minSineDur < 0): # raise error if minSineDur is smaller than 0 raise ValueError("Minimum duration of sine tracks smaller than 0") hM1 = int(math.floor( (w.size + 1) / 2)) # half analysis window size by rounding hM2 = int(math.floor(w.size / 2)) # half analysis window size by floor x = np.append( np.zeros(hM2), x) # add zeros at beginning to center first window at sample 0 x = np.append(x, np.zeros(hM2)) # add zeros at the end to analyze last sample pin = hM1 # initialize sound pointer in middle of analysis window pend = x.size - hM1 # last sample to start a frame w = w / sum(w) # normalize analysis window tfreq = np.array([]) for i in range(mXs.shape[0]): mX = mXs[i, :] pX = pXs[i, :] ploc = UF.peakDetection(mX, t) # detect locations of peaks iploc, ipmag, ipphase = UF.peakInterp( mX, pX, ploc) # refine peak values by interpolation ipfreq = fs * iploc / float(N) # convert peak locations to Hertz # perform sinusoidal tracking by adding peaks to trajectories tfreq, tmag, tphase = sineTracking(ipfreq, ipmag, ipphase, tfreq, freqDevOffset, freqDevSlope) tfreq = np.resize(tfreq, min( maxnSines, tfreq.size)) # limit number of tracks to maxnSines tmag = np.resize(tmag, min(maxnSines, tmag.size)) # limit number of tracks to maxnSines tphase = np.resize(tphase, min( maxnSines, tphase.size)) # limit number of tracks to maxnSines jtfreq = np.zeros(maxnSines) # temporary output array jtmag = np.zeros(maxnSines) # temporary output array jtphase = np.zeros(maxnSines) # temporary output array jtfreq[:tfreq. size] = tfreq # save track frequencies to temporary array jtmag[:tmag.size] = tmag # save track magnitudes to temporary array jtphase[:tphase. size] = tphase # save track magnitudes to temporary array if pin == hM1: # if first frame initialize output sine tracks xtfreq = jtfreq xtmag = jtmag xtphase = jtphase else: # rest of frames append values to sine tracks xtfreq = np.vstack((xtfreq, jtfreq)) xtmag = np.vstack((xtmag, jtmag)) xtphase = np.vstack((xtphase, jtphase)) pin += H # delete sine tracks shorter than minSineDur xtfreq = cleaningSineTracks(xtfreq, round(fs * minSineDur / H)) return xtmag, xtfreq, xtphase