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

Python utilFunctions.peakDetection函数代码示例

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

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



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

示例1: f0Twm

def f0Twm(x, fs, w, N, H, t, minf0, maxf0, f0et):
  # fundamental frequency detection using twm algorithm
  # x: input sound, fs: sampling rate, w: analysis window, 
  # N: FFT size (minimum 512), t: threshold in negative dB, 
  # minf0: minimum f0 frequency in Hz, maxf0: maximim f0 frequency in Hz, 
  # f0et: error threshold in the f0 detection (ex: 5),
  # returns f0: fundamental frequency
  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
  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
  f0 = []
  f0t = 0
  f0stable = 0
  while pin<pend:             
    x1 = x[pin-hM1:pin+hM2]                       # select frame
    mX, pX = DFT.dftAnal(x1, w, N)                # compute dft           
    ploc = UF.peakDetection(mX, hN, t)            # detect peak locations   
    iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc)   # refine peak values
    ipfreq = fs * iploc/N
    f0t = UF.f0DetectionTwm(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

    f0 = np.append(f0, f0t)
    pin += H                                        # advance sound pointer
  return f0
开发者ID:Jose-Coursera,项目名称:sms-tools,代码行数:35,代码来源:harmonicModel.py


示例2: check_k

def check_k(fr1, fr2, fs, k, window):
    fr = fr1
    for fr in fr1 + np.arange(fr2-fr1):
        t = np.arange(441000.0)
        x = np.sin(2.0*np.pi * fr * t / fs)
        M = 100*k+1
        i=0
        while (2**i) < M:
            i+=1
        N = 2**i
        h = M/2
        l_h = len(x)/2 - h + 1
        h_h = l_h + M
        x_cnk = x[l_h:h_h]
        w = get_window(window, M)
        (mX, pX) = DFT.dftAnal(x_cnk, w, N)
        p_loc = UF.peakDetection(mX, -40)
        p_int = UF.peakInterp(mX, pX, p_loc)
        peak = p_int[0]*(fs/float(N))
        p = peak[0]
        if abs(p-fr) > 0.05:
            print "fr: ", fr, " error: ", abs(p-fr)
            return 0
        print "fr: ", fr, " checked"
        fr+=1
        

    return 3
开发者ID:akkeh,项目名称:SOGM_jr3,代码行数:28,代码来源:A5Part1.py


示例3: minFreqEstErr

def minFreqEstErr(inputFile, f):
    """
    Inputs:
            inputFile (string) = wav file including the path
            f (float) = frequency of the sinusoid present in the input audio signal (Hz)
    Output:
            fEst (float) = Estimated frequency of the sinusoid (Hz)
            M (int) = Window size
            N (int) = FFT size
    """
    # analysis parameters:
    window = 'blackman'
    t = -40
    fs, x = UF.wavread(inputFile)
    x_half = len(x) // 2
    f_error = np.inf
    k = 1
    while f_error > 0.05:  # Hz
        M = 100 * k + 1
        M2 = M // 2
        W = get_window(window, M)
        N = int(2 ** np.ceil(np.log2(M)))
        mX, pX = DFT.dftAnal(x[x_half - M2: x_half - M2 + M], W, N)
        ploc = UF.peakDetection(mX, t)
        iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc)
        fEst = iploc * fs / N
        f_error = np.abs(f - fEst)
        k += 1
    return(fEst, M, N)
开发者ID:Jee-Bee,项目名称:ASPFMA,代码行数:29,代码来源:A5Part1.py


示例4: 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


示例5: 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


示例6: peak

 def peak(m, n):
     hfs = fs * 0.5
     x1 = x[hfs-m/2:hfs+(m+1)/2]
     w = get_window(window, m)
     mX, pX = DFT.dftAnal(x1, w, n)
     
     ploc = UF.peakDetection(mX, t)
     iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc)
     fest = fs * iploc[0] / n
     return fest, ploc, mX, pX
开发者ID:icoxfog417,项目名称:sms-tools-workspace,代码行数:10,代码来源:A5Part1.py


示例7: run_one_estimate

def run_one_estimate(x, fs, M, window=DEFAULT_WINDOW, t=DEFAULT_THRESHOLD):
    center_sample = int(len(x) / 2)
    start_sample = center_sample - int(M / 2)
    end_sample = start_sample + M
    N = min_power_2(M)
    x1 = x[start_sample:end_sample]
    w = get_window(window, M)
    mX, pX = DFT.dftAnal(x1, w, N)
    ploc = UF.peakDetection(mX, t)
    iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc)
    fEst = iploc * fs / N
    return (mX, pX, ploc, iploc, ipmag, ipphase, fEst, N)
开发者ID:pearpan,项目名称:asp-class,代码行数:12,代码来源:A5Part1.py


示例8: harmonicModelAnal

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
开发者ID:ChunHungLiu,项目名称:sms-tools,代码行数:51,代码来源:harmonicModel.py


示例9: minFreqEstErr

def minFreqEstErr(inputFile, f):
    """
    Inputs:
            inputFile (string) = wav file including the path
            f (float) = frequency of the sinusoid present in the input audio signal (Hz)
    Output:
            fEst (float) = Estimated frequency of the sinusoid (Hz)
            M (int) = Window size
            N (int) = FFT size
    """
    # analysis parameters:
    window = 'blackman'
    t = -40
   
    #read the file
    fs, s = UF.wavread(inputFile)

    fEst = 0
    error = abs(f - fEst)
    k = 1
    #begin iteration
    while error > 0.05:

        #set window_size for this iteration
        M = 100 * k + 1

        #compute FFT size as next power of two
        exponent = int(np.log2(M)) + 1
        FFT_size = 2**exponent
        df = float(fs) / FFT_size

        #slice the input signal
        s_sliced = s[0.5 * fs - M/2 : 0.5 * fs + M/2 + 1]

        #generate window
        w = get_window("blackman", M)

        #compute DFT
        mX, pX = DFT.dftAnal(s_sliced, w, FFT_size)

        #detect the peaks
        peak_locations = UF.peakDetection(mX, t)
        iploc, ipmag, ipphase = UF.peakInterp(mX, pX, peak_locations)

        fEst = iploc[0] * df

        error = abs(fEst - f)

        k += 1

    return (fEst, M, FFT_size)
开发者ID:phenylalaninqualle,项目名称:aspma_exchange,代码行数:51,代码来源:A5Part1.py


示例10: sineModelAnal

def sineModelAnal(x, fs, w, N, 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, N: size of complex spectrum, H: hop-size, 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")
	
	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([])
	while pin<pend:                                         # while input sound pointer is within sound            
		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
		pmag = mX[ploc]                                       # get the magnitude of the 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 xtfreq, xtmag, xtphase
开发者ID:hello-sergei,项目名称:sms-tools,代码行数:50,代码来源:sineModel.py


示例11: 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


示例12: minFreqEstErr

def minFreqEstErr(inputFile, f):
    """
    Inputs:
            inputFile (string) = wav file including the path
            f (float) = frequency of the sinusoid present in the input audio signal (Hz)
    Output:
            fEst (float) = Estimated frequency of the sinusoid (Hz)
            M (int) = Window size
            N (int) = FFT size
    """
    # analysis parameters:
    window = 'blackman'
    t = -40

    ### Your code here
    k = 1
    M = 100*k + 1
    N = nextPow2(M)
    fs, x = UF.wavread(inputFile)
    w = get_window(window, M)
    x1	= x[ 0.5*fs - M/2.0 : 0.5*fs + M/2.0]
    mX, pX = DFT.dftAnal(x1, w, N)
    ploc = UF.peakDetection(mX, t)
    iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc)
    fEst = iploc * fs / N
    while len(fEst) < 1 or abs(f - fEst[0]) >= 0.05:
        k += 1
        M = 100*k + 1
        N = nextPow2(M)
        w = get_window(window, M)
        fs, x = UF.wavread(inputFile)
        x1	= x[ 0.5*fs - M/2.0 : 0.5*fs + M/2.0]
        mX, pX = DFT.dftAnal(x1, w, N)
        ploc = UF.peakDetection(mX, t)
        iploc, ipmag, ipphase = UF.peakInterp(mX, pX, ploc)
        fEst = iploc * fs / N
    return (fEst[0], M, N)
开发者ID:feynmanliang,项目名称:sms-tools,代码行数:37,代码来源:A5Part1.py


示例13: 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


示例14: test

def test():
    window = 'blackman'
    t = -40
    fs = 44100
    a = [101, 200, 440]
    k = 1
    matched = False
    while True:
        M = (100*k) + 1

        N = int(pow(2, np.ceil(np.log2(M))))  

        w = get_window(window, M)

        for f in np.arange(100,8000):
        #for i in range(len(a)):
            #f = a[i]
            x = generateSine(f)

            hx = len(x) / 2
            x1 = x[(.5*fs)-(M/2):(.5*fs)+((M/2)+1)]
            #x1 = x[hx-(M/2):hx+(M/2)+1]
        
            mX, pX = DFT.dftAnal(x1, w, N)
            ploc = UF.peakDetection(mX, t)
            pmag = mX[ploc]

            (iploc, ipmag, ipphase) = UF.peakInterp(mX, pX, ploc)

            fEst = (fs * float(np.sum(iploc))) / float(N)
            esterror = np.abs(fEst - f)
            print esterror

            if (esterror > 0.05):
                matched = False
                break
            else:
                matched = True
        
        if matched:
            break
        else:
            k += 1
    
    print fEst 
    print k
    print M
    print N
开发者ID:yashiro32,项目名称:audio_signal_processing,代码行数:48,代码来源:get_window_size.py


示例15: sineModelAnal

def sineModelAnal(x, fs, w, N, H, t, maxnSines = 100, minSineDur=.01, freqDevOffset=20, freqDevSlope=0.01):
  # Analysis of a sound using the sinusoidal model
  # x: input array sound, w: analysis window, N: size of complex spectrum,
  # H: hop-size, 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 sinusoids
  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                                               # 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([])
  while pin<pend:                                         # while input sound pointer is within sound            
    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
    ipfreq = fs*iploc/float(N)
    tfreq, tmag, tphase = UF.sineTracking(ipfreq, ipmag, ipphase, 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 pin == hM1:
      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 = UF.cleaningSineTracks(xtfreq, round(fs*minSineDur/H))
  return xtfreq, xtmag, xtphase
开发者ID:john36590,项目名称:sms-tools,代码行数:46,代码来源:sineModel.py


示例16: f0Detection

def f0Detection(x, fs, w, N, H, t, minf0, maxf0, f0et):
    """
    Fundamental frequency detection of a sound using twm algorithm
    x: input sound; fs: sampling rate; w: analysis window; 
    N: FFT size; t: threshold in negative dB, 
    minf0: minimum f0 frequency in Hz, maxf0: maximim f0 frequency in Hz, 
    f0et: error threshold in the f0 detection (ex: 5),
    returns f0: fundamental frequency
    """
    if (minf0 < 0):                                            # raise exception if minf0 is smaller than 0
        raise ValueError("Minumum fundamental frequency (minf0) smaller than 0")
    
    if (maxf0 >= 10000):                                       # raise exception if maxf0 is bigger than fs/2
        raise ValueError("Maximum fundamental frequency (maxf0) bigger than 10000Hz")
    
    if (H <= 0):                                               # raise error if hop size 0 or negative
        raise ValueError("Hop size (H) smaller or equal to 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(hM1))                             # 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
    f0 = []                                                    # initialize f0 output
    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 Hez
        f0t = 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
        f0 = np.append(f0, f0t)                                  # add f0 to output array
        pin += H                                                 # advance sound pointer
    return f0
开发者ID:Jee-Bee,项目名称:ASPFMA,代码行数:45,代码来源:A6Part4.py


示例17: minFreqEstErr

def minFreqEstErr(inputFile, f):
    """
    Inputs:
            inputFile (string) = wav file including the path
            f (float) = frequency of the sinusoid present in the input audio signal (Hz)
    Output:
            fEst (float) = Estimated frequency of the sinusoid (Hz)
            M (int) = Window size
            N (int) = FFT size
    """
    # analysis parameters:
    window = 'blackman'
    t = -40
    
    ### Your code here
    (fs, x) = UF.wavread(inputFile)    

    numbins = 6
    k = 21
    #M = int(numbins * fs / f)
    
    
    M = (100*k) + 1

    N = int(pow(2, np.ceil(np.log2(M))))  

    w = get_window(window, M)

    hx = len(x) / 2
    x1 = x[(.5*fs)-(M/2):(.5*fs)+((M/2)+1)]
    #x1 = x[hx-(M/2):hx+(M/2)+1]
        
    mX, pX = DFT.dftAnal(x1, w, N)
    ploc = UF.peakDetection(mX, t)
    pmag = mX[ploc]

    (iploc, ipmag, ipphase) = UF.peakInterp(mX, pX, ploc)

    fEst = (fs * float(np.sum(iploc))) / float(N)
    
    esterror = np.abs(fEst - f)
    print esterror

    return (fEst, M, N)
开发者ID:yashiro32,项目名称:audio_signal_processing,代码行数:44,代码来源:get_window_size.py


示例18: minFreqEstErr

def minFreqEstErr(inputFile, f):
    """
    Inputs:
            inputFile (string) = wav file including the path
            f (float) = frequency of the sinusoid present in the input audio signal (Hz)
    Output:
            fEst (float) = Estimated frequency of the sinusoid (Hz)
            M (int) = Window size
            N (int) = FFT size
    """
    # analysis parameters:
    window = 'blackman'
    t = -40
    
    ### Your code here
    (fs, x) = UF.wavread(inputFile)
    print fs
    #k = find_k(100, 2000, fs, window)
    k=1
    
    while check_k(100, 2000, fs, k, window) < 2:
        print k
        k+=1
   
    M = 100*k+1
    print "M: ", M
    i=0
    while (2**i) < M:
        i+=1
    N = 2**i
    print "N: ", N
    print "fs: ", fs
    print "length: ", len(x)
    center = len(x)/2
    h = M/2
    x_cnk = x[center-h:center+h+1]
    print "chunk: ", len(x_cnk)
    w = get_window(window, M)
    (mX, pX) = DFT.dftAnal(x_cnk, w, N)
    p_loc = UF.peakDetection(mX, t)
    p_int = UF.peakInterp(mX, pX, p_loc)
    peak = p_int[0]*(fs/float(N))
    return (peak[0], M, N)
开发者ID:akkeh,项目名称:SOGM_jr3,代码行数:43,代码来源:A5Part1.py


示例19: minFreqEstErr

def minFreqEstErr(inputFile, f):
    """
    Inputs:
            inputFile (string) = wav file including the path
            f (float) = frequency of the sinusoid present in the input audio signal (Hz)
    Output:
            fEst (float) = Estimated frequency of the sinusoid (Hz)
            M (int) = Window size
            N (int) = FFT size
    """
    # analysis parameters:
    window = 'blackman'
    t = -40

    ### Your code here
    (fs, x) = UF.wavread(inputFile)
    offset = 0.5
    center = offset * fs
    x_center = len(x) / 2

    k = 2
    while True:
        M = 100 * k + 1
        N = smallest_power_of_2_greater_than(M)
        hM1 = int(math.floor((M+1)/2))
      

鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Python utilFunctions.peakInterp函数代码示例发布时间:2022-05-26
下一篇:
Python utilFunctions.genSpecSines函数代码示例发布时间: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