def dense_gauss_kernel(sigma, x, y=None):
    """
    Gaussian Kernel with dense sampling.
    Evaluates a gaussian kernel with bandwidth SIGMA for all displacements
    between input images X and Y, which must both be MxN. They must also
    be periodic (ie., pre-processed with a cosine window). The result is
    an MxN map of responses.

    If X and Y are the same, ommit the third parameter to re-use some
    values, which is faster.
    """

    xf = pylab.fft2(x)  # x in Fourier domain
    x_flat = x.flatten()
    xx = pylab.dot(x_flat.transpose(), x_flat)  # squared norm of x

    if y is not None:
        # general case, x and y are different
        yf = pylab.fft2(y)
        y_flat = y.flatten()
        yy = pylab.dot(y_flat.transpose(), y_flat)
    else:
        # auto-correlation of x, avoid repeating a few operations
        yf = xf
        yy = xx

    # cross-correlation term in Fourier domain
    xyf = pylab.multiply(xf, pylab.conj(yf))

    # to spatial domain
    xyf_ifft = pylab.ifft2(xyf)
    #xy_complex = circshift(xyf_ifft, floor(x.shape/2))
    row_shift, col_shift = pylab.floor(pylab.array(x.shape)/2).astype(int)
    xy_complex = pylab.roll(xyf_ifft, row_shift, axis=0)
    xy_complex = pylab.roll(xy_complex, col_shift, axis=1)
    xy = pylab.real(xy_complex)

    # calculate gaussian response for all positions
    scaling = -1 / (sigma**2)
    xx_yy = xx + yy
    xx_yy_2xy = xx_yy - 2 * xy
    k = pylab.exp(scaling * pylab.maximum(0, xx_yy_2xy / x.size))

    #print("dense_gauss_kernel x.shape ==", x.shape)
    #print("dense_gauss_kernel k.shape ==", k.shape)

    return k
Example #2
0
def calcaV(W,method = "ratio"):
    """Calculate aV"""
    if method == "ratio":
        return pl.log(pl.absolute(W/pl.roll(W,-1,axis=1)))
    else:
        aVs = pl.zeros(pl.shape(W))
        n = pl.arange(1,pl.size(W,axis=1)+1)
        f = lambda b,t,W: W - b[0] * pl.exp(-b[1] * t)
        
        for i in xrange(pl.size(W,axis=0)):
            params,result = optimize.leastsq(f,[1.,1.],args=(n,W[i]))
            aVs[i] = params[1] * pl.ones(pl.shape(W[i]))
            
        return aVs
Example #3
0
 def _extract_onsets(self):
     """
     ::
     
        The simplest onset detector in the world: power envelope derivative zero crossings +/-
     """
     fp = self._check_feature_params()
     if not self._have_power:
         return None
     dd = P.diff(P.r_[0,self.POWER])
     self.ONSETS = P.where((dd>0) & (P.roll(dd,-1)<0))[0]
     if self.verbosity:
         print "Extracted ONSETS"
     self._have_onsets = True
     return True
Example #4
0
def autoCor(Ps,t):
    """Calculates autocorrelation function"""
    meanP = pl.mean(Ps)
    return pl.mean((Ps - meanP) * (pl.roll(Ps,-t) - meanP))
    def 取音訊且顯示頻譜於幕(它, 鍵盤= None):

        能量=    它.音.en
        頻率=    它.音.f0 # 也可用 .fm, .f0

        #頻率 *=2

        長度= 能量**0.5 *0.1

        x= 10 # 它.幕寬//2 # int(t*0.1)%它.幕寬

        y= 頻率 * 它.幕高  #
        y= 它.幕高 - y     #

        色= 頻率轉顏色(頻率)

        方形= (x, y, 長度, 10)

        #pg.draw.rect(它.幕, 色, 方形)

        #
        # 主要 畫頻譜 的技術 在此! 另有 調色盤 要在 啟動音訊時 先做好。
        #
        頻譜= 它.音.specgram

        #
        # up_down flip, 頻譜上下對調,讓低頻在下,高頻在上,比較符合直覺。
        #
        頻譜= 頻譜[:,-1::-1]

        #
        # 這個 頻譜 大小要如何自動調整才能恰當的呈現在螢幕上,還有待研究一下。
        #
        頻譜= (pl.log(頻譜)+10)*10

        #
        # 錦上添花
        #
        # 加這行讓頻譜會轉,有趣!!
        #
        if 鍵盤 == K_e:
            頻譜= pl.roll(頻譜, -int(它.音.frameI % 它.音.frameN), axis=0)

        #
        # pygame 的 主要貢獻:  頻譜 ---> 音幕
        #
        pg.surfarray.blit_array(它.音幕, 頻譜.astype('int'))

        #
        # pygame 的 次要貢獻: 調整一下 寬高 音幕 ---> aSurf
        #
        aSurf= pg.transform.scale(它.音幕, (它.幕寬, 它.幕高)) #//4))

        #
        # 黏上幕  aSurf ---> display
        #

        #aSurf= pg.transform.average_surfaces([aSurf, 它.攝影畫面])
        它.幕.blit(aSurf, (0,0))

        pg.draw.rect(它.幕, 色, 方形)  
        #這行是音高 f0, 原本在之前畫,但發現會被頻譜擋住,就移到此處。

        #
        # 把攝影畫面 黏上去,1/4 螢幕就好了,不要太大,以免宣賓奪主。
        #
        #### bSurf= pg.transform.scale(它.攝影畫面, (它.幕寬//4, 它.幕高//4)) #//4))
        #### 它.幕.blit(bSurf, (0,0))

        #
        # 江永進的建議,在頻譜前畫一條白線,並把能量、頻率軌跡畫出。
        #
        if 鍵盤 == K_f:
            x= (它.音.frameI % 它.音.frameN)  * 它.幕寬 / 它.音.frameN
            h= 它.幕高
            pg.draw.line(它.幕, pg.Color('white'),(x,h),(x,0) , 2)

            y= 長度
            y= h-y

            z= 頻率
            z= h-z

            if 它.音.frameI % 它.音.frameN == 0:
                它.能量點列表 = [(x,y)]
                它.頻率點列表 = [(x,z)]
            else:
                if 它.能量點列表[-1] != (x,y):
                    它.能量點列表 += [(x,y)]
                if 它.頻率點列表[-1] != (x,z):
                    它.頻率點列表 += [(x,z)]
                pass

            if len(它.能量點列表)>1 :
                pg.draw.lines(它.幕, pg.Color('black'), False, 它.能量點列表, 1)

            if len(它.頻率點列表)>1:
                pg.draw.lines(它.幕, pg.Color('blue'),  False,  它.頻率點列表, 2)