Пример #1
0
 def matlab_fn_as_matlab_input_data(self, filename):
     data = load_json(os.path.join('shrp', filename))
     mag, index = two_max(data['x'],
                          int(data['lowerbound']) - 1,
                          int(data['upperbound']) - 1, data['unitlen'])
     np.testing.assert_array_almost_equal(mag, data['mag'])
     np.testing.assert_array_almost_equal(index, int(data['index']) - 1)
Пример #2
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)
Пример #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_with_matlab_data(self):
     data = load_json(os.path.join('shrp', 'toframes_data'))
     res = toframes(data['input'],
                    data['curpos'].astype(int)-1,
                    int(data['segmentlen']),
                    'hamm')
     np.testing.assert_array_almost_equal(res, data['frames'])
Пример #5
0
 def test_with_matlab_data(self):
     data = load_json(os.path.join('shrp', 'GetLogSpectrum_data'))
     interp_amplitude = get_log_spectrum(data['segment'],
                                         int(data['fftlen']),
                                         int(data['limit']) - 1,
                                         data['logf'], data['interp_logf'])
     np.testing.assert_array_almost_equal(interp_amplitude,
                                          data['interp_amplitude'])
Пример #6
0
 def matlab_fn_as_matlab_input_data(self, filename):
     data = load_json(os.path.join('shrp',filename))
     mag, index = two_max(data['x'],
                          int(data['lowerbound'])-1,
                          int(data['upperbound'])-1,
                          data['unitlen'])
     np.testing.assert_array_almost_equal(mag, data['mag'])
     np.testing.assert_array_almost_equal(index, int(data['index'])-1)
Пример #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_with_matlab_data(self):
     data = load_json(os.path.join('shrp', 'GetLogSpectrum_data'))
     interp_amplitude = get_log_spectrum(
         data['segment'],
         int(data['fftlen']),
         int(data['limit']) - 1,
         data['logf'],
         data['interp_logf'])
     np.testing.assert_array_almost_equal(interp_amplitude,
                                          data['interp_amplitude'])
Пример #9
0
 def test_second_peak_index(self):
     data = load_json(os.path.join('shrp', 'twomax_data'))
     x = data['x']
     for i in range(142, 146):
         # Zap the values in the second peak range to exercise the branch.
         x[i] = 0.0001 * i
     mag, index = two_max(x,
                          int(data['lowerbound']) - 1,
                          int(data['upperbound']) - 1, data['unitlen'])
     np.testing.assert_array_almost_equal(mag,
                                          np.append(data['mag'], 0.0145))
     np.testing.assert_array_equal(index, [int(data['index']) - 1, 145])
Пример #10
0
 def test_med_smooth_greater_than_zero(self):
     data = load_json(os.path.join('shrp', 'shrp_data'))
     with self.assertRaises(NotImplementedError):
         f0_time, f0_value, shr, f0_candidates = shrp(
             data['Y'],
             int(data['Fs']), [int(x) for x in data['F0MinMax']],
             int(data['frame_length']),
             int(data['timestep']),
             data['SHR_Threshold'],
             data['ceiling'],
             med_smooth=5,
             CHECK_VOICING=False)
Пример #11
0
 def test_with_matlab_data(self):
     data = load_json(os.path.join('shrp', 'shrp_data'))
     f0_time, f0_value, shr, f0_candidates = shrp(
         data['Y'], int(data['Fs']), [int(x) for x in data['F0MinMax']],
         int(data['frame_length']), int(data['timestep']),
         data['SHR_Threshold'], data['ceiling'], data['med_smooth'],
         data['CHECK_VOICING'])
     np.testing.assert_array_almost_equal(f0_time, data['f0_time'])
     np.testing.assert_array_almost_equal(f0_value, data['f0_value'])
     np.testing.assert_array_almost_equal(shr, data['SHR'])
     np.testing.assert_array_almost_equal(f0_candidates,
                                          data['f0_candidates'])
Пример #12
0
 def matlab_fn_as_matlab_input_data(self, filename):
     data = load_json(os.path.join('shrp', filename))
     peak_index, shr, shshift, index = compute_shr(
         data['log_spectrum'], data['min_bin'],
         data['startpos'].astype(int) - 1, data['endpos'].astype(int) - 1,
         int(data['lowerbound']) - 1,
         int(data['upperbound']) - 1, int(data['N']),
         int(data['shift_units']), data['SHR_Threshold'])
     np.testing.assert_array_equal(peak_index, int(data['peak_index']) - 1)
     np.testing.assert_array_almost_equal(shr, data['SHR'])
     np.testing.assert_array_almost_equal(shshift, data['shshift'])
     np.testing.assert_array_almost_equal(index, data['index'] - 1)
Пример #13
0
 def test_second_peak_index(self):
     data = load_json(os.path.join('shrp', 'twomax_data'))
     x = data['x']
     for i in range(142, 146):
         # Zap the values in the second peak range to exercise the branch.
         x[i] = 0.0001*i
     mag, index = two_max(x,
                          int(data['lowerbound'])-1,
                          int(data['upperbound'])-1,
                          data['unitlen'])
     np.testing.assert_array_almost_equal(mag,
                                          np.append(data['mag'], 0.0145))
     np.testing.assert_array_equal(index, [int(data['index'])-1, 145])
Пример #14
0
 def test_med_smooth_greater_than_zero(self):
     data = load_json(os.path.join('shrp', 'shrp_data'))
     with self.assertRaises(NotImplementedError):
         f0_time, f0_value, shr, f0_candidates = shrp(
             data['Y'],
             int(data['Fs']),
             [int(x) for x in data['F0MinMax']],
             int(data['frame_length']),
             int(data['timestep']),
             data['SHR_Threshold'],
             data['ceiling'],
             med_smooth=5,
             CHECK_VOICING=False)
Пример #15
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)
Пример #16
0
 def test_resample_data_against_matlab(self):
     # XXX: This test fails because SciPy is using a different algorithm
     #      for resampling than Matlab.  When the SciPy and Matlab
     #      resampled data are plotted against each other, they look
     #      reasonably similar, so the fact that this test fails is
     #      probably okay.
     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)
         resample_fn = os.path.splitext(os.path.basename(fn))[0] + '-matlab-resample'
         data = load_json(os.path.join('soundfile', 'resample', resample_fn))
         self.assertEqual(len(s.wavdata_rs), len(data['y_rs']))
         self.assertAllClose(s.wavdata_rs, data['y_rs'], rtol=1e-01)
Пример #17
0
 def matlab_fn_as_matlab_input_data(self, filename):
     data = load_json(os.path.join('shrp', filename))
     peak_index, shr, shshift, index = compute_shr(
         data['log_spectrum'],
         data['min_bin'],
         data['startpos'].astype(int)-1,
         data['endpos'].astype(int)-1,
         int(data['lowerbound'])-1,
         int(data['upperbound'])-1,
         int(data['N']),
         int(data['shift_units']),
         data['SHR_Threshold'])
     np.testing.assert_array_equal(peak_index, int(data['peak_index'])-1)
     np.testing.assert_array_almost_equal(shr, data['SHR'])
     np.testing.assert_array_almost_equal(shshift, data['shshift'])
     np.testing.assert_array_almost_equal(index, data['index']-1)
Пример #18
0
 def test_with_matlab_data(self):
     data = load_json(os.path.join('shrp', 'shrp_data'))
     f0_time, f0_value, shr, f0_candidates = shrp(
         data['Y'],
         int(data['Fs']),
         [int(x) for x in data['F0MinMax']],
         int(data['frame_length']),
         int(data['timestep']),
         data['SHR_Threshold'],
         data['ceiling'],
         data['med_smooth'],
         data['CHECK_VOICING'])
     np.testing.assert_array_almost_equal(f0_time, data['f0_time'])
     np.testing.assert_array_almost_equal(f0_value, data['f0_value'])
     np.testing.assert_array_almost_equal(shr, data['SHR'])
     np.testing.assert_array_almost_equal(f0_candidates,
                                          data['f0_candidates'])
Пример #19
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)
Пример #20
0
 def test_with_matlab_data(self):
     data = load_json(os.path.join('shrp', 'toframes_data'))
     res = toframes(data['input'], data['curpos'].astype(int) - 1,
                    int(data['segmentlen']), 'hamm')
     np.testing.assert_array_almost_equal(res, data['frames'])
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')
Пример #22
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'])
Пример #23
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')