datastft.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:jrm_ssl 作者: Fhrozen 项目源码 文件源码
def STFT(x, wlen, h, nfft, fs): 
    ########################################################
    #              Short-Time Fourier Transform            %
    #               with MATLAB Implementation             %
    #                  For Python                          %
    # Copier: Nelson Yalta                       11/03/15  %
    ########################################################
    # function: [stft, f, t] = stft(x, wlen, h, nfft, fs)
    # x - signal in the time domain
    # wlen - length of the hamming window
    # h - hop size
    # nfft - number of FFT points
    # fs - sampling frequency, Hz
    # f - frequency vector, Hz
    # t - time vector, s
    # stft - STFT matrix (only unique points, time across columns, freq across rows)
    # represent x as column-vector if it is not

    if (len(x.shape) > 1) and (x.shape[1] > 1):
            x = x.transpose()

    # length of the signal
    xlen = x.shape[0]

    # form a periodic hamming window
    win = hamming(wlen, False)
    # form the stft matrix
    rown = int(np.ceil((1.0+nfft)/2))
    coln = int(np.fix((xlen-wlen)/h) + 1)
    short_tft = np.zeros((rown,coln)).astype('complex64')

    # initialize the indexes
    indx = 0
    col = 0

    # perform STFT
    while (indx + wlen <= xlen):
        # windowing

        xw =x[indx:indx+wlen]*win

        # FFT
        X = np.fft.fft(xw,nfft)

        # update the stft matrix
        short_tft[:,col] = X[0:rown]

        # update the indexes
        indx +=  h
        col += 1

    # calculate the time and frequency vectors

    t = np.linspace(wlen/2,wlen/2+(coln-1)*h,coln)/fs
    f = np.arange(0,rown,dtype= np.float32)*fs/nfft   

    return short_tft, f, t
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号