joelle-o-world / mfcc

Calculate MFCC (Mel-frequency Cepstral Coefficients) from mic input in the browser. (TypeScript)
1 stars 0 forks source link

How does zero padding work in numpy fft? #7

Open joelle-o-world opened 5 years ago

joelle-o-world commented 5 years ago

In python_speech_features's mfcc - which we are taking as our yardstick - winlen (window length) and nfft (fft size are independent parameters).

According to the documentation, _raw_fft zero pads frames which are shorter than the fft size.

The question is: Are they zero padded at the end (which would be simplest), at either side (which would make sense from a symetry/periodicity perspective) or at the beginning (which would make no sense at all.

joelle-o-world commented 5 years ago
def _raw_fft(a, n, axis, is_real, is_forward, fct):
    axis = normalize_axis_index(axis, a.ndim)
    if n is None:
        n = a.shape[axis]

    if n < 1:
        raise ValueError("Invalid number of FFT data points (%d) specified."
                         % n)

    if a.shape[axis] != n:
        s = list(a.shape)
        if s[axis] > n:
            index = [slice(None)]*len(s)
            index[axis] = slice(0, n)
            a = a[tuple(index)]
        else:
            index = [slice(None)]*len(s)
            index[axis] = slice(0, s[axis])
            s[axis] = n
            z = zeros(s, a.dtype.char)
            z[tuple(index)] = a
            a = z

    if axis == a.ndim-1:
        r = pfi.execute(a, is_real, is_forward, fct)
    else:
        a = swapaxes(a, axis, -1)
        r = pfi.execute(a, is_real, is_forward, fct)
        r = swapaxes(r, axis, -1)
    return r

from https://github.com/numpy/numpy/blob/v1.17.0/numpy/fft/pocketfft.py#L282-L368

joelle-o-world commented 5 years ago

@fahimsun perhaps you can answer this. You're probably more fluent in python than I am