Commit 12d23d82 authored by LiniEisha's avatar LiniEisha

Reformatting noiseRemove.py

parent 3a879725
import numpy as np import numpy as nump
import scipy as sp import scipy as sip
from scipy.io.wavfile import read from scipy.io.wavfile import read
from scipy.io.wavfile import write from scipy.io.wavfile import write
from scipy import signal from scipy import signal
import matplotlib.pyplot as plt import matplotlib.pyplot as mplt
#get_ipython().magic('matplotlib inline') #get_ipython().magic('matplotlib inline')
(Frequency, array) = read('lectures/Lecture01.wav') (Frequency, array) = read('lectures/Lecture01.wav')
len(array) len(array)
plt.plot(array) mplt.plot(array)
plt.title('Original Signal Spectrum') mplt.title('Original Signal Spectrum')
plt.xlabel('Frequency(Hz)') mplt.xlabel('Frequency(Hz)')
plt.ylabel('Amplitude') mplt.ylabel('Amplitude')
FourierTransformation = sp.fft(array) fourierTransformation = sip.fft(array)
scale = sp.linspace(0, Frequency, len(array)) scale = sip.linspace(0, Frequency, len(array))
plt.stem(scale[0:5000], np.abs(FourierTransformation[0:5000]), 'r') mplt.stem(scale[0:5000], nump.abs(fourierTransformation[0:5000]), 'r')
plt.title('Signal spectrum after FFT') mplt.title('Signal spectrum after FFT')
plt.xlabel('Frequency(Hz)') mplt.xlabel('Frequency(Hz)')
plt.ylabel('Amplitude') mplt.ylabel('Amplitude')
GuassianNoise = np.random.rand(len(FourierTransformation)) guassianNoise = nump.random.rand(len(fourierTransformation))
NewSound = GuassianNoise + array NewSound = guassianNoise + array
write("New-Sound-Added-With-Guassian-Noise.wav", Frequency, NewSound) write("New-Sound-Added-With-Guassian-Noise.wav", Frequency, NewSound)
b,a = signal.butter(5, 1000/(Frequency/2), btype='highpass') u,v = signal.butter(5, 1000/(Frequency/2), btype='highpass')
filteredSignal = signal.lfilter(b,a,NewSound) filteredSignal = signal.lfilter(u,v,NewSound)
plt.plot(filteredSignal) # plotting the signal.
plt.title('Highpass Filter')
plt.xlabel('Frequency(Hz)')
plt.ylabel('Amplitude')
c,d = signal.butter(5, 380/(Frequency/2), btype='lowpass') # ButterWorth low-filter # plotting the signal.
newFilteredSignal = signal.lfilter(c,d,filteredSignal) # Applying the filter to the signal mplt.plot(filteredSignal)
plt.plot(newFilteredSignal) # plotting the signal. mplt.title('Highpass Filter')
plt.title('Lowpass Filter') mplt.xlabel('Frequency(Hz)')
plt.xlabel('Frequency(Hz)') mplt.ylabel('Amplitude')
plt.ylabel('Amplitude')
write("file.wav", Frequency, np.int16(newFilteredSignal/np.max(np.abs(newFilteredSignal)) * 32767)) # ButterWorth low-filter
\ No newline at end of file x,y = signal.butter(5, 380/(Frequency/2), btype='lowpass')
# Applying the filter to the signal
newFilteredSignal = signal.lfilter(x,y,filteredSignal)
# plotting the signal.
mplt.plot(newFilteredSignal)
mplt.title('Lowpass Filter')
mplt.xlabel('Frequency(Hz)')
mplt.ylabel('Amplitude')
write("removed.wav", Frequency, nump.int16(newFilteredSignal/nump.max(nump.abs(newFilteredSignal)) * 32767))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment