コード例 #1
0
 def test_resample_wav(self):
     for fn in wav_fns:
         t = self.tmpdir()
         tmp_path = os.path.join(t, os.path.basename(fn))
         shutil.copy(fn, tmp_path)
         s = SoundFile(tmp_path, resample_freq=16000)
         data, data_int, fs = wavread(s.wavpath_rs)
         resample_fn = os.path.splitext(os.path.basename(fn))[0] + '-resample-16kHz.wav'
         data_test, data_test_int, fs_test = wavread(data_file_path(os.path.join('soundfile', 'resample', resample_fn)))
         self.assertEqual(fs, fs_test)
         self.assertEqual(len(data), len(data_test))
         self.assertAllClose(data, data_test, rtol=1e-05, atol=1e-08)
コード例 #2
0
 def test_load_wav_file(self):
     spath = data_file_path('beijing_f3_50_a.wav')
     s = SoundFile(spath)
     self.assertEqual(s.wavpath, spath)
     data, fs = wavread(spath)
     np.testing.assert_array_equal(data, s.wavdata)
     self.assertEqual(fs, s.fs)
コード例 #3
0
 def test_with_matlab_data(self):
     data = load_json(os.path.join('shrp', 'shr_pitch_data'))
     wav_data, wavdata_int, fps = wavread(
         sound_file_path('beijing_f3_50_a.wav'))
     shr, f0 = shr_pitch(wav_data, fps, 25, 1, 50, 550, 0.4, 5, 200)
     np.testing.assert_array_almost_equal(f0, data['F0'])
     np.testing.assert_array_almost_equal(shr, data['SHR'])
コード例 #4
0
 def test_wavread(self):
     fn = sound_file_path('beijing_f3_50_a.wav')
     samples, samples_int, Fs = wavread(fn)
     expected = load_json(
         os.path.join('helpers', 'beijing_f3_50_a-wavread-expected'))
     self.assertEqual(Fs, expected['Fs'])
     self.assertAllClose(samples, expected['y'], rtol=1e-05, atol=1e-08)
コード例 #5
0
 def test_wavread(self):
     fn = data_file_path('beijing_f3_50_a.wav')
     samples, Fs = wavread(fn)
     expected = loadmat('beijing_f3_50_a-wavread-expected')
     self.assertEqual(Fs, expected['Fs'])
     # XXX may need to use allclose here instead of array_equal.
     if not np.array_equal(samples, expected['y']):
         # Produce a useful error message for debugging.
         self.assertEqual(list(samples), list(expected['y']))
コード例 #6
0
    def test_with_min_max_pitch_not_specified(self):
        data = load_json(os.path.join('shrp', 'shr_pitch_data'))
        wav_data, wavdata_int, fps = wavread(sound_file_path('beijing_f3_50_a.wav'))

        with self.assertRaisesRegex(ValueError, 'none or both of min_pitch, max_pitch must be specified'):
            shr, f0 = shr_pitch(wav_data, fps, min_pitch=50, datalen=200)

        with self.assertRaisesRegex(ValueError, 'none or both of min_pitch, max_pitch must be specified'):
            shr, f0 = shr_pitch(wav_data, fps, max_pitch=550, datalen=200)
コード例 #7
0
    def test_with_min_max_pitch_not_specified(self):
        data = load_json(os.path.join('shrp', 'shr_pitch_data'))
        wav_data, wavdata_int, fps = wavread(
            sound_file_path('beijing_f3_50_a.wav'))

        with self.assertRaisesRegex(
                ValueError,
                'none or both of min_pitch, max_pitch must be specified'):
            shr, f0 = shr_pitch(wav_data, fps, min_pitch=50, datalen=200)

        with self.assertRaisesRegex(
                ValueError,
                'none or both of min_pitch, max_pitch must be specified'):
            shr, f0 = shr_pitch(wav_data, fps, max_pitch=550, datalen=200)
コード例 #8
0
 def test_load_wav_file(self):
     spath = sound_file_path('beijing_f3_50_a.wav')
     s = SoundFile(spath)
     self.assertEqual(s.wavpath, spath)
     data, data_int, fs = wavread(spath)
     self.assertAllClose(data, s.wavdata)
     self.assertTrue(s.wavdata_int.dtype == 'int16')
     self.assertAllClose(data, s.wavdata_int/np.float64(32768.0), rtol=1e-05, atol=1e-08)
     self.assertEqual(fs, s.fs)
     self.assertEqual(s.fs, 22050)
     self.assertEqual(s.ns, 51597)
     self.assertEqual(s.ms_len, 2340)
     self.assertIsNone(s.wavdata_rs)
     self.assertIsNone(s.fs_rs)
     self.assertIsNone(s.ns_rs)
コード例 #9
0
 def test_resample_properties(self):
     fn = 'beijing_f3_50_a.wav'
     t = self.tmpdir()
     tmp_path = os.path.join(t, fn)
     shutil.copy(sound_file_path(fn), tmp_path)
     s = SoundFile(tmp_path, resample_freq=16000)
     self.assertEqual(s.fs, 22050)
     self.assertEqual(s.ns, 51597)
     self.assertEqual(s.ms_len, 2340)
     self.assertEqual(os.path.basename(s.wavpath_rs), 'beijing_f3_50_a-resample-16000Hz.wav')
     self.assertEqual(s.fs_rs, 16000)
     self.assertEqual(s.ns_rs, 37440)
     y_rs, y_rs_int, fs_rs = wavread(s.wavpath_rs)
     self.assertEqual(fs_rs, s.fs_rs)
     self.assertEqual(len(y_rs), s.ns_rs)
     self.assertAllClose(y_rs * 32768, np.int16(s.wavdata_rs * 32768))
コード例 #10
0
 def test_wavread_formats(self):
     # 16-bit PCM file should be read correctly
     fn = data_file_path(os.path.join('helpers', 'wav-formats', 'pcm-16bit.wav'))
     samples, samples_int, Fs = wavread(fn)
     # Other WAV file formats should raise IOError
     with self.assertRaisesRegex(IOError, 'Input WAV file must be in 16-bit integer PCM format'):
         fn = data_file_path(os.path.join('helpers', 'wav-formats', 'pcm-8bit.wav'))
         samples, samples_int, Fs = wavread(fn)
     with self.assertRaisesRegex(IOError, 'Input WAV file must be in 16-bit integer PCM format'):
         fn = data_file_path(os.path.join('helpers', 'wav-formats', 'pcm-32bit.wav'))
         samples, samples_int, Fs = wavread(fn)
     with self.assertRaisesRegex(IOError, 'Input WAV file must be in 16-bit integer PCM format'):
         fn = data_file_path(os.path.join('helpers', 'wav-formats', 'float-32bit.wav'))
         samples, samples_int, Fs = wavread(fn)
     with self.assertRaisesRegex(IOError, 'Input WAV file must be in 16-bit integer PCM format'):
         fn = data_file_path(os.path.join('helpers', 'wav-formats', 'float-64bit.wav'))
         samples, samples_int, Fs = wavread(fn)
     # SciPy does not support 24-bit PCM format
     with self.assertRaisesRegex(ValueError, 'Unsupported bit depth: the wav file has 24-bit data.'):
         fn = data_file_path(os.path.join('helpers', 'wav-formats', 'pcm-24bit.wav'))
         samples, samples_int, Fs = wavread(fn)
コード例 #11
0
 def test_wavread_formats(self):
     # 16-bit PCM file should be read correctly
     fn = data_file_path(
         os.path.join('helpers', 'wav-formats', 'pcm-16bit.wav'))
     samples, samples_int, Fs = wavread(fn)
     # Other WAV file formats should raise IOError
     with self.assertRaisesRegex(
             IOError,
             'Input WAV file must be in 16-bit integer PCM format'):
         fn = data_file_path(
             os.path.join('helpers', 'wav-formats', 'pcm-8bit.wav'))
         samples, samples_int, Fs = wavread(fn)
     with self.assertRaisesRegex(
             IOError,
             'Input WAV file must be in 16-bit integer PCM format'):
         fn = data_file_path(
             os.path.join('helpers', 'wav-formats', 'pcm-32bit.wav'))
         samples, samples_int, Fs = wavread(fn)
     with self.assertRaisesRegex(
             IOError,
             'Input WAV file must be in 16-bit integer PCM format'):
         fn = data_file_path(
             os.path.join('helpers', 'wav-formats', 'float-32bit.wav'))
         samples, samples_int, Fs = wavread(fn)
     with self.assertRaisesRegex(
             IOError,
             'Input WAV file must be in 16-bit integer PCM format'):
         fn = data_file_path(
             os.path.join('helpers', 'wav-formats', 'float-64bit.wav'))
         samples, samples_int, Fs = wavread(fn)
     # SciPy does not support 24-bit PCM format
     with self.assertRaisesRegex(
             ValueError,
             'Unsupported bit depth: the wav file has 24-bit data.'):
         fn = data_file_path(
             os.path.join('helpers', 'wav-formats', 'pcm-24bit.wav'))
         samples, samples_int, Fs = wavread(fn)
コード例 #12
0
 def test_with_matlab_data(self):
     data = load_json(os.path.join('shrp', 'shr_pitch_data'))
     wav_data, wavdata_int, fps = wavread(sound_file_path('beijing_f3_50_a.wav'))
     shr, f0 = shr_pitch(wav_data, fps, 25, 1, 50, 550, 0.4, 5, 200)
     np.testing.assert_array_almost_equal(f0, data['F0'])
     np.testing.assert_array_almost_equal(shr, data['SHR'])
コード例 #13
0
def main(wav_dir, fs_rs):
    """Compare original data vs resampled data for all wav files in wav_dir,
    where resampling frequency is given in Hz by fs_rs
    """
    # Find all .wav files in test/data directory
    wav_files = glob.glob(os.path.join(wav_dir, '*.wav'))

    for wav_file in wav_files:
        print('Processing wav file {}'.format(wav_file))
        # y is data points, fs is sampling frequency
        y, y_int, fs = wavread(wav_file)
        # ns is number of samples
        ns = len(y)
        period = 1.0 / fs
        # Time points corresponding to samples
        t = np.arange(0, ns * period, period)

        # Resample to 16 kHz
        period_rs = 1.0 / fs_rs
        # Number of points in resample
        ns_rs = np.int_(np.ceil(ns * fs_rs / fs))
        # Do resample
        y_rs, t_rs = resample(y, ns_rs, t)
        y_h, t_h = resample(y, ns_rs, t, window='hamming')

        # Number of points to plot
        n = 1000
        # Start plotting from this data point
        # Choose random starting point
        s = randint(10000, 35015 - n)

        fig = plt.figure()
        fig.suptitle(os.path.basename(wav_file))
        # Compare original data with resampled ata
        ax = plt.subplot(2, 1, 1)
        ax.plot(t, y, 'b-', markersize=1)
        ax.plot(t_rs, y_rs, 'ro', markersize=3)
        ax.set_xlim([s * period, (s + n) * period])
        ax.set_ylabel('Amplitude')
        ax.set_title('Normal resample')
        # Compare original data with resampling + Hamming window
        ax = plt.subplot(2, 1, 2)
        ax.plot(t, y, 'b-', markersize=1)
        ax.plot(t_h, y_h, 'ro', markersize=3)
        ax.set_xlim([s * period, (s + n) * period])
        ax.set_ylabel('Amplitude')
        ax.set_xlabel('Time (s)')
        ax.set_title('Hamming window')
        plt.savefig(os.path.splitext(os.path.basename(wav_file))[0] + '.pdf')

        fn = os.path.splitext(
            os.path.basename(wav_file))[0] + '-matlab-resample'
        data = load_json(os.path.join('soundfile', fn))
        y_rs_matlab = data['y_rs']

        # Compare SciPy resampled data with Matlab resampled data
        plt.figure()
        plt.plot(np.linspace(0, period * ns, len(y_rs_matlab)),
                 y_rs_matlab,
                 'b-',
                 markersize=1)
        plt.plot(t_rs, y_rs, 'ro', markersize=3)
        plt.xlim(s * period, (s + n) * period)
        plt.title(os.path.basename(wav_file) + ' - Matlab comparison')
        plt.xlabel('Time (s)')
        plt.ylabel('Amplitude')
        plt.savefig(
            os.path.splitext(os.path.basename(wav_file))[0] + '-matlab.pdf')
コード例 #14
0
def main(wav_dir, fs_rs):
    """Compare original data vs resampled data for all wav files in wav_dir,
    where resampling frequency is given in Hz by fs_rs
    """
    # Find all .wav files in test/data directory
    wav_files = glob.glob(os.path.join(wav_dir, '*.wav'))

    for wav_file in wav_files:
        print('Processing wav file {}'.format(wav_file))
        # y is data points, fs is sampling frequency
        y, y_int, fs = wavread(wav_file)
        # ns is number of samples
        ns = len(y)
        period = 1.0 / fs
        # Time points corresponding to samples
        t = np.arange(0, ns*period, period)

        # Resample to 16 kHz
        period_rs = 1.0 / fs_rs
        # Number of points in resample
        ns_rs = np.int_(np.ceil(ns * fs_rs / fs))
        # Do resample
        y_rs, t_rs = resample(y, ns_rs, t)
        y_h, t_h = resample(y, ns_rs, t, window='hamming')

        # Number of points to plot
        n = 1000
        # Start plotting from this data point
        # Choose random starting point
        s = randint(10000, 35015-n)

        fig = plt.figure()
        fig.suptitle(os.path.basename(wav_file))
        # Compare original data with resampled ata
        ax = plt.subplot(2,1,1)
        ax.plot(t, y, 'b-', markersize=1)
        ax.plot(t_rs, y_rs, 'ro', markersize=3)
        ax.set_xlim([s*period, (s+n)*period])
        ax.set_ylabel('Amplitude')
        ax.set_title('Normal resample')
        # Compare original data with resampling + Hamming window
        ax = plt.subplot(2,1,2)
        ax.plot(t, y, 'b-', markersize=1)
        ax.plot(t_h, y_h, 'ro', markersize=3)
        ax.set_xlim([s*period, (s+n)*period])
        ax.set_ylabel('Amplitude')
        ax.set_xlabel('Time (s)')
        ax.set_title('Hamming window')
        plt.savefig(os.path.splitext(os.path.basename(wav_file))[0] + '.pdf')

        fn = os.path.splitext(os.path.basename(wav_file))[0] + '-matlab-resample'
        data = load_json(os.path.join('soundfile', fn))
        y_rs_matlab = data['y_rs']

        # Compare SciPy resampled data with Matlab resampled data
        plt.figure()
        plt.plot(np.linspace(0, period * ns, len(y_rs_matlab)), y_rs_matlab, 'b-', markersize=1)
        plt.plot(t_rs, y_rs, 'ro', markersize=3)
        plt.xlim(s*period, (s+n)*period)
        plt.title(os.path.basename(wav_file) + ' - Matlab comparison')
        plt.xlabel('Time (s)')
        plt.ylabel('Amplitude')
        plt.savefig(os.path.splitext(os.path.basename(wav_file))[0] + '-matlab.pdf')
コード例 #15
0
 def _wavdata(self):
     data, data_int, fs = wavread(self.wavpath)
     self.__dict__['wavdata'], self.__dict__['fs'] = data, fs
     return data, data_int, fs
コード例 #16
0
 def test_wavread(self):
     fn = sound_file_path('beijing_f3_50_a.wav')
     samples, samples_int, Fs = wavread(fn)
     expected = load_json(os.path.join('helpers', 'beijing_f3_50_a-wavread-expected'))
     self.assertEqual(Fs, expected['Fs'])
     self.assertAllClose(samples, expected['y'], rtol=1e-05, atol=1e-08)
コード例 #17
0
 def _wavdata(self):
     data, fs = wavread(self.wavpath)
     self.__dict__['wavdata'], self.__dict__['fs'] = data, fs
     return data, fs
コード例 #18
0
 def test_with_matlab_data(self):
     data = loadmat("shr_pitch_data")
     wav_data, fps = wavread(data_file_path("beijing_f3_50_a.wav"))
     shr, f0 = shr_pitch(wav_data, fps, 25, 1, 50, 550, 0.4, 5, 200)
     np.testing.assert_array_almost_equal(f0, data["F0"])
     np.testing.assert_array_almost_equal(shr, data["SHR"])