0
$\begingroup$

I'm working on machine learning model and tried to downsample WAV files with Python script, but it didn't work, so I made the simple version - script "resamples" WAV file from 44100Hz to the same 44100Hz sample rate. Original and output files seems identical while listening to, they are both 16-bit depth, 44100Hz sample rate, output has 1 channel, but I did it for testing purposes and I think shouldn't matter.

However, plot shows minimal differences original vs "resampled" signal:

There is my whole code:

def calculate_differences_between_arrays(arr1, arr2):
    differences = []
    for val1, val2 in zip(arr1, arr2):
        diff = val1 - val2
        differences.append(diff)
    return differences

def show_plot_for_list(list, header):
    plt.plot(list)
    plt.title(header)
    plt.show()

if __name__ == "__main__":

    # audio read
    origin_sample_rate, origin_audio = wavfile.read('org.wav')
    wavfile.write('out_org.wav', origin_sample_rate, origin_audio[:,0])

    origin_num_samples, origin_num_channels = origin_audio.shape

    # resampling
    target_audio_scipy = scipy.signal.resample(origin_audio[:,0], origin_num_samples).astype(int)
    target_audio_scipy = np.array(target_audio_scipy, np.int16)

    # show differences
    diffArray = calculate_differences_between_arrays(origin_audio[:,0], target_audio_scipy)
    show_plot_for_list(diffArray, 'Differences')

    # write modified audio to WAV file
    wavfile.write('out.wav', origin_sample_rate, target_audio_scipy)

Is my script wrong, or it is normal behavior and it won't be problem while downsampling for example to 16000Hz?

$\endgroup$
1
  • 1
    $\begingroup$ Have you looked what all resample() function internally does? $\endgroup$
    – Juha P
    Commented Jul 1, 2023 at 5:15

1 Answer 1

0
$\begingroup$

There is no problem with the resample command of sampling to the same sampling rate, and the 1 count error is simply due to rounding with the 16 bit precision used. Zooming in on an example audio track I confirmed the OP's result of having the input and output matching within 1 sample due to rounding and not a time offset.

audio

Extreme zoom-in:

zoom-in

Resampling to 16 KHz will result in larger deviations as expected by reducing all spectrum above ~ 7 KHz and indicated in the comparative plot below. The audio should be completely recognizable however:

After resampling to 16 KHz

after resampling

My code with resampling done:

# audio read
origin_sample_rate, origin_audio = wavfile.read('./data/Inspiring-Powerful-Cinematic_AdobeStock_462873176.wav')
wavfile.write('./out_org.wav', origin_sample_rate, origin_audio[:,0])

origin_num_samples, origin_num_channels = origin_audio.shape

new_samps = int(origin_num_samples * 16000/44100)

# resampling
target_audio_scipy = sig.resample(origin_audio[:,0], new_samps).astype(int)
target_audio_scipy = np.array(target_audio_scipy, np.int16)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.