Skip to content

Commit 029ddcb

Browse files
authored
Noise Reduction Script (avinashkranjan#1106)
1 parent c14aacc commit 029ddcb

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed
Binary file not shown.
125 KB
Binary file not shown.
Loading
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Spectral Subtraction: Method used for noise reduction
2+
import scipy.io.wavfile as wav
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
file = input("Enter the file path: ")
7+
sr, data = wav.read(file)
8+
fl = 400 #frame_length
9+
frames = [] #empty list
10+
for i in range(0,int(len(data)/(int(fl/2))-1)):
11+
arr = data[int(i*int(fl/2)):int(i*int(fl/2)+fl)]
12+
frames.append(arr) #appending each array data into the frames list
13+
frames = np.array(frames) #converting the frames list into an array
14+
ham_window = np.hamming(fl) #using np.hamming
15+
windowed_frames = frames*ham_window #multiplying frames array with ham_window
16+
dft = [] #empty list containing fft of windowed_frames
17+
for i in windowed_frames:
18+
dft.append(np.fft.fft(i)) #now taking the first fourier transform of each window
19+
dft = np.array(dft) #converting dft into array
20+
21+
dft_mag_spec = np.abs(dft) #converting dft into absolute values
22+
dft_phase_spec = np.angle(dft) #finding dft angle
23+
noise_estimate = np.mean(dft_mag_spec,axis=0) #mean
24+
noise_estimate_mag = np.abs(noise_estimate) #absolute value
25+
26+
estimate_mag = (dft_mag_spec-2*noise_estimate_mag) #subtraction method
27+
estimate_mag[estimate_mag<0]=0
28+
estimate = estimate_mag*np.exp(1j*dft_phase_spec) #calculating the final estimate
29+
ift = [] #taking ift as input list containing inverse fourier transform of estimate
30+
for i in estimate:
31+
ift.append(np.fft.ifft(i)) #appending in ift list
32+
33+
clean_data = []
34+
clean_data.extend(ift[0][:int(fl/2)]) #extending clean_data containg ift list
35+
for i in range(len(ift)-1):
36+
clean_data.extend(ift[i][int(fl/2):]+ift[i+1][:int(fl/2)])
37+
clean_data.extend(ift[-1][int(fl/2):]) #extending clean_data containing ift list
38+
clean_data = np.array(clean_data) #converting it into array
39+
40+
#finally plotting the graph showing the diffrence in the noise
41+
fig = plt.figure(figsize=(8,5))
42+
ax = plt.subplot(1,1,1)
43+
ax.plot(np.linspace(0,64000,64000),data,label='Original',color="orange")
44+
ax.plot(np.linspace(0,64000,64000),clean_data,label='Filtered',color="purple")
45+
ax.legend(fontsize=12)
46+
ax.set_title('Spectral Subtraction Method', fontsize=15)
47+
filename = os.path.basename(file)
48+
cleaned_file = "(Filtered_Audio)"+filename #final filtered audio
49+
wav.write(cleaned_file,rate=sr, data = clean_data.astype(np.int16))
50+
plt.savefig(filename+"(Spectral Subtraction graph).jpg") #saved file name as audio.wav(Spectral Subtraction graph).jpg

Noise Reduction Script/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Noise Reduction Script
2+
Implementing a feature that helps to filter an audio file by reducing the background noise similar to "Audacity".
3+
4+
## Libraries used
5+
Firstly import the following python libraries
6+
* NumPy
7+
* scipy.io.wavfile
8+
* Matplotlib
9+
* Os
10+
Save the audio files and your code in the same folder
11+
Run the python code
12+
13+
## Detailed explanation of method used for "Noise Reduction Script"
14+
* Imported the required libraries (NumPy, scipy.io.wavfile, and Matplotlib)
15+
* Read the input audio file using scipy.io.wavfile library
16+
* Converting the audio file into an array containg all the information of the given audio file and intiallizing the frame value.
17+
* Calculating the first fourier transform of each window of the noisy audio file
18+
* Subtracting the noise spectral mean from input spectral, and istft (Inverse Short-Time Fourier Transform)
19+
* Finally getting an audio file with reduction in the background noise at a much higher extent
20+
21+
## Output
22+
<img src="Graph/test_noise.wav(Spectral Subtraction graph).jpg" height="500px">
23+
24+
## Author(s)
25+
[Akriti](https://github.com/A-kriti)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
matplotlib==3.2.2
2+
numpy==1.18.5
3+
scipy==1.5.0

0 commit comments

Comments
 (0)