-
Notifications
You must be signed in to change notification settings - Fork 3
/
noiseremoval.py
118 lines (68 loc) · 2.28 KB
/
noiseremoval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import numpy as np
import scipy as sp
from scipy.io.wavfile import read
from scipy.io.wavfile import write # Imported libaries such as numpy, scipy(read, write), matplotlib.pyplot
from scipy import signal
import matplotlib.pyplot as plt
import sys
import pyaudio
import wave
# get_ipython().magic('matplotlib inline')
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
# RECORD_SECONDS = 3
# In[10]:
# Replace this with the location of your downloaded file.
(Frequency, array) = read(sys.argv[1]) # Reading the sound file.
# In[11]:
# len(array) # length of our array
# In[12]:
plt.plot(array)
plt.title('Original Signal Spectrum')
plt.xlabel('Frequency(Hz)')
plt.ylabel('Amplitude')
plt.show()
# In[13]:
FourierTransformation = sp.fft(array) # Calculating the fourier transformation of the signal
# In[14]:
scale = sp.linspace(0, Frequency, len(array))
# In[15]:
# plt.stem(scale[0:5000], np.abs(FourierTransformation[0:5000]), 'r') # The size of our diagram
# plt.title('Signal spectrum after FFT')
# plt.xlabel('Frequency(Hz)')
# plt.ylabel('Amplitude')
# plt.show()
# In[16]:
# GuassianNoise = np.random.rand(len(FourierTransformation)) # Adding guassian Noise to the signal.
# In[17]:
# NewSound = GuassianNoise + array
# In[18]:
# write("New-Sound-Added-With-Guassian-Noise.wav", Frequency, NewSound) # Saving it to the file.
# In[19]:
b,a = signal.butter(5, 1000/(Frequency/2), btype='highpass') # ButterWorth filter 4350
# In[20]:
filteredSignal = signal.lfilter(b,a,array)
plt.plot(filteredSignal) # plotting the signal.
plt.title('Highpass Filter')
plt.xlabel('Frequency(Hz)')
plt.ylabel('Amplitude')
plt.show()
# In[21]:
c,d = signal.butter(5, 1000/(Frequency/2), btype='lowpass') # ButterWorth low-filter
newfilteredSignal = signal.lfilter(c,d,filteredSignal) # Applying the filter to the signal
plt.plot(newfilteredSignal) # plotting the signal.
plt.title('Lowpass Filter')
plt.xlabel('Frequency(Hz)')
plt.ylabel('Amplitude')
plt.show()
newfilteredSignal =np.asarray(newfilteredSignal)
# In[22]:
# write("output.wav", Frequency, newfilteredSignal) # Saving it to the file
waveFile = wave.open("output.wav", 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(2)
waveFile.setframerate(RATE)
waveFile.writeframes(newfilteredSignal)
waveFile.close()