0
$\begingroup$

I have written the following code which is supposed to put echo over an available sound file. Unfortunately the output is a very noisy result which I don't exactly understand. Can anybody help me with regard to this? Is there any skipped step?

#convolving a room impulse response function with a sound sample both of stereo type
from scipy.io import wavfile
inp = wavfile.read(sound_path + sound_file_name)
IR = wavfile.read(IR_path + IR_file_name)
if inp[0] != IR[0]:
    print "Size mismatch"
    sys.exit(-1)
else:
    rate = inp[0]
print sound_file_name
out_0 = fftconvolve(inp[1][:, 1], IR[1][:, 1])
out_1 = fftconvolve(inp[1][:, 1], IR[1][:, 1])
in_counter += 1
out = np.vstack((out_0, out_1)).T
wavfile.write(sound_path + sound_file_name + '_echoed.wav', rate, out)
$\endgroup$
  • 1
    $\begingroup$ "a very noisy result" Plot part of it and look at the graph and see what the problem is. Is it clipped? Wrapping around? Skipping samples? $\endgroup$ – endolith Sep 25 '14 at 13:56
  • 1
    $\begingroup$ actually it was clipped to full amplitude and I believe the reason of conversion between float and integer which I was not doing. $\endgroup$ – Cupitor Sep 25 '14 at 17:48
2
$\begingroup$
  1. The way you read the files is a little odd. The method used here is clearer with respect to the sampling rate and the signal.
  2. You will note that the above example calls pcm2float after reading. This may be the cause of your problem. The wave reader returns an array of ints. Processing those (quite large) values may well cause problems. Try converting your wave data to floats before doing the convolution.
$\endgroup$
  • $\begingroup$ Thank you. I actually forgot to update here although I am still not completely satisified with final result. $\endgroup$ – Cupitor Sep 25 '14 at 17:43
0
$\begingroup$

I have finally made some manipulations that was mainly based on what @JRE suggested and I have independently found out too. I have posted this answer on SO earlier. Manipulations(like convolution) on signal need to be done after converting the wave file to float type and to be finally reconverted to original 16 bit Wav. There also need to be an additional step of adding the original signal with the convolved signal which I added to the below code.

from utility import pcm2float,float2pcm
input_rate,input_sig=wavfile.read(sound_path+sound_file_name)
input_sig=pcm2float(input_sig,'float32')
IR_rate,IR_sig=wavfile.read(IR_path+IR_file_name)
IR_sig=pcm2float(IR_sig,'float32')

if input_rate!=IR_rate:
    print "Size mismatch"
    sys.exit(-1)
else:
    rate=input_rate
print sound_file_name
    out_0=fftconvolve(input_sig[:,0],IR_sig[:,0])
out_0=out_0/np.max(np.abs(out_0))
out_1=fftconvolve(input_sig[:,1],IR_sig[:,1])
out_1=out_1/np.max(np.abs(out_1))
out=np.vstack((out_0,out_1)).T
out=float2pcm(out,'int16')
out[:input_sig[:,0].shape[0]]=out[:input_sig[:,0].shape[0]]+input_sig
wavfile.write(sound_path+'echoed outputs/'+sound_file_name+'_'+IR_file_name+'_echoed.wav',rate,out)

I am still not completely happy with the results. The results using openair website is far better than mine and I still don't know the reason for that: http://www.openairlib.net/auralizationdb/content/elveden-hall-suffolk-england

$\endgroup$
  • $\begingroup$ You don't "need" to convert to float, but it's certainly easier that way. $\endgroup$ – endolith Sep 25 '14 at 18:01
  • $\begingroup$ Without conversion I always had the problem of a huge noise over the output. Then I would be happy if you could explain what might be the source of that noisy output (basically not noise but overflown amplitude)... $\endgroup$ – Cupitor Sep 25 '14 at 18:31
  • 1
    $\begingroup$ Well keeping it in integer form is like using a fixed-point DSP rather than a floating-point DSP. You can still do processing that way, by scaling things appropriately at each step, but there's no reason to do it on a computer. Modern computers are faster at floating point math than integer/fixed-point math, so that's the easier and faster way. Fixed-point is used on microcontrollers or specialized processors. $\endgroup$ – endolith Sep 25 '14 at 19:31
  • $\begingroup$ For the downvoter, I would happily edit my code to make it more suited for the question that I asked(!!!!), if you specify your reason for downvoting $\endgroup$ – Cupitor Sep 25 '14 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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