问题
We are currently integrating three codes 1. Mic recorder 2. Integrator (Low-Pass filtering) 3. Applying the filter
We encountered this error:
fltrd()
[[ 0 0]
[ -1 0]
[ 0 0]
...,
[-65 -60]
[-31 -52]
[-45 -53]]
Traceback (most recent call last):
File "<ipython-input-2-72cbac6fd2ac>", line 1, in <module>
fltrd()
File "C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT/etc/project.py", line 57, in fltrd
a2 = integ(x)
File "C:/Users/vsecadesang/Desktop/5th year/2nd sem/SIGNLAB/PROJECT/etc/project.py", line 49, in integ
y[0] = x[0]
ValueError: setting an array element with a sequence.
The microphone record part works fine. The only problem is that when we put the wav file of the mic record into the integrator and call the filter that is defined it produces a ValueError that is mentioned above. Our projected output is that the mic recording that we're going to put in the integrator (low-pass) filter will produce the same recording but with less noise. Kind of like an active noise reductor. We don't know what's wrong, please help.
Here are our codes below:
import pyaudio
import wave
import matplotlib.pyplot as plt
import numpy as np
import scipy.io.wavfile
import scipy.signal as sp
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "file1.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print ("recording...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print ("finished recording")
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
x = scipy.io.wavfile.read('file1.wav')
n = x[1]
def integ(x):
y = np.zeros(len(x))
for i in range(0, len(x)):
if i == 0:
y[0] = x[0]
else:
w = y[i-1]
y[i] = w + x[i]
return y
def fltrd():
n,x = scipy.io.wavfile.read('file1.wav')
print(x)
a2 = integ(x)
a3 = np.asarray(a2, dtype = np.int16)
scipy.io.wavfile.write('file2.wav',n,a3)
回答1:
Your file has two channels, so x
has shape (m, 2)
for some integer m
. In your function integ(x)
, you have y = np.zeros(len(x))
. That creates an array with shape (m,)
. Then the line y[0] = x[0]
attempts to copy the two values in x[0]
into the single value y[0]
.
To fix this, create y
to have the same shape as x
:
y = np.zeros(x.shape)
Note that np.zeros()
creates a floating point array by default. If you want y
to have the same data type as x
, you could do
y = np.zeros_like(x)
You can also set the data type explicitly, so if you want y
to be 32 bit floating point:
y = np.zeros(x.shape, dtype=np.float32)
Finally, note that your integ()
function is the cumulative sum of x
along its first axis, so it can be simplified to y = x.cumsum(axis=0)
. That is, you could completely remove the function integ
, and replace
a2 = integ(x)
with
a2 = x.cumsum(axis=0)
One potential problem with that is you could get overflow in the 16 bit sums. If that happens and you don't know how to deal with it, well, you can always create a new question about it on stackoverflow. :)
来源:https://stackoverflow.com/questions/49607395/value-error-occurs-when-filtering-wav-file