Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Error is wave file processing

I am executing the following program for SE32_LE wave file format:

import wave
import matplotlib.pyplot as plt
import numpy as np

f ="M1F132.wav", "rb") #rb = read only

# (nchannels, sampwidth, framerate, nframes, comptype, compname)
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
print("Channels =", nchannels)
print("Sample width =", sampwidth)
print("Frame rate =", framerate)
print("Frames =", nframes)
str_data = f.readframes(nframes)   #Reads and returns at most n frames of audio, as a bytes object.


#length = f.getnframes()
#for i in range(0,length):
#    waveData = f.readframes(1)
#    data = struct.unpack("<h", waveData)
#    print(int(data[0]))
wave_data = np.fromstring(str_data, dtype=np.short)  #A new 1-D array initialized from text data in a string
                                                     # str_data is string containing the data
                                                     #dtype= The data type of the array; default: float

print ("Audio data =", wave_data)       

wave_data.shape = -1, 2     #formats array in rows and columns
                            #-1. In this case, the value is taken from the length of the array and remaining dimensions.

wave_data = wave_data.T     #.T is self transpose the array i.e. rows to columns

time = np.arange(0, nframes) * (1.0 / framerate) #for creating time axis for plot, arrange for no. of frames, (1.0 / framerate) will create in seconds
                                                 #arange = Returns evenly spaced values within a given interval.
figure = plt.gcf() # get current figure
figure.set_size_inches(8*20, 6)

duration = nframes/float(framerate)
xticks = np.arange(0, duration, 2)
plt.plot(time, wave_data[0])
plt.title('Audio channel 1', loc='left')

plt.plot(time, wave_data[1], c="g")
plt.xlabel("time (seconds)")
plt.title('Audio channel 2', loc='left')
plt.savefig('Audio.png', dpi=100, bbox_inches='tight', pad_inches=0.1)
but i am getting the following error:

Channels = 2
Sample width = 4
Frame rate = 8000
Frames = 23493
Audio data = [ 0 0 0 ... 0 0 -6]
Traceback (most recent call last): File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\", line 45, in <module> plt.plot(time, wave_data[0]) File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\", line 2813, in plot is not None else {}), **kwargs) File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\", line 1810, in inner return func(ax, *args, **kwargs) File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\", line 1611, in plot for line in self._get_lines(*args, **kwargs): File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\", line 393, in _grab_next_args yield from self._plot_args(this, kwargs) File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\", line 370, in _plot_args x, y = self._xy_from_xy(x, y) File "C:\Users\a\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\axes\", line 231, in _xy_from_xy "have shapes {} and {}".format(x.shape, y.shape)) ValueError: x and y must have same first dimension, but have shapes (23493,) and (46986,)
PLease guide

Hello Buran,

I shall take care henceforth

Than you
buran wrote Jan-18-2019, 11:22 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Line 45 of your code:
plt.plot(time, wave_data[0])
ValueError: x and y must have same first dimension, but have shapes (23493,) and (46986,)

It's in the error traceback, there are among the best constructed error tracebacks in the programming world.

start with the last posted line number from your module, this is where the error initiated.
Code listed after your scripts last line shows what's executed after leaving your module, and code
Code listed before your scripts last line shows what's executed prior to the error.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  python one line file processing har 4 634 Dec-09-2019, 06:10 AM
Last Post: har
  pdf file processing: how to "Enable Editing" Pavel_47 4 515 Dec-04-2019, 10:00 AM
Last Post: Pavel_47
  Trouble processing file villumanati 1 365 Jul-30-2019, 04:17 AM
Last Post: Larz60+

Forum Jump:

Users browsing this thread: 1 Guest(s)