Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Noise(hiss) from Raw WAV audio file using Python

I want to remove noise(hisss) from a wave audio file. The full wave audio graph is here :

I'm using below code. It might be a stupid attempt but in matlab i've noticed that the noise part's amplitude varies between 0-3000. So i tried to make all of them to zero and save new frames to a new wav file. Somehow it did not work!

import wave
import sys
ip = wave.open(sys.argv[1], 'r')

op = wave.open(sys.argv[2], 'w')
op.setparams(ip.getparams())

for i in range(ip.getnframes()):
    iframes = ip.readframes(1)
    amp = int(iframes.encode('hex'),16)
    if amp > 32767:
        amp = 65535 - int(iframes.encode('hex'),16)#-ve
        print amp
    else:
        amp = int(iframes.encode('hex'),16)#+ve
        print amp
    if amp < 2000:
        #make it zero
        final_frame = '\x00\x00'
    else:
        #Keep the frame 
        final_frame = iframe
    op.writeframes(final_frame)
op.close()
ip.close()

After running above script it became this:

The noise part (<= 2500 ) is still present..So Please suggest how i can remove those unnecessary parts !

Best Regards,

like image 800
Dev.K. Avatar asked Feb 21 '26 17:02

Dev.K.


1 Answers

Your first problem is that you're decoding the values as big-endian, while they're actually little-endian. You can fix that easily with the struct module. I've also added the abs function since amplitude is usually the distance from zero, which is always positive.

amplitude = abs(struct.unpack('<h', iframe))

This will make your code do what you expect. Unfortunately it doesn't solve the larger problem, which is that this is the wrong approach entirely. It doesn't look at the waveform itself, it only looks at a single sample at a time. A simple sine wave that runs full scale will have many samples that are below your threshold, and you'll introduce significant distortion by setting them to zero.

like image 147
Mark Ransom Avatar answered Feb 23 '26 06:02

Mark Ransom