Created
September 19, 2013 20:44
-
-
Save abinashpanda/6629575 to your computer and use it in GitHub Desktop.
Simple Filter Design for Noise Removal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import scipy.fftpack as fftpack | |
import scipy.io.wavfile as wav | |
import numpy as np | |
import matplotlib.pylab as plt | |
# Reading wav file | |
AUDIO_FILE = '/home/abinash/hin_0044.wav' | |
input_file = open(AUDIO_FILE) | |
s_rate, wav_file = wav.read(input_file) | |
m, = wav_file.shape | |
# Plotting Time Domain Representation of Signal | |
figure_1 = plt.figure('Original Signal') | |
plt.subplot(2, 1, 1) | |
plt.title('Time Domain Signal') | |
plt.plot(np.arange(m)/float(s_rate), wav_file) | |
plt.xlabel('Time') | |
plt.ylabel('Amplitude') | |
# FFT of Signal | |
fft_signal = fftpack.fft(wav_file) | |
freq = fftpack.fftfreq(m) | |
# Plotting Frequency Domain Representation of Signal | |
plt.subplot(2, 1, 2) | |
plt.title('Frequency Domain') | |
plt.plot(freq*s_rate, fft_signal) | |
plt.xlabel('Frequency') | |
plt.ylabel('Spectrum') | |
# Noisy Signal | |
noisy_signal = wav_file + 100*np.random.randn(m) | |
figure_2 = plt.figure('Noisy Signal') | |
plt.subplot(2, 1, 1) | |
plt.title('Time Domain Noisy Signal') | |
plt.plot(np.arange(m)/float(s_rate), noisy_signal) | |
plt.xlabel('Time') | |
plt.ylabel('Amplitude') | |
fft_noisy_signal = fftpack.fft(noisy_signal) | |
plt.subplot(2, 1, 2) | |
plt.title('Frequency Domain Representation Noisy Signal') | |
plt.plot(freq*s_rate, fft_noisy_signal) | |
plt.xlabel('Frequency') | |
plt.ylabel('Spectrum') | |
filter_signal = [fft_noisy_signal[val] if freq[val]*s_rate < 3000 and | |
freq[val]*s_rate > -3000 else 0 for val in range(m)] | |
filter_signal_time = fftpack.ifft(filter_signal) | |
figure_fitler = plt.figure('Filtered Signal') | |
plt.plot(np.arange(m)/float(s_rate), filter_signal_time) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment