Exemplo n.º 1
0
    def write(self, data, scale=True):
        """Write data to the end of the file

        This function can be called serially to store data in chunks. Each chunk
        is appended at the end of the file.

        - data : input data, in any form that can be converted to an array with
                 the file's dtype. Data are silently coerced into an array whose
                 shape matches the number of channels in the file. This means
                 the caller is responsible for checking dimensions in
                 multichannel files.

        - scale : if True, data are rescaled so that their maximum range matches
                    that of the file's encoding. If not, the raw values are
                    used, which can result in clipping.

        """
        from numpy import asarray
        if self.mode == 'r':
            raise IOError('file is read-only')

        if scale:
            data = rescale(data, self._dtype)
        else:
            data = asarray(data, self._dtype)

        self.fp.seek(0, 2)
        self.fp.write(data.flatten().tostring())
        return self
Exemplo n.º 2
0
def load_stimulus(path,
                  window,
                  step,
                  f_min=0.5,
                  f_max=8.0,
                  f_count=30,
                  compress=1,
                  **kwargs):
    """Load sound stimulus and calculate spectrotemporal representation.

    Parameters:

    path: location of wave file
    window: duration of window (in ms)
    step: window step (in ms)
    f_min: minimum frequency (in kHz)
    f_max: maximum frequency (in kHz)
    f_count: number of frequency bins
    gammatone: if True, use gammatone filterbank

    Returns spectrogram, duration (ms)
    """
    fp = ewave.open(path, "r")
    Fs = fp.sampling_rate / 1000.
    osc = ewave.rescale(fp.read(), 'h')
    Pxx = gtgram(osc, Fs * 1000, window / 1000, step / 1000, f_count,
                 f_min * 1000, f_max * 1000)
    Pxx = np.log10(Pxx + compress) - np.log10(compress)
    return Pxx, Pxx.shape[1] * step, step
Exemplo n.º 3
0
def test01_rescalevalues():
    from numpy import array
    tests = ((32767, 'h', 1 - 1 / (1 << 15)),
             (32768, 'h', -1.0),
             ((1 << 31) - 1, 'i', 1 - 1 / (1 << 31)),
             (1 << 31, 'i', -1.0),)
    for val, dtype, expected in tests:
        assert_almost_equal(ewave.rescale(array([val], dtype), 'f')[0], expected)
Exemplo n.º 4
0
def load_stimulus(path,
                  window,
                  step,
                  f_min=0.5,
                  f_max=8.0,
                  f_count=30,
                  compress=1,
                  gammatone=False,
                  **kwargs):
    """Load sound stimulus and calculate spectrotemporal representation.

    Parameters:

    path: location of wave file
    window: duration of window (in ms)
    step: window step (in ms)
    f_min: minimum frequency (in kHz)
    f_max: maximum frequency (in kHz)
    f_count: number of frequency bins
    gammatone: if True, use gammatone filterbank

    Returns spectrogram, duration (ms)
    """
    import ewave
    fp = ewave.open(path, "r")
    Fs = fp.sampling_rate / 1000.
    osc = ewave.rescale(fp.read(), 'h')
    if gammatone:
        import gammatone.gtgram as gg
        Pxx = gg.gtgram(osc, Fs * 1000, window / 1000, step / 1000, f_count,
                        f_min * 1000)
    else:
        import libtfr
        # nfft based on desired number of channels btw f_min and f_max
        nfft = int(f_count / (f_max - f_min) * Fs)
        npoints = int(Fs * window)
        if nfft < npoints:
            raise ValueError(
                "window size {} ({} points) too large for desired freq resolution {}. "
                "Decrease to {} ms or increase f_count.".format(
                    window, f_count, npoints, nfft / Fs))

        nstep = int(Fs * step)
        taper = np.hanning(npoints)
        mfft = libtfr.mfft_precalc(nfft, taper)
        Pxx = mfft.mtspec(osc, nstep)
        freqs, ind = libtfr.fgrid(Fs, nfft, [f_min, f_max])
        Pxx = Pxx[ind, :]
    if compress is not None:
        Pxx = np.log10(Pxx + compress) - np.log10(compress)
    return Pxx, Pxx.shape[1] * step
Exemplo n.º 5
0
def rescale(src_type, tgt_type):
    from numpy import ones,dtype
    d1 = ones(1000,dtype=src_type)
    if dtype(src_type).kind=='f': d1 *= 0.9
    d2 = ewave.rescale(d1, tgt_type)
    assert d2.dtype == dtype(tgt_type)
Exemplo n.º 6
0
def test01_clipping():
    assert_almost_equal(ewave.rescale(ewave.rescale([-1.01], 'h'), 'f')[0], -1.0)
    assert_almost_equal(ewave.rescale(ewave.rescale([1.01], 'i'), 'f')[0], 1.0)
Exemplo n.º 7
0
def test00_rescalebad():
    ewave.rescale([1,2,3],'S2')