I am seeing an issue in the magnitude response between reading a wav file with soundfile
and wavefile
. Here are the different plots:
Can you tell me what I would need to adjust in the wavefile.read
to get the same magnitude as the soundfile.read
?
Here is the code that I use:
import os
import matplotlib.pyplot as plt
from matplotlib import transforms
import numpy as np
import soundfile as sf
import scipy.io.wavfile as wavfile
from matplotlib.gridspec import GridSpec
input_file1 = (r'G:/file.wav')
plt.subplot(211)
a, b = sf.read(input_file1);
pxx, fs = plt.psd(a, 512, b)
plt.semilogx(fs, 10*np.log10(pxx))
plt.title('Sound File Read')
plt.grid(which='major', axis='both', color='g', linestyle='-', alpha=0.4)
plt.grid(which='minor', axis='x', color='g', linestyle='-', alpha=0.1)
plt.subplot(212)
sample_rate, signal1 = wavfile.read(input_file1)
Pxx, freq = plt.psd(signal1, 512, sample_rate)
plt.semilogx(freq, 10*np.log10(Pxx))
plt.grid(which='major', axis='both', color='g', linestyle='-', alpha=0.4)
plt.grid(which='minor', axis='x', color='g', linestyle='-', alpha=0.1)
plt.title('Wavfile File Read')
plt.ylabel('PSD')
plt.xlabel('Frequency (Hz)')
# set the spacing between subplots
plt.tight_layout()
plt.show()
Here is a link to an example .wav file.
Thanks!
By the two values you reported, it really does seem like soundfile.read
gave you a float64
array between -1 and 1 while wavfile.io.read
gave you a int32
array between -2147483648 and 2147483647 (-4850432/2147483648 = -0.00225866). You can make a normalized float_
array from either int_
or float_
array with the following:
def normalize(signal1):
try:
intinfo = np.iinfo(signal1.dtype)
return signal1 / max( intinfo.max, -intinfo.min )
except ValueError: # array is not integer dtype
return signal1 / max( signal1.max(), -signal1.min() )