def test_matches_fftfreq(self):
     samplerate = SR44100()
     n_bands = 2048
     fft_freqs = np.fft.rfftfreq(n_bands, 1 / int(samplerate))
     bands = LinearScale.from_sample_rate(samplerate, n_bands // 2)
     linear_freqs = np.array([b.start_hz for b in bands])
     np.testing.assert_allclose(linear_freqs, fft_freqs[:-1])
Exemple #2
0
def fft(x, axis=-1, padding_samples=0):
    """
    Apply an FFT along the given dimension, and with the specified amount of
    zero-padding

    Args:
        x (ArrayWithUnits): an :class:`~zounds.core.ArrayWithUnits` instance
            which has one or more :class:`~zounds.timeseries.TimeDimension`
            axes
        axis (int): The axis along which the fft should be applied
        padding_samples (int): The number of padding zeros to apply along
            axis before performing the FFT
    """
    if padding_samples > 0:
        padded = np.concatenate(
            [x, np.zeros((len(x), padding_samples), dtype=x.dtype)], axis=axis)
    else:
        padded = x

    transformed = np.fft.rfft(padded, axis=axis, norm='ortho')

    sr = audio_sample_rate(int(Seconds(1) / x.dimensions[axis].frequency))
    scale = LinearScale.from_sample_rate(sr, transformed.shape[-1])
    new_dimensions = list(x.dimensions)
    new_dimensions[axis] = FrequencyDimension(scale)
    return ArrayWithUnits(transformed, new_dimensions)
Exemple #3
0
    def _process(self, data):
        transformed = self._process_raw(data)

        sr = audio_sample_rate(data.dimensions[1].samples_per_second)
        scale = LinearScale.from_sample_rate(sr, transformed.shape[1])

        yield ArrayWithUnits(
            transformed,
            [data.dimensions[0], FrequencyDimension(scale)])
Exemple #4
0
 def _process(self, data):
     raw = self._process_raw(data)
     sr = audio_sample_rate(
         int(data.shape[1] / data.dimensions[0].duration_in_seconds))
     scale = LinearScale.from_sample_rate(
         sr, data.shape[1], always_even=self.scale_always_even)
     yield ArrayWithUnits(
         raw,
         [data.dimensions[0], FrequencyDimension(scale)])
Exemple #5
0
    def _process(self, data):
        transformed = dct(data, norm='ortho', axis=self._axis)

        sr = audio_sample_rate(
            int(data.shape[1] / data.dimensions[0].duration_in_seconds))
        scale = LinearScale.from_sample_rate(
            sr, transformed.shape[-1], always_even=self.scale_always_even)

        yield ArrayWithUnits(
            transformed,
            [data.dimensions[0], FrequencyDimension(scale)])
 def test_can_get_all_even_sized_bands(self):
     samplerate = SR44100()
     scale = LinearScale.from_sample_rate(samplerate,
                                          44100,
                                          always_even=True)
     log_scale = GeometricScale(20, 20000, 0.01, 64)
     slices = [scale.get_slice(band) for band in log_scale]
     sizes = [s.stop - s.start for s in slices]
     self.assertTrue(
         not any([s % 2 for s in sizes]),
         'All slice sizes should be even but were {sizes}'.format(
             **locals()))