def stftAnal(x, win, N, H): if (H <= 0): raise ValueError("Hop size too small!") M = win.size pM1 = (M + 1) // 2 pM2 = M // 2 x = np.append(np.zeros(pM2), x) x = np.append(x, np.zeros(pM2)) begin = pM1 # init sound pointer in the middle of analysis window end = x.size - pM1 win = win / sum(win) xmX = [] xphX = [] while begin <= end: x1 = x[begin - pM1:begin + pM2] mX, phX = dft.dftAnal(x1, win, N) xmX.append(np.array(mX)) xphX.append(np.array(phX)) begin += H xmX = np.array(xmX) xphX = np.array(xphX) return xmX, xphX
def harmonicModel(x, fs, w, N, t, nH, minf0, maxf0, f0et): hM1 = int(math.floor((w.size+1)/2)) hM2 = int(math.floor(w.size/2)) x = np.append(np.zeros(hM2),x) x = np.append(x,np.zeros(hM1)) Ns = 512 H = int(Ns/4) hNs = int(Ns/2) begin = max(hNs, hM1) end = x.size - max(hNs, hM1) fftbuffer = np.zeros(N) yh = np.zeros(Ns) y = np.zeros(x.size) w = w / sum(w) sw = np.zeros(Ns) ow = triang(2*H) sw[hNs-H:hNs+H] = ow bh = blackmanharris(Ns) bh = bh / sum(bh) sw[hNs-H:hNs+H] = sw[hNs-H:hNs+H] / bh[hNs-H:hNs+H] hfreqp = [] f0t = 0 f0stable = 0 while begin<end: # analyze x1 = x[begin-hM1:begin+hM2] mX, pX = DFT.dftAnal(x1, w, N) ploc = U.peakDetection(mX, t) iploc, ipmag, ipphase = U.peak_parabolicInterp(mX, pX, ploc) ipfreq = fs * iploc/N f0t = U.f0Twm(ipfreq, ipmag, f0et, minf0, maxf0, f0stable) if ((f0stable==0)&(f0t>0)) \ or ((f0stable>0)&(np.abs(f0stable-f0t)<f0stable/5.0)): f0stable = f0t else: f0stable = 0 hfreq, hmag, hphase = harmonicDetection(ipfreq, ipmag, ipphase, f0t, nH, hfreqp, fs) hfreqp = hfreq # synth Yh = U.genSinesSpectrum(hfreq, hmag, hphase, Ns, fs) fftbuffer = np.real(ifft(Yh)) yh[:hNs-1] = fftbuffer[hNs+1:] yh[hNs-1:] = fftbuffer[:hNs+1] y[begin-hNs:begin+hNs] += sw*yh begin += H y = np.delete(y, range(hM2)) y = np.delete(y, range(y.size-hM1, y.size)) return y
def harmonicModelAnal(x, fs, w, N, H, t, nH, minf0, maxf0, f0et, harmDevSlope=0.01, minSineDur=.02): if (minSineDur <0): raise ValueError("Minimum duration of sine tracks smaller than 0") hM1 = int(math.floor((w.size+1)/2)) hM2 = int(math.floor(w.size/2)) x = np.append(np.zeros(hM2),x) x = np.append(x,np.zeros(hM2)) begin = hM1 end = x.size - hM1 w = w / sum(w) hfreqp = [] f0t = 0 f0stable = 0 while begin<=end: x1 = x[begin-hM1:begin+hM2] mX, pX = DFT.dftAnal(x1, w, N) ploc = U.peakDetection(mX, t) iploc, ipmag, ipphase = U.peak_parabolicInterp(mX, pX, ploc) ipfreq = fs * iploc/N f0t = U.f0Twm(ipfreq, ipmag, f0et, minf0, maxf0, f0stable) if ((f0stable==0)&(f0t>0)) \ or ((f0stable>0)&(np.abs(f0stable-f0t)<f0stable/5.0)): f0stable = f0t else: f0stable = 0 hfreq, hmag, hphase = harmonicDetection(ipfreq, ipmag, ipphase, f0t, nH, hfreqp, fs, harmDevSlope) hfreqp = hfreq if begin == hM1: xhfreq = np.array([hfreq]) xhmag = np.array([hmag]) xhphase = np.array([hphase]) else: xhfreq = np.vstack((xhfreq,np.array([hfreq]))) xhmag = np.vstack((xhmag, np.array([hmag]))) xhphase = np.vstack((xhphase, np.array([hphase]))) begin += H xhfreq = SM.cleanSineTracks(xhfreq, round(fs*minSineDur/H)) return xhfreq, xhmag, xhphase
def sineModelAnal(x, fs, win, N, H, tresh, maxSines = 100, minSineDur=.01, freqDevOffset=20, freqDevSlope=0.01): if (minSineDur <0): raise ValueError("Minimum duration is smaller than 0") hM1 = int(math.floor((win.size+1)/2)) hM2 = int(math.floor(win.size/2)) x = np.append(np.zeros(hM2),x) # center around 0 x = np.append(x,np.zeros(hM2)) begin = hM1 end = x.size - hM1 win = win / sum(win) tfreq = np.array([]) while begin<end: x1 = x[begin-hM1:begin+hM2] mX, pX = dft.dftAnal(x1, win, N) peaksLocation = U.peakDetection(mX, tresh) iplocation, ipmag, ipphase = U.peak_parabolicInterp(mX, pX, peaksLocation) ipfreq = fs*iplocation/float(N) # perform sinusoidal tracking by adding peaks to trajectories tfreq, tmag, tphase = sineTracking(ipfreq, ipmag, ipphase, tfreq, freqDevOffset, freqDevSlope) tfreq = np.resize(tfreq, min(maxSines, tfreq.size)) tmag = np.resize(tmag, min(maxSines, tmag.size)) tphase = np.resize(tphase, min(maxSines, tphase.size)) jtfreq = np.zeros(maxSines) jtmag = np.zeros(maxSines) jtphase = np.zeros(maxSines) jtfreq[:tfreq.size]=tfreq jtmag[:tmag.size]=tmag jtphase[:tphase.size]=tphase if begin == 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)) begin += H xtfreq = cleanSineTracks(xtfreq, round(fs*minSineDur/H)) return xtfreq, xtmag, xtphase
def f0Detection(x, fs, w, N, H, t, minf0, maxf0, f0et): if (minf0 < 0): raise ValueError("Minumum fundamental frequency (minf0) smaller than 0") if (maxf0 >= fs/2): raise ValueError("Maximum fundamental frequency (maxf0) bigger than 10000Hz") if (H <= 0): raise ValueError("Hop size (H) smaller or equal to 0") hM1 = int(math.floor((w.size+1)/2)) hM2 = int(math.floor(w.size/2)) x = np.append(np.zeros(hM2),x) x = np.append(x,np.zeros(hM1)) begin = hM1 end = x.size - hM1 w = w / sum(w) f0 = [] f0t = 0 f0stable = 0 while begin<end: x1 = x[begin-hM1:begin+hM2] mX, pX = DFT.dftAnal(x1, w, N) ploc = U.peakDetection(mX, t) iploc, ipmag, ipphase = U.peak_parabolicInterp(mX, pX, ploc) ipfreq = fs * iploc/N f0t = U.f0Twm(ipfreq, ipmag, f0et, minf0, maxf0, f0stable) if ((f0stable==0)&(f0t>0)) \ or ((f0stable>0)&(np.abs(f0stable-f0t)<f0stable/5.0)): f0stable = f0t else: f0stable = 0 f0 = np.append(f0, f0t) begin += H return f0
def hprModel(x, fs, w, N, t, nH, minf0, maxf0, f0et): 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 Ns = 512 # FFT size for synthesis (even) H = Ns//4 # Hop size used for analysis and synthesis hNs = Ns//2 pin = max(hNs, hM1) # initialize sound pointer in middle of analysis window pend = x.size - max(hNs, hM1) # last sample to start a frame fftbuffer = np.zeros(N) # initialize buffer for FFT yhw = np.zeros(Ns) # initialize output sound frame xrw = np.zeros(Ns) # initialize output sound frame yh = np.zeros(x.size) # initialize output array xr = np.zeros(x.size) # initialize output array w = w / sum(w) # normalize analysis window sw = np.zeros(Ns) ow = triang(2*H) sw[hNs-H:hNs+H] = ow bh = blackmanharris(Ns) bh = bh / sum(bh) wr = bh sw[hNs-H:hNs+H] = sw[hNs-H:hNs+H] / bh[hNs-H:hNs+H] hfreqp = [] f0t = 0 f0stable = 0 while pin<pend: # analyze x1 = x[pin-hM1:pin+hM2] mX, pX = DFT.dftAnal(x1, w, N) ploc = U.peakDetection(mX, t) iploc, ipmag, ipphase = U.peak_parabolicInterp(mX, pX, ploc) ipfreq = fs * iploc/N f0t = U.f0Twm(ipfreq, ipmag, f0et, minf0, maxf0, f0stable) if ((f0stable==0)&(f0t>0)) or ((f0stable>0)&(np.abs(f0stable-f0t)<f0stable/5.0)): f0stable = f0t else: f0stable = 0 hfreq, hmag, hphase = HM.harmonicDetection(ipfreq, ipmag, ipphase, f0t, nH, hfreqp, fs) hfreqp = hfreq ri = pin-hNs-1 xw2 = x[ri:ri+Ns]*wr fftbuffer = np.zeros(Ns) fftbuffer[:hNs] = xw2[hNs:] # zero-phase window fftbuffer[hNs:] = xw2[:hNs] X2 = fft(fftbuffer) # synth Yh = U.genSinesSpectrum(hfreq, hmag, hphase, Ns, fs) Xr = X2-Yh fftbuffer = np.zeros(Ns) fftbuffer = np.real(ifft(Yh)) yhw[:hNs-1] = fftbuffer[hNs+1:] yhw[hNs-1:] = fftbuffer[:hNs+1] fftbuffer = np.zeros(Ns) fftbuffer = np.real(ifft(Xr)) xrw[:hNs-1] = fftbuffer[hNs+1:] xrw[hNs-1:] = fftbuffer[:hNs+1] yh[ri:ri+Ns] += sw*yhw xr[ri:ri+Ns] += sw*xrw pin += H y = yh+xr return y, yh, xr
def hpsModel(x, fs, w, N, t, nH, minf0, maxf0, f0et, stocf): hM1 = int(math.floor((w.size + 1) / 2)) hM2 = int(math.floor(w.size / 2)) Ns = 512 H = Ns // 4 hNs = Ns // 2 begin = max(hNs, hM1) end = x.size - max(hNs, hM1) fftbuffer = np.zeros(N) yhw = np.zeros(Ns) ystw = np.zeros(Ns) yh = np.zeros(x.size) yst = np.zeros(x.size) w = w / sum(w) sw = np.zeros(Ns) ow = triang(2 * H) sw[hNs - H:hNs + H] = ow bh = blackmanharris(Ns) bh = bh / sum(bh) wr = bh sw[hNs - H:hNs + H] = sw[hNs - H:hNs + H] / bh[hNs - H:hNs + H] sws = H * hanning(Ns) / 2 hfreqp = [] f0t = 0 f0stable = 0 while begin < end: # analyze x1 = x[begin - hM1:begin + hM2] mX, pX = DFT.dftAnal(x1, w, N) ploc = U.peakDetection(mX, t) iploc, ipmag, ipphase = U.peak_parabolicInterp(mX, pX, ploc) ipfreq = fs * iploc / N f0t = U.f0Twm(ipfreq, ipmag, f0et, minf0, maxf0, f0stable) if ((f0stable==0)&(f0t>0)) \ or ((f0stable>0)&(np.abs(f0stable-f0t)<f0stable/5.0)): f0stable = f0t else: f0stable = 0 hfreq, hmag, hphase = HM.harmonicDetection(ipfreq, ipmag, ipphase, f0t, nH, hfreqp, fs) hfreqp = hfreq ri = begin - hNs - 1 xw2 = x[ri:ri + Ns] * wr fftbuffer = np.zeros(Ns) fftbuffer[:hNs] = xw2[hNs:] fftbuffer[hNs:] = xw2[:hNs] X2 = fft(fftbuffer) # synth Yh = U.genSinesSpectrum(hfreq, hmag, hphase, Ns, fs) Xr = X2 - Yh mXr = 20 * np.log10(abs(Xr[:hNs])) mXrenv = resample(np.maximum(-200, mXr), mXr.size * stocf) # decimate the spectrum to avoid -Inf stocEnv = resample(mXrenv, hNs) pYst = 2 * np.pi * np.random.rand(hNs) Yst = np.zeros(Ns, dtype=complex) Yst[:hNs] = 10**(stocEnv / 20) * np.exp(1j * pYst) Yst[hNs + 1:] = 10**(stocEnv[:0:-1] / 20) * np.exp(-1j * pYst[:0:-1]) fftbuffer = np.zeros(Ns) fftbuffer = np.real(ifft(Yh)) yhw[:hNs - 1] = fftbuffer[hNs + 1:] yhw[hNs - 1:] = fftbuffer[:hNs + 1] fftbuffer = np.zeros(Ns) fftbuffer = np.real(ifft(Yst)) ystw[:hNs - 1] = fftbuffer[hNs + 1:] ystw[hNs - 1:] = fftbuffer[:hNs + 1] yh[ri:ri + Ns] += sw * yhw yst[ri:ri + Ns] += sws * ystw begin += H y = yh + yst return y, yh, yst