def visualize(N, mX, pin, fs, ploc, iploc, ipmag, ipphase): ''' visuaizes ''' ipfreq = fs * iploc / N # generate total spectrum by main lobe X = UF.genSpecSines_p(ipfreq, ipmag, ipphase, N, fs) # generate spec sines hN = N / 2 + 1 # size of positive spectrum absX = abs(X[:hN]) # compute ansolute value of positive side absX[absX < np.finfo(float).eps] = np.finfo( float).eps # if zeros add epsilon to handle log generatedX = absX # generatedX = 20 * np.log10(absX) ######################## visualizeSpectum(mX, pin / fs, ploc, iploc, ipmag, generatedX)
def sineModelSynth(tfreq, tmag, tphase, N, H, fs): """ Synthesis of a sound using the sinusoidal model tfreq,tmag,tphase: frequencies, magnitudes and phases of sinusoids N: synthesis FFT size, H: hop size, fs: sampling rate returns y: output array sound """ hN = N // 2 # half of FFT size for synthesis L = tfreq.shape[0] # number of frames pout = 0 # initialize output sound pointer ysize = H * (L + 3) # output sound size y = np.zeros(ysize) # initialize output array sw = np.zeros(N) # initialize synthesis window ow = triang(2 * H) # triangular window sw[hN - H:hN + H] = ow # add triangular window bh = blackmanharris(N) # blackmanharris window bh = bh / sum(bh) # normalized blackmanharris window sw[hN - H:hN + H] = sw[hN - H:hN + H] / bh[hN - H:hN + H] # normalized synthesis window lastytfreq = tfreq[0, :] # initialize synthesis frequencies ytphase = 2 * np.pi * np.random.rand( tfreq[0, :].size) # initialize synthesis phases for l in range(L): # iterate over all frames if (tphase.size > 0): # if no phases generate them ytphase = tphase[l, :] else: ytphase += (np.pi * (lastytfreq + tfreq[l, :]) / fs) * H # propagate phases Y = UF.genSpecSines_p(tfreq[l, :], tmag[l, :], ytphase, N, fs) # generate sines in the spectrum lastytfreq = tfreq[l, :] # save frequency for phase propagation ytphase = ytphase % (2 * np.pi) # make phase inside 2*pi yw = np.real(fftshift(ifft(Y))) # compute inverse FFT y[pout:pout + N] += sw * yw # overlap-add and apply a synthesis window pout += H # advance sound pointer y = np.delete(y, range(hN)) # delete half of first window y = np.delete(y, range(y.size - hN, y.size)) # delete half of the last window return y
from scipy.fftpack import ifft import dftModel as DFT (fs, x) = UF.wavread('../../sounds/oboe-A4.wav') Ns = 512 hNs = Ns / 2 H = Ns / 4 M = 511 t = -70 w = get_window('hamming', M) x1 = x[.8*fs:0.8*fs+M] mX, pX = DFT.dftAnal(x1, w, Ns) ploc = UF.peakDetection(mX, t) iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc) ipfreq = fs * iploc / float(Ns) Y = UF.genSpecSines_p(ipfreq, ipmag, ipphase, Ns, fs) y = np.real(ifft(Y)) sw = np.zeros(Ns) ow = triang(Ns/2) 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] yw = np.zeros(Ns) yw[:hNs-1] = y[hNs+1:] yw[hNs-1:] = y[:hNs+1] yw *= sw freqaxis = fs * np.arange(Ns/2 + 1) / float(Ns)
from scipy.signal import blackmanharris, triang from scipy.fftpack import ifft (fs, x) = UF.wavread('../../sounds/oboe-A4.wav') Ns = 512 hNs = Ns / 2 H = Ns / 4 M = 511 t = -70 w = get_window('hamming', M) x1 = x[.8 * fs:.8 * fs + M] mX, pX = DFT.dftAnal(x1, w, Ns) ploc = UF.peakDetection(mX, t) iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc) ipfreq = fs * iploc / float(Ns) Y = UF.genSpecSines_p(ipfreq, ipmag, ipphase, Ns, fs) y = np.real(ifft(Y)) sw = np.zeros(Ns) ow = triang(Ns / 2) 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] yw = np.zeros(Ns) yw[:hNs - 1] = y[hNs + 1:] yw[hNs - 1:] = y[:hNs + 1] yw *= sw freqaxis = fs * np.arange(Ns / 2 + 1) / float(Ns)
def harmonicModel(x, fs, w, N, t, nH, minf0, maxf0, f0et): """ Analysis/synthesis 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), returns y: output array sound """ 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(hM1)) # add zeros at the end to analyze last sample Ns = 512 # FFT size for synthesis (even) H = Ns / 4 # Hop size used for analysis and synthesis hNs = Ns / 2 pin = max(hNs, hM1) # init sound pointer in middle of anal window pend = x.size - max(hNs, hM1) # last sample to start a frame fftbuffer = np.zeros(N) # initialize buffer for FFT yh = np.zeros(Ns) # initialize output sound frame y = np.zeros(x.size) # initialize output array w = w / sum(w) # normalize analysis window sw = np.zeros(Ns) # initialize synthesis window ow = triang(2 * H) # overlapping window sw[hNs - H:hNs + H] = ow bh = blackmanharris(Ns) # synthesis window bh = bh / sum(bh) # normalize synthesis window sw[hNs - H:hNs + H] = sw[hNs - H:hNs + H] / bh[hNs - H:hNs + H] # window for overlap-add hfreqp = [] f0t = 0 f0stable = 0 while pin < pend: #-----analysis----- 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 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) # find harmonics hfreqp = hfreq #-----synthesis----- Yh = UF.genSpecSines_p(hfreq, hmag, hphase, Ns, fs) # generate spec sines fftbuffer = np.real(ifft(Yh)) # inverse FFT yh[:hNs - 1] = fftbuffer[hNs + 1:] # undo zero-phase window yh[hNs - 1:] = fftbuffer[:hNs + 1] y[pin - hNs:pin + hNs] += sw * yh # overlap-add pin += H # advance sound pointer y = np.delete( y, range(hM2)) # delete half of first window which was added in stftAnal y = np.delete(y, range(y.size - hM1, y.size)) # add zeros at the end to analyze last sample return y
Ns = 512 hNs = int(Ns/2) H = int(Ns/4) t = -70 #min threshold from the peak for other peaks w = get_window('hamming', M) x1 = x[int(.8*fs):int(.8*fs) + M] mX, pX = DFT.dftAnal(x1, w, Ns) peaks = UF.peakDetection(mX, t) ipeaks, iMag, iPhase = UF.peakInterp(mX, pX, peaks) ipfreq = fs * ipeaks / float(Ns) #ipfreq = np.array([3000.0, 4000.0]) #iMag = np.array([0.0, 0.0]) #iPhase = np.array([0.0, 0.0]) Y = UF.genSpecSines_p(ipfreq, iMag, iPhase, Ns, fs) y = np.real(ifft(Y)) print(y.size) sw = np.zeros(Ns) #code in order to undo the blackman harris window and apply triangular function ow = triang(Ns/2) 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] yw = np.zeros(Ns) yw[:hNs-1] = y[hNs+1:] yw[hNs-1:] = y[:hNs+1] yw *= sw
(fs, x) = UF.wavread('../../sounds/oboe-A4.wav') Ns = 512 hNs = Ns / 2 H = Ns / 4 M = 511 t = -70 w = get_window('hamming', M) x1 = x[int(.8 * fs):int(.8 * fs) + M] mX, pX = DFT.dftAnal(x1, w, Ns) ploc = UF.peakDetection(mX, t) iploc, ipmag, ipphase = UF.peakInterp( mX, pX, ploc ) #interpolate the peaks to get more accurate value of the location of the sinusoid ipfreq = fs * iploc / float(Ns) Y = UF.genSpecSines_p(ipfreq, ipmag, ipphase, Ns, fs) # do the synthesis y = np.real(ifft(Y)) # inverse fft # undo the window sw = np.zeros(Ns) ow = triang(Ns / 2) sw[int(hNs - H):int(hNs + H)] = ow bh = blackmanharris(Ns) bh = bh / sum(bh) sw[int(hNs - H):int(hNs + H)] = sw[int(hNs - H):int(hNs + H)] / bh[int(hNs - H):int(hNs + H)] yw = np.zeros(Ns) yw[:int(hNs - 1)] = y[int(hNs + 1):]
sys.path.append( os.path.join(os.path.dirname(os.path.realpath(__file__)), "/home/pvardanis/sms-tools/software/models/")) import dftModel as DFT import utilFunctions as UF from scipy.fftpack import ifft from scipy.signal import blackmanharris, triang fs = 44100 Ns = 512 hNs = int(Ns / 2) H = int(Ns / 4) ipfreq = np.array([4000.0]) # frequency of the sinusoid ipmag = np.array([0.0]) ipphase = np.array([0.0]) Y = UF.genSpecSines_p( ipfreq, ipmag, ipphase, Ns, fs) # create the frequency spectrum of a blackmanharris window y = np.real(ifft(Y)) sw = np.zeros(Ns) # create a triangular window for better hopping factor ow = triang(Ns / 2) sw[hNs - H:hNs + H] = ow # center around the middle bh = blackmanharris(Ns) bh = bh / sum(bh) sw[hNs - H:hNs + H] = sw[hNs - H:hNs + H] / bh[hNs - H:hNs + H] yw = np.zeros(Ns) yw[:hNs - 1] = y[hNs + 1:] yw[hNs - 1:] = y[:hNs + 1] yw *= sw