• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python utilFunctions.genSpecSines函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中utilFunctions.genSpecSines函数的典型用法代码示例。如果您正苦于以下问题:Python genSpecSines函数的具体用法?Python genSpecSines怎么用?Python genSpecSines使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了genSpecSines函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: sinewaveSynth

def sinewaveSynth(freq, mag, N, H, fs):
  # Synthesis of a time-varying sinusoid
  # freq,mag, phase: frequency, magnitude and phase of sinusoid,
  # 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 = freq.size                                           # 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
  lastfreq = freq[0]                                      # initialize synthesis frequencies
  phase = 0                                               # initialize synthesis phases 
  for l in range(L):                                      # iterate over all frames
    phase += (np.pi*(lastfreq+freq[l])/fs)*H            # propagate phases
    Y = UF.genSpecSines(freq[l], mag[l], phase, N, fs)    # generate sines in the spectrum         
    lastfreq = freq[l]                                    # save frequency for phase propagation
    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
开发者ID:imclab,项目名称:sms-tools,代码行数:28,代码来源:sineModel.py


示例2: sineModelSynth

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(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
开发者ID:hello-sergei,项目名称:sms-tools,代码行数:35,代码来源:sineModel.py


示例3: harmonicModel

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(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
开发者ID:ronggong,项目名称:JingjuAriasMIRAnalysis,代码行数:59,代码来源:harmonicModel.py


示例4: sprModel

def sprModel(x, fs, w, N, t):
	"""
	Analysis/synthesis of a sound using the sinusoidal plus residual model, one frame at a time
	x: input sound, fs: sampling rate, w: analysis window, 
	N: FFT size (minimum 512), t: threshold in negative dB, 
	returns y: output sound, ys: sinusoidal component, xr: residual component
	"""

	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
	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
	ysw = np.zeros(Ns)                                            # initialize output sound frame
	xrw = np.zeros(Ns)                                            # initialize output sound frame
	ys = 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)                                              # overlapping window
	sw[hNs-H:hNs+H] = ow      
	bh = blackmanharris(Ns)                                       # synthesis window
	bh = bh / sum(bh)                                             # normalize synthesis window
	wr = bh                                                       # window for residual
	sw[hNs-H:hNs+H] = sw[hNs-H:hNs+H] / bh[hNs-H:hNs+H]
	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)                              # find peaks 
		iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc)         # refine peak values		iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc)          # refine peak values
		ipfreq = fs*iploc/float(N)                                  # convert peak locations to Hertz
		ri = pin-hNs-1                                              # input sound pointer for residual analysis
		xw2 = x[ri:ri+Ns]*wr                                        # window the input sound                                       
		fftbuffer = np.zeros(Ns)                                    # reset buffer
		fftbuffer[:hNs] = xw2[hNs:]                                 # zero-phase window in fftbuffer
		fftbuffer[hNs:] = xw2[:hNs]                           
		X2 = fft(fftbuffer)                                         # compute FFT for residual analysis
  #-----synthesis-----
		Ys = UF.genSpecSines(ipfreq, ipmag, ipphase, Ns, fs)        # generate spec of sinusoidal component          
		Xr = X2-Ys;                                                 # get the residual complex spectrum
		fftbuffer = np.zeros(Ns)
		fftbuffer = np.real(ifft(Ys))                               # inverse FFT of sinusoidal spectrum
		ysw[:hNs-1] = fftbuffer[hNs+1:]                             # undo zero-phase window
		ysw[hNs-1:] = fftbuffer[:hNs+1] 
		fftbuffer = np.zeros(Ns)
		fftbuffer = np.real(ifft(Xr))                               # inverse FFT of residual spectrum
		xrw[:hNs-1] = fftbuffer[hNs+1:]                             # undo zero-phase window
		xrw[hNs-1:] = fftbuffer[:hNs+1]
		ys[ri:ri+Ns] += sw*ysw                                      # overlap-add for sines
		xr[ri:ri+Ns] += sw*xrw                                      # overlap-add for residual
		pin += H                                                    # advance sound pointer
	y = ys+xr                                                     # sum of sinusoidal and residual components
	return y, ys, xr
开发者ID:2opremio,项目名称:sms-tools,代码行数:58,代码来源:sprModel.py


示例5: hpsModelSynth

def hpsModelSynth(hfreq, hmag, hphase, mYst, N, H, fs):
	# Synthesis of a sound using the harmonic plus stochastic model
	# hfreq: harmonic frequencies, hmag:harmonic amplitudes, mYst: stochastic envelope
	# Ns: synthesis FFT size, H: hop size, fs: sampling rate 
	# y: output sound, yh: harmonic component, yst: stochastic component
	hN = N/2                                                  # half of FFT size for synthesis
	L = hfreq[:,0].size                                       # number of frames
	nH = hfreq[0,:].size                                      # number of harmonics
	pout = 0                                                  # initialize output sound pointer         
	ysize = H*(L+4)                                           # output sound size
	yhw = np.zeros(N)                                        # initialize output sound frame
	ysw = np.zeros(N)                                        # initialize output sound frame
	yh = np.zeros(ysize)                                      # initialize output array
	yst = np.zeros(ysize)                                     # initialize output array
	sw = np.zeros(N)     
	ow = triang(2*H)                                          # overlapping window
	sw[hN-H:hN+H] = ow      
	bh = blackmanharris(N)                                   # synthesis window
	bh = bh / sum(bh)                                         # normalize synthesis window
	wr = bh                                                   # window for residual
	sw[hN-H:hN+H] = sw[hN-H:hN+H] / bh[hN-H:hN+H]             # synthesis window for harmonic component
	sws = H*hanning(N)/2                                      # synthesis window for stochastic component
	lastyhfreq = hfreq[0,:]                                   # initialize synthesis harmonic frequencies
	yhphase = 2*np.pi*np.random.rand(nH)                      # initialize synthesis harmonic phases     
	for l in range(L):
		yhfreq = hfreq[l,:]                                     # synthesis harmonics frequencies
		yhmag = hmag[l,:]                                       # synthesis harmonic amplitudes
		mYrenv = mYst[l,:]                                      # synthesis residual envelope
		if (hphase.size > 0):
			yhphase = hphase[l,:] 
		else:
			yhphase += (np.pi*(lastyhfreq+yhfreq)/fs)*H             # propagate phases
		lastyhfreq = yhfreq
		Yh = UF.genSpecSines(yhfreq, yhmag, yhphase, N, fs)     # generate spec sines 
		mYs = resample(mYrenv, hN)                              # interpolate to original size
		mYs = 10**(mYs/20)                                      # dB to linear magnitude  
		pYs = 2*np.pi*np.random.rand(hN)                        # generate phase random values
		Ys = np.zeros(N, dtype = complex)
		Ys[:hN] = mYs * np.exp(1j*pYs)                         # generate positive freq.
		Ys[hN+1:] = mYs[:0:-1] * np.exp(-1j*pYs[:0:-1])        # generate negative freq.
		fftbuffer = np.zeros(N)
		fftbuffer = np.real(ifft(Yh))                           # inverse FFT of harm spectrum
		yhw[:hN-1] = fftbuffer[hN+1:]                         # undo zer-phase window
		yhw[hN-1:] = fftbuffer[:hN+1] 
		fftbuffer = np.zeros(N)
		fftbuffer = np.real(ifft(Ys))                           # inverse FFT of stochastic approximation spectrum
		ysw[:hN-1] = fftbuffer[hN+1:]                           # undo zero-phase window
		ysw[hN-1:] = fftbuffer[:hN+1]
		yh[pout:pout+N] += sw*yhw                               # overlap-add for sines
		yst[pout:pout+N] += sws*ysw                             # overlap-add for stoch
		pout += H                                               # advance sound pointer
	y = yh+yst                                                # sum harmonic and stochastic components
	return y, yh, yst
开发者ID:imclab,项目名称:sms-tools,代码行数:53,代码来源:hpsModel.py


示例6: sineModelMultiRes

def sineModelMultiRes(x, fs, wList, NList, t, BList):
	"""
	Analysis/synthesis of a sound using the sinusoidal model, without sine tracking
	x: input array sound, w: analysis window, N: size of complex spectrum, t: threshold in negative dB 
	returns y: output array sound
	"""

	#-----synthesis params init-----             
	Ns = 512                                                # FFT size for synthesis (even)
	H = Ns/4                                                # Hop size used for analysis and synthesis
	hNs = Ns/2                                              # half of synthesis FFT size
	yw = np.zeros(Ns)                                       # initialize output sound frame
	y = np.zeros(x.size)                                    # initialize output array
	sw = np.zeros(Ns)                                       # initialize synthesis window
	ow = triang(2*H)                                        # triangular window
	sw[hNs-H:hNs+H] = ow                                    # add triangular window
	bh = blackmanharris(Ns)                                 # blackmanharris window
	bh = bh / sum(bh)                                       # normalized blackmanharris window
	sw[hNs-H:hNs+H] = sw[hNs-H:hNs+H] / bh[hNs-H:hNs+H]     # normalized synthesis window
	for i in range(3):
	#-----analysis params init-----             
		w = wList[i]
		N = NList[i]
		Bmin = BList[i][0]
		Bmax = BList[i][1]
		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
		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	
		w = w / sum(w)                                          # normalize analysis window
		while pin<pend:                                         # while input sound pointer is within sound 
		#-----analysis-----             			
			x1 = x[pin-hM1:pin+hM2]                               # select frame
			mX, pX = DFT.dftAnal(x1, w, N)                        # compute dft
			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
			ipmag = ipmag[np.logical_and(ipfreq>=Bmin, ipfreq<Bmax)]
			ipphase = ipphase[np.logical_and(ipfreq>=Bmin, ipfreq<Bmax)]
			ipfreq = ipfreq[np.logical_and(ipfreq>=Bmin, ipfreq<Bmax)]
		#-----synthesis-----
			Y = UF.genSpecSines(ipfreq, ipmag, ipphase, Ns, fs)   # generate sines in the spectrum         
			fftbuffer = np.real(ifft(Y))                          # compute inverse FFT
			yw[:hNs-1] = fftbuffer[hNs+1:]                        # undo zero-phase window
			yw[hNs-1:] = fftbuffer[:hNs+1] 
			y[pin-hNs:pin+hNs] += sw*yw                           # overlap-add and apply a synthesis window
			pin += H                                              # advance sound pointer

	return y
开发者ID:dshoot,项目名称:sms-tools,代码行数:50,代码来源:sineModel.py


示例7: sineModelMultiRes

def sineModelMultiRes(x, fs, w, N, t, B):
    """
	Analysis/synthesis of a sound using the sinusoidal model, without sine tracking
	x: input array sound, w: array of analysis windows, N: array of sizes of complex spectrum,
	t: threshold in negative dB, B: array of frequency bands
	returns y: output array sound
	"""

    hM1 = [int(math.floor((_w.size + 1) / 2)) for _w in w]  # half analysis window(s) size by rounding
    hM2 = [int(math.floor(_w.size / 2)) for _w in w]  # half analysis window(s) size by floor
    Ns = 512  # FFT size for synthesis (even)
    H = Ns / 4  # Hop size used for analysis and synthesis
    hNs = Ns / 2  # half of synthesis FFT size
    pin = max(hNs, max(hM1))  # init sound pointer in middle of anal window
    pend = x.size - max(hNs, max(hM1))  # last sample to start a frame
    fftbuffer = np.array([])  # initialize buffer for FFT
    yw = np.zeros(Ns)  # initialize output sound frame
    y = np.zeros(x.size)  # initialize output array
    w = [_w / sum(_w) for _w in w]  # normalize analysis window(s)
    sw = np.zeros(Ns)  # initialize synthesis window
    ow = triang(2 * H)  # triangular window
    sw[hNs - H : hNs + H] = ow  # add triangular window
    bh = blackmanharris(Ns)  # blackmanharris window
    bh = bh / sum(bh)  # normalized blackmanharris window
    sw[hNs - H : hNs + H] = sw[hNs - H : hNs + H] / bh[hNs - H : hNs + H]  # normalized synthesis window
    while pin < pend:  # while input sound pointer is within sound
        # -----analysis-----
        ipmag = ipphase = ipfreq = np.array([])  # initialize the synthesis arrays
        for i in range(0, len(w)):  # for each window, use some loop variables ('_' prefix)
            _hM1, _hM2, _w, _N, _B = (hM1[i], hM2[i], w[i], N[i], B[i])
            x1 = x[pin - _hM1 : pin + _hM2]  # select frame
            mX, pX = DFT.dftAnal(x1, _w, _N)  # compute dft
            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
            lo, hi = (_B[0], _B[1])  # low/high from band tuples [..(lo, hi)..]
            mask = (_ipfreq >= lo) * (_ipfreq < hi)  # mask for in-band components
            ipmag = np.append(ipmag, _ipmag * mask)  # mask and append components
            ipphase = np.append(ipphase, _ipphase * mask)
            ipfreq = np.append(ipfreq, _ipfreq * mask)
            # -----synthesis-----
        Y = UF.genSpecSines(ipfreq, ipmag, ipphase, Ns, fs)  # generate sines in the spectrum
        fftbuffer = np.real(ifft(Y))  # compute inverse FFT
        yw[: hNs - 1] = fftbuffer[hNs + 1 :]  # undo zero-phase window
        yw[hNs - 1 :] = fftbuffer[: hNs + 1]
        y[pin - hNs : pin + hNs] += sw * yw  # overlap-add and apply a synthesis window
        pin += H  # advance sound pointer
    return y
开发者ID:raffaelenoro,项目名称:sms-tools,代码行数:48,代码来源:sineModel.py


示例8: sineModel

def sineModel(x, fs, w, N, t):
  # Analysis/synthesis of a sound using the sinusoidal model
  # x: input array sound, w: analysis window, N: size of complex spectrum,
  # t: threshold in negative dB 
  # 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
  Ns = 512                                                # FFT size for synthesis (even)
  H = Ns/4                                                # Hop size used for analysis and synthesis
  hNs = Ns/2                                              # half of synthesis FFT size
  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
  yw = 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);                                       # triangular window
  sw[hNs-H:hNs+H] = ow                                    # add triangular window
  bh = blackmanharris(Ns)                                 # blackmanharris window
  bh = bh / sum(bh)                                       # normalized blackmanharris window
  sw[hNs-H:hNs+H] = sw[hNs-H:hNs+H] / bh[hNs-H:hNs+H]     # normalized synthesis window
  while pin<pend:                                         # while input sound pointer is within sound 
  #-----analysis-----             
    x1 = x[pin-hM1:pin+hM2]                               # select frame
    mX, pX = DFT.dftAnal(x1, w, N)                        # compute dft
    ploc = UF.peakDetection(mX, hN, t)                    # detect locations of peaks
    pmag = mX[ploc]                                       # get the magnitude of the peaks
    iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc)   # refine peak values by interpolation
  #-----synthesis-----
    plocs = iploc*Ns/N                                    # adapt peak locations to size of synthesis FFT
    Y = UF.genSpecSines(fs*plocs/N, ipmag, ipphase, Ns, fs)    # generate sines in the spectrum         
    fftbuffer = np.real(ifft(Y))                          # compute inverse FFT
    yw[:hNs-1] = fftbuffer[hNs+1:]                        # undo zero-phase window
    yw[hNs-1:] = fftbuffer[:hNs+1] 
    y[pin-hNs:pin+hNs] += sw*yw                           # overlap-add and apply a synthesis window
    pin += H                                              # advance sound pointer
  return y
开发者ID:john36590,项目名称:sms-tools,代码行数:39,代码来源:sineModel.py


示例9: loudnessHarmonics

def loudnessHarmonics (fileName, dumpFile = False):

    eps = np.finfo(np.float).eps
    
    path2SmsTools = '../../sms-tools-master'
    path2Models = os.path.join(path2SmsTools, 'software/models')
    sys.path.append(path2Models)
    
    import utilFunctions as UF
    import harmonicModel as HM
    import dftModel as DFT

    # Computing predominant melody
    H = 128
    M = 2048
    fs = 44100
    guessUnvoiced = True
    MELODIA = ess.PredominantMelody(guessUnvoiced=guessUnvoiced,
                                                   frameSize=M,
                                                   hopSize=H)
    audio = ess.MonoLoader(filename = fileName, sampleRate=fs) ()
    audioEL = ess.EqualLoudness() (audio)
    pitch = MELODIA(audioEL)[0]
    
    # Computing loudness including harmonics
    LOUDNESS = ess.Loudness()
    winAnalysis = 'hann'
    t = -80
    harmLoudness = []
    ## Synthesis
    nH = 15
    f0et = 5
    x = audioEL
    w = get_window(winAnalysis, M)
    hM1 = int(math.floor(w.size+1)/2)
    hM2 = int(math.floor(w.size/2))
    Ns = 4*H
    hNs = Ns/2
    startApp = max(hNs, hM1)
    pin = startApp
    pend = x.size - startApp
    x = np.append(np.zeros(startApp), x)
    x = np.append(x, np.zeros(startApp))
    N = 2 * M
    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
    cnt = 0
    while pin < pend:
        x1 = x[pin-hM1:pin+hM2]
        mX, pX = DFT.dftAnal(x1, w, N)
        ploc = UF.peakDetection(mX, t)
        iploc, ipmag, ipphase = UF. peakInterp(mX, pX, ploc)
        ipfreq = fs * iploc/N
        f0t = pitch[cnt]
        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
        
        Yh = UF.genSpecSines(hfreq, hmag, hphase, Ns, fs)
        fftbuffer = np.real(ifft(Yh))
        yh[:hNs-1] = fftbuffer[hNs+1:]
        yh[hNs-1:] = fftbuffer[:hNs+1]
        yh_frame = sw*yh
        y[pin-hNs:pin+hNs] += yh_frame
        pin += H
        cnt+=1
        harmLoudness.append(LOUDNESS(yh_frame.tolist()))

    harmLoudness = np.array(harmLoudness)

    timeStamps = np.arange(harmLoudness.size) * H / float(fs)
    
    # Plotting
#    plt.plot(timeStamps, harmLoudness, color = 'b', linewidth=1)
#    plt.xlabel('Time (s)')
#    plt.ylabel('Amplitude')
#    plt.show()
    
    loudnessData = np.column_stack((timeStamps, harmLoudness))
    
    # Dumping a csv file
    if dumpFile:
        np.savetxt(fileName[:-4] + '-loudnessHarmonics.csv', loudnessData,
               delimiter=',')
#.........这里部分代码省略.........
开发者ID:ronggong,项目名称:Jingju-Audio-Analysis,代码行数:101,代码来源:loudnessAnalysis.py


示例10: sineModelMultiRes

def sineModelMultiRes(x, fs, multi_w, multi_N, t, multi_B):
    """
    Analysis/synthesis of a sound using the sinusoidal model, without sine tracking
    x: input array sound, w: analysis window, N: size of complex spectrum, t: threshold in negative dB
    returns y: output array sound
    """

    bands = range(len(multi_B))                                     # to iterate over bands

    N = max(multi_N)

    multi_w_size = np.array([multi_w[i].size for i in bands])
    multi_hM1 = np.floor((multi_w_size + 1)/2.0).astype(int)                     # half analysis window size by rounding
    multi_hM2 = np.floor(multi_w_size / 2.0).astype(int)                         # 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                                              # half of synthesis FFT size

    multi_pin = np.maximum(hNs, multi_hM1)                    # init sound pointer in middle of anal window
    multi_pend = x.size - multi_pin                           # last sample to start a frame

    fftbuffer_combined = np.zeros(N)

    yw_combined = np.zeros(Ns)                                       # initialize output sound frame
    y_combined = np.zeros(x.size)                                    # initialize output array

    multi_w = [multi_w[i] / sum(multi_w[i]) for i in bands]                                          # normalize analysis window

    sw = np.zeros(Ns)                                       # initialize synthesis window
    ow = triang(2*H)                                        # triangular window
    sw[hNs-H:hNs+H] = ow                                    # add triangular window
    bh = blackmanharris(Ns)                                 # blackmanharris window
    bh = bh / sum(bh)                                       # normalized blackmanharris window
    sw[hNs-H:hNs+H] = sw[hNs-H:hNs+H] / bh[hNs-H:hNs+H]     # normalized synthesis window

    while (multi_pin<multi_pend).all():                                         # while input sound pointer is within sound
    #-----analysis-----

        multi_x1 = [x[(multi_pin[i] - multi_hM1[i]) : (multi_pin[i] + multi_hM2[i])] for i in bands]                               # select frame

        multi_mX = []
        multi_pX = []
        for i in bands:
            mXi, pXi = DFT.dftAnal(multi_x1[i], multi_w[i], multi_N[i])
            multi_mX.append(mXi)
            multi_pX.append(pXi)

        multi_ploc = []
        for i in bands:
            ploci = UF.peakDetection(multi_mX[i], t)                        # detect locations of peaks
            multi_ploc.append(ploci)

        multi_ipmag = []
        multi_ipphase = []
        multi_ipfreq = []
        for i in bands:
            iploci, ipmagi, ipphasei = UF.peakInterp(multi_mX[i], multi_pX[i], multi_ploc[i])   # refine peak values by interpolation
            ipfreqi = fs*iploci/float(multi_N[i])                            # convert peak locations to Hertz
            multi_ipmag.append(ipmagi)
            multi_ipphase.append(ipphasei)
            multi_ipfreq.append(ipfreqi)

        # count first for array allocation
        num_ip = 0
        for i in bands:
            for p in range(len(multi_ipfreq[i])):
                f = multi_ipfreq[i][p]
                if (i == 0 or f >= multi_B[i-1]) and f < multi_B[i]:
                    num_ip += 1

        ipfreq_combined = np.zeros(num_ip)
        ipmag_combined = np.zeros(num_ip)
        ipphase_combined = np.zeros(num_ip)
        ip = 0
        for i in bands:
            for p in range(len(multi_ipfreq[i])):
                f = multi_ipfreq[i][p]
                if (i == 0 or f >= multi_B[i-1]) and f < multi_B[i]:
                    ipfreq_combined[ip] = f
                    ipmag_combined[ip] = multi_ipmag[i][p]
                    ipphase_combined[ip] = multi_ipphase[i][p]
                    ip += 1

    #-----synthesis-----
        Y_combined = UF.genSpecSines(ipfreq_combined, ipmag_combined, ipphase_combined, Ns, fs)   # generate sines in the spectrum
        fftbuffer_combined = np.real(ifft(Y_combined))                          # compute inverse FFT
        yw_combined[:hNs-1] = fftbuffer_combined[hNs+1:]                        # undo zero-phase window
        yw_combined[hNs-1:] = fftbuffer_combined[:hNs+1]
        y_combined[multi_pin[0]-hNs:multi_pin[0]+hNs] += sw*yw_combined                           # overlap-add and apply a synthesis window
        multi_pin += H

    return y_combined
开发者ID:hoinx,项目名称:sms-tools,代码行数:93,代码来源:sineModelMultiRes.py


示例11: hprModel

def hprModel(x, fs, w, N, t, nH, minf0, maxf0, f0et):
	"""
	Analysis/synthesis of a sound using the harmonic plus residual 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),
	maxhd: max. relative deviation in harmonic detection (ex: .2)
	returns y: output sound, yh: harmonic component, xr: residual component
	"""

	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
	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)                                              # overlapping window
	sw[hNs-H:hNs+H] = ow      
	bh = blackmanharris(Ns)                                       # synthesis window
	bh = bh / sum(bh)                                             # normalize synthesis window
	wr = bh                                                       # window for residual
	sw[hNs-H:hNs+H] = sw[hNs-H:hNs+H] / bh[hNs-H:hNs+H]
	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)                              # find peaks 
		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 = HM.harmonicDetection(ipfreq, ipmag, ipphase, f0t, nH, hfreqp, fs) # find harmonics
		hfreqp = hfreq
		ri = pin-hNs-1                                             # input sound pointer for residual analysis
		xw2 = x[ri:ri+Ns]*wr                                       # window the input sound                     
		fftbuffer = np.zeros(Ns)                                   # reset buffer
		fftbuffer[:hNs] = xw2[hNs:]                                # zero-phase window in fftbuffer
		fftbuffer[hNs:] = xw2[:hNs]                     
		X2 = fft(fftbuffer)                                        # compute FFT of input signal for residual analysis
		#-----synthesis-----
		Yh = UF.genSpecSines(hfreq, hmag, hphase, Ns, fs)          # generate sines
		Xr = X2-Yh                                                 # get the residual complex spectrum                       
		fftbuffer = np.zeros(Ns)
		fftbuffer = np.real(ifft(Yh))                              # inverse FFT of harmonic spectrum
		yhw[:hNs-1] = fftbuffer[hNs+1:]                            # undo zero-phase window
		yhw[hNs-1:] = fftbuffer[:hNs+1] 
		fftbuffer = np.zeros(Ns)
		fftbuffer = np.real(ifft(Xr))                              # inverse FFT of residual spectrum
		xrw[:hNs-1] = fftbuffer[hNs+1:]                            # undo zero-phase window
		xrw[hNs-1:] = fftbuffer[:hNs+1]
		yh[ri:ri+Ns] += sw*yhw                                     # overlap-add for sines
		xr[ri:ri+Ns] += sw*xrw                                     # overlap-add for residual
		pin += H                                                   # advance sound pointer
	y = yh+xr                                                    # sum of harmonic and residual components
	return y, yh, xr
开发者ID:2opremio,项目名称:sms-tools,代码行数:73,代码来源:hprModel.py


示例12: fftshift

import stft as STFT
import sineModel as SM
import utilFunctions as UF
  
Ns = 256
hNs = Ns//2
yw = np.zeros(Ns)
fs = 44100
freqs = np.array([1000.0, 4000.0, 8000.0])
amps = np.array([.6, .4, .6])
phases = ([0.5, 1.2, 2.3])
yploc = Ns*freqs/fs
ypmag = 20*np.log10(amps/2.0)
ypphase = phases

Y = UF.genSpecSines(freqs, ypmag, ypphase, Ns, fs)       
mY = 20*np.log10(abs(Y[:hNs]))
pY = np.unwrap(np.angle(Y[:hNs]))
y= fftshift(ifft(Y))*sum(blackmanharris(Ns))
 
plt.figure(1, figsize=(9, 5))
plt.subplot(3,1,1)
plt.plot(fs*np.arange(Ns/2)/Ns, mY, 'r', lw=1.5)
plt.axis([0, fs/2.0,-100,0])
plt.title("mY, freqs (Hz) = 1000, 4000, 8000; amps = .6, .4, .6")

plt.subplot(3,1,2)
pY[pY==0]= np.nan
plt.plot(fs*np.arange(Ns/2)/Ns, pY, 'c', lw=1.5)
plt.axis([0, fs/2.0,-.01,3.0])
plt.title("pY, phases (radians) = .5, 1.2, 2.3")
开发者ID:MTG,项目名称:sms-tools,代码行数:31,代码来源:spectral-sine-synthesis.py


示例13: sineModelMultiRes_combined


#.........这里部分代码省略.........
        #multi_iploc = []
        multi_ipmag = []
        multi_ipphase = []
        multi_ipfreq = []
        for i in bands:
            iploci, ipmagi, ipphasei = UF.peakInterp(multi_mX[i], multi_pX[i], multi_ploc[i])   # refine peak values by interpolation
            ipfreqi = fs*iploci/float(multi_N[i])                            # convert peak locations to Hertz
            #multi_iploc.append(iploci)
            multi_ipmag.append(ipmagi)
            multi_ipphase.append(ipphasei)
            multi_ipfreq.append(ipfreqi)
        #----------------------------------

        # ... but we shall decide here!

        """
        print "--------------------------------------"
        print ipfreq
        print ipmag
        print ipphase
        """

        """
        ipfreq_combined = []
        ipmag_combined = []
        ipphase_combined = []
        for i in bands:
            for p in range(len(multi_ipfreq[i])):
                f = multi_ipfreq[i][p]
                if (i == 0 or f >= multi_B[i-1]) and f < multi_B[i]:
                    ipfreq_combined.append(f)
                    ipmag_combined.append(multi_ipmag[i][p])
                    ipphase_combined.append(multi_ipphase[i][p])


        #ipfreq = np.array(ipfreq_combined)
        #ipmag = np.array(ipmag_combined)
        #ipphase = np.array(ipphase_combined)
        """

        # count first for array allocation
        num_ip = 0
        for i in bands:
            for p in range(len(multi_ipfreq[i])):
                f = multi_ipfreq[i][p]
                if (i == 0 or f >= multi_B[i-1]) and f < multi_B[i]:
                    num_ip += 1

        ipfreq_combined = np.zeros(num_ip)
        ipmag_combined = np.zeros(num_ip)
        ipphase_combined = np.zeros(num_ip)
        ip = 0
        for i in bands:
            for p in range(len(multi_ipfreq[i])):
                f = multi_ipfreq[i][p]
                if (i == 0 or f >= multi_B[i-1]) and f < multi_B[i]:
                    ipfreq_combined[ip] = f
                    ipmag_combined[ip] = multi_ipmag[i][p]
                    ipphase_combined[ip] = multi_ipphase[i][p]
                    ip += 1






        """
        print "--------------------------------------"
        print ipfreq_combined
        print ipmag_combined
        print ipphase_combined
        """

    #-----synthesis-----
        Y = UF.genSpecSines(ipfreq, ipmag, ipphase, Ns, fs)   # generate sines in the spectrum
        fftbuffer = np.real(ifft(Y))                          # compute inverse FFT
        yw[:hNs-1] = fftbuffer[hNs+1:]                        # undo zero-phase window
        yw[hNs-1:] = fftbuffer[:hNs+1]
        y[pin-hNs:pin+hNs] += sw*yw                           # overlap-add and apply a synthesis window
        #print y[pin-hNs:pin+hNs]
        pin += H                                              # advance sound pointer


        Y_combined = UF.genSpecSines(ipfreq_combined, ipmag_combined, ipphase_combined, Ns, fs)   # generate sines in the spectrum
        fftbuffer_combined = np.real(ifft(Y_combined))                          # compute inverse FFT
        yw_combined[:hNs-1] = fftbuffer_combined[hNs+1:]                        # undo zero-phase window
        yw_combined[hNs-1:] = fftbuffer_combined[:hNs+1]
        y_combined[pin-hNs:pin+hNs] += sw*yw_combined                           # overlap-add and apply a synthesis window
        #print y_combined[pin-hNs:pin+hNs]
        multi_pin += H

        """
        plt.figure(1)
        plt.plot(abs(Y))
        plt.figure(2)
        plt.plot(abs(Y_combined))
        plt.show()
        """

    return y, y_combined
开发者ID:hoinx,项目名称:sms-tools,代码行数:101,代码来源:sineModelMultiRes.py


示例14: sineModelMultiRes

def sineModelMultiRes(x, fs, w, N, t, b):
	"""
	Analysis/synthesis of a sound using the sinusoidal model, without sine tracking
	x: input array sound, w: array of analysis windows, N: array of sizes of complex spectrum, t: threshold in negative dB,
	b: array of bandwidths' right borders
	returns y: output array sound
	"""

	winParams = []
	Ns = 512                                                        # FFT size for synthesis (even)
	H = Ns/4                                                        # Hop size used for analysis and synthesis
	hNs = Ns/2                                                      # half of synthesis FFT size
	yw = np.zeros(Ns)                                               # initialize output sound frame
	y = np.zeros(x.size)                                            # initialize output array
	sw = np.zeros(Ns)                                               # initialize synthesis window
	ow = triang(2*H)                                                # triangular window
	sw[hNs-H:hNs+H] = ow                                            # add triangular window
	bh = blackmanharris(Ns)                                         # blackmanharris window
	bh = bh / sum(bh)                                               # normalized blackmanharris window
	sw[hNs-H:hNs+H] = sw[hNs-H:hNs+H] / bh[hNs-H:hNs+H]             # normalized synthesis window
	pin = 0
	pend = x.size - 1
	for winNum in xrange(len(w)):
		win = w[winNum]
		fftN = N[winNum]
		bandFreq = b[winNum]
		hM1 = int(math.floor((win.size+1)/2))                   # half analysis window size by rounding
		hM2 = int(math.floor(win.size/2))                       # half analysis wind 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python utilFunctions.peakDetection函数代码示例发布时间:2022-05-26
下一篇:
Python util2.chdir_top函数代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap