user4488000
user4488000

Reputation:

Data returned by scipy.io.wavfile.read

I'm trying to get the data from a wav file in Python and plot it. When I use scipy.io.wavfile.read(), I get back an array that looks like this:

[[ -1.49836736e-02  -1.27559584e-02]
 [ -1.84625713e-02  -1.63264061e-02]
 [ -2.17888858e-02  -1.95001373e-02]
 ..., 
 [  6.10332937e-05   6.10332937e-05]
 [ -3.05166468e-05   0.00000000e+00]
 [  3.05166468e-05  -6.10332937e-05]]

Why is it a bunch of arrays with length 2 as opposed to one long array with the value at each sample? What does this data being returned represent? Thanks in advance.

convert_16_bit = float(2**15)
sr, samples = scipy.io.wavfile.read('singingonenote.wav')
x = np.linspace(0, 2000, 0.01)
samples = samples / (convert_16_bit + 1.0)
y = samples
print samples
plt.plot(x, y)
plt.show()

Upvotes: 8

Views: 8984

Answers (1)

theister
theister

Reputation: 1023

The file you are reading seems to be a stereo file. These contain two-dimensional data - one track for the left and one track for the right speaker.

The general concept is explained here: https://en.wikipedia.org/wiki/Stereophonic_sound

If you want to select just the left audio channel from your two-dimensional data sequence, you can select it like

y = samples[:,0]

To select the right channel, replace the 0 with a 1.

As an alternative, make sure that the program you use to generate the file saves mono wave files in the first place. Depending on what you are trying to do, this might be the actual bug.

Upvotes: 10

Related Questions