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"
    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)
  • 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
  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.
| improve this answer | |
  • $\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

It is a little late, but i'm also working on convolution reverb at the moment. If it is still of interest, you can use my code. Simply call the function convolution_reverb and pass the paths to the two audio files (audio and impulse response, both need to be .wav files), as well as the name for the result file to be created.

import numpy as np
from wave import open
import soundfile

class Wave:
    def __init__(self, data, frame_rate):
        self.data = normalize(data)
        self.frame_rate = frame_rate

    def make_spectrum(self):
        amplitudes = np.fft.rfft(self.data)
        frequencies = np.fft.rfftfreq(len(self.data), 1 / self.frame_rate)

        return Spectrum(amplitudes, frequencies, self.frame_rate)

    def zero_padding(self, n):
        zeros = np.zeros(n)
        zeros[:len(self.data)] = self.data

        self.data = zeros

    def write(self, file):
        reader = open(file, 'w')


        frames = self.quantize().tostring()


    def quantize(self):
        if max(self.data) > 1 or min(self.data) < -1:
            self.data = normalize(self.data)

        return (self.data * 32767).astype(np.int16)

class Spectrum:
    def __init__(self, amplitudes, frequencies, frame_rate):
        self.amplitudes = np.asanyarray(amplitudes)
        self.frequencies = np.asanyarray(frequencies)
        self.frame_rate = frame_rate

    def __mul__(self, other):
        return Spectrum(self.amplitudes * other.amplitudes, self.frequencies, self.frame_rate)

    def make_wave(self):
        return Wave(np.fft.irfft(self.amplitudes), self.frame_rate)

def convert_wav(file):
    data, samprate = soundfile.read(file)
    soundfile.write(file, data, samprate, subtype='PCM_16')

def read_wave(file):
    reader = open(file)

    _, sampwidth, framerate, nframes, _, _ = reader.getparams()
    frames = reader.readframes(nframes)


    dtypes = {1: np.int8, 2: np.int16, 4: np.int32}

    if sampwidth not in dtypes:
        raise ValueError('unsupported sample width')

    data = np.frombuffer(frames, dtype=dtypes[sampwidth])

    num_channels = reader.getnchannels()
    if num_channels == 2:
        data = data[::2]

    return Wave(data, framerate)

def normalize(data):
    high, low = abs(max(data)), abs(min(data))
    return data / max(high, low)

def convolution_reverb(audio_file, ir_file, output_file):

    audio = read_wave(audio_file)
    ir = read_wave(ir_file)

    if len(audio.data) > len(ir.data):


    ir_spectrum = ir.make_spectrum()
    audio_spectrum = audio.make_spectrum()

    convolution = audio_spectrum * ir_spectrum
    wave = convolution.make_wave()

convolution_reverb('audio.wav', 'ir.wav', 'result.wav')
| improve this answer | |
  • $\begingroup$ Thanks pal! I have moved on a long time ago! :) Also welcome to dsp! $\endgroup$ – Cupitor May 31 at 6:52

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

if input_rate!=IR_rate:
    print "Size mismatch"
print sound_file_name
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

| improve this answer | |
  • $\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.