Python scipy.signal.hamming() Examples

The following are 8 code examples of scipy.signal.hamming(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.signal , or try the search function .
Example #1
Source File: audio.py    From asr-study with MIT License 7 votes vote down vote up
def __init__(self, win_len=0.025, win_step=0.01,
                 num_filt=40, nfft=512, low_freq=20, high_freq=7800,
                 pre_emph=0.97, win_fun=signal.hamming, **kwargs):

        super(FBank, self).__init__(**kwargs)

        if high_freq > self.fs / 2:
            raise ValueError("high_freq must be less or equal than fs/2")

        self.win_len = win_len
        self.win_step = win_step
        self.num_filt = num_filt
        self.nfft = nfft
        self.low_freq = low_freq
        self.high_freq = high_freq or self.fs / 2
        self.pre_emph = pre_emph
        self.win_fun = win_fun
        self._filterbanks = self._get_filterbanks()

        self._num_feats = self.num_filt 
Example #2
Source File: cls_fe_dft.py    From signaltrain with GNU General Public License v3.0 5 votes vote down vote up
def initialize(self):
        f_matrix = np.fft.fft(np.eye(self.sz), norm='ortho')
        w = sig.hamming(self.sz)

        f_matrix_real = (np.real(f_matrix) * w).astype(np.float32, copy=False)
        f_matrix_imag = (np.imag(f_matrix) * w).astype(np.float32, copy=False)

        if torch.has_cudnn:
            self.conv_analysis_real.weight.data.copy_(torch.from_numpy(f_matrix_real[:, None, :]).cuda())
            self.conv_analysis_imag.weight.data.copy_(torch.from_numpy(f_matrix_imag[:, None, :]).cuda())
        else:
            self.conv_analysis_real.weight.data.copy_(torch.from_numpy(f_matrix_real[:, None, :]))
            self.conv_analysis_imag.weight.data.copy_(torch.from_numpy(f_matrix_imag[:, None, :])) 
Example #3
Source File: cls_fe_dft.py    From signaltrain with GNU General Public License v3.0 5 votes vote down vote up
def GLA(wsz, hop, N=4096):
        """ LSEE-MSTFT algorithm for computing the synthesis window used in
        inverse STFT method.
        Args:
            wsz :   (int)    Synthesis window size
            hop :   (int)    Hop size
            N   :   (int)    DFT Size
        Returns :
            symw:   (array)  Synthesised windowing function
        References :
            [1] Daniel W. Griffin and Jae S. Lim, ``Signal estimation from modified short-time
            Fourier transform,'' IEEE Transactions on Acoustics, Speech and Signal Processing,
            vol. 32, no. 2, pp. 236-243, Apr 1984.
        """
        synw = sig.hamming(wsz)
        synwProd = synw ** 2.
        synwProd.shape = (wsz, 1)
        redundancy = wsz // hop
        env = np.zeros((wsz, 1))
        for k in range(-redundancy, redundancy + 1):
            envInd = (hop * k)
            winInd = np.arange(1, wsz + 1)
            envInd += winInd
            valid = np.where((envInd > 0) & (envInd <= wsz))
            envInd = envInd[valid] - 1
            winInd = winInd[valid] - 1
            env[envInd] += synwProd[winInd]

        synw = synw / env[:, 0]
        return synw 
Example #4
Source File: normhamming.py    From speaker_extraction with GNU General Public License v3.0 5 votes vote down vote up
def normhamming(fft_len):
    win = numpy.sqrt(hamming(fft_len, False))
    win = win/numpy.sqrt(numpy.sum(numpy.power(win[0:fft_len:FRAME_SHIFT],2)))
    return win 
Example #5
Source File: signal_transforms.py    From autosynch with GNU General Public License v3.0 5 votes vote down vote up
def _gl_alg(window_size, hop, fft_size=4096):
    """LSEE-MSTFT algorithm for computing the synthesis window.

    According to: Daniel W. Griffin and Jae S. Lim, `Signal estimation\
    from modified short-time Fourier transform,` IEEE Transactions on\
    Acoustics, Speech and Signal Processing, vol. 32, no. 2, pp. 236-243,\
    Apr 1984.

    :param window_size: Synthesis window size in samples.
    :type window_size: int
    :param hop: Hop size in samples.
    :type hop: int
    :param fft_size: FTT size
    :type fft_size: int
    :return: The synthesized window
    :rtype: numpy.core.multiarray.ndarray
    """
    syn_w = signal.hamming(window_size) / np.sqrt(fft_size)
    syn_w_prod = syn_w ** 2.
    syn_w_prod.shape = (window_size, 1)
    redundancy = int(window_size / hop)
    env = np.zeros((window_size, 1))

    for k in range(-redundancy, redundancy + 1):
        env_ind = (hop * k)
        win_ind = np.arange(1, window_size + 1)
        env_ind += win_ind

        valid = np.where((env_ind > 0) & (env_ind <= window_size))
        env_ind = env_ind[valid] - 1
        win_ind = win_ind[valid] - 1
        env[env_ind] += syn_w_prod[win_ind]

    syn_w = syn_w / env[:, 0]

    return syn_w 
Example #6
Source File: TFMethods.py    From ASP with GNU General Public License v3.0 5 votes vote down vote up
def GLA(wsz, hop):
        """ LSEE-MSTFT algorithm for computing the synthesis window used in
        inverse STFT method below.
        Args:
            wsz :   (int)    Synthesis Window size
            hop :   (int)    Hop size
        Returns :
            symw:   (array) Synthesised time-domain real signal.

        References :
            [1] Daniel W. Griffin and Jae S. Lim, ``Signal estimation from modified short-time
            Fourier transform,'' IEEE Transactions on Acoustics, Speech and Signal Processing,
            vol. 32, no. 2, pp. 236-243, Apr 1984.
        """
        synw = hamming(wsz)/np.sum(hamming(wsz))
        synwProd = synw ** 2.
        synwProd.shape = (wsz, 1)
        redundancy = wsz/hop
        env = np.zeros((wsz, 1))
        for k in xrange(-redundancy, redundancy + 1):
            envInd = (hop*k)
            winInd = np.arange(1, wsz+1)
            envInd += winInd

            valid = np.where((envInd > 0) & (envInd <= wsz))
            envInd = envInd[valid] - 1
            winInd = winInd[valid] - 1
            env[envInd] += synwProd[winInd]

        synw = synw/env[:, 0]
        return synw 
Example #7
Source File: mel_coefficients.py    From Speaker-Recognition with MIT License 5 votes vote down vote up
def mfcc(s,fs, nfiltbank):
  
    #divide into segments of 25 ms with overlap of 10ms
    nSamples = np.int32(0.025*fs)
    overlap = np.int32(0.01*fs)
    nFrames = np.int32(np.ceil(len(s)/(nSamples-overlap)))
    #zero padding to make signal length long enough to have nFrames
    padding = ((nSamples-overlap)*nFrames) - len(s)
    if padding > 0:
        signal = np.append(s, np.zeros(padding))
    else:
        signal = s
    segment = np.empty((nSamples, nFrames))
    start = 0
    for i in range(nFrames):
        segment[:,i] = signal[start:start+nSamples]
        start = (nSamples-overlap)*i
    
    #compute periodogram
    nfft = 512
    periodogram = np.empty((nFrames, int(nfft/2 + 1)))
    for i in range(nFrames):
        x = segment[:,i] * hamming(nSamples)
        spectrum = fftshift(fft(x,nfft))
        periodogram[i,:] = abs(spectrum[int(nfft/2-1):])/nSamples
        
    #calculating mfccs    
    fbank = mel_filterbank(nfft, nfiltbank, fs)
    #nfiltbank MFCCs for each frame
    mel_coeff = np.empty((nfiltbank,nFrames))
    for i in range(nfiltbank):
        for k in range(nFrames):
            mel_coeff[i,k] = np.sum(periodogram[k,:]*fbank[:,i])
            
    mel_coeff = np.log10(mel_coeff)
    mel_coeff = dct(mel_coeff)
    #exclude 0th order coefficient (much larger than others)
    mel_coeff[0,:]= np.zeros(nFrames)
    return mel_coeff 
Example #8
Source File: data_feeder.py    From autosynch with GNU General Public License v3.0 4 votes vote down vote up
def data_feeder_testing(window_size, fft_size, hop_size, seq_length, context_length,
                        batch_size, debug, sources_list=None):
    """Provides an iterator over the testing examples.

    :param window_size: The window size to be used for the time-frequency transformation.
    :type window_size: int
    :param fft_size: The size of the FFT in samples.
    :type fft_size: int
    :param hop_size: The hop size in samples.
    :type hop_size: int
    :param seq_length: The sequence length in frames.
    :type seq_length: int
    :param context_length: The context length in frames.
    :type context_length: int
    :param batch_size: The batch size.
    :type batch_size: int
    :param debug: A flag to indicate debug
    :type debug: bool
    :param sources_list: The file list provided for using the MaD-TwinNet.
    :type sources_list: list[str]
    :return: An iterator that will provide the input and target values.\
             The iterator yields (mix, mix magnitude, mix phase, voice true, bg true) values.
    :rtype: callable
    """
    if sources_list is None:
        usage_case = False
        sources_list = _get_files_lists('testing')[-1]
    else:
        usage_case = True
    hamming_window = signal.hamming(window_size, True)

    def testing_it():

        for index in range(len(sources_list)):
            yield _get_data_testing(
                sources_parent_path=sources_list[index],
                window_values=hamming_window, fft_size=fft_size, hop=hop_size,
                seq_length=seq_length, context_length=context_length,
                batch_size=batch_size, usage_case=usage_case
            )

            if debug:
                break

    return testing_it