간단한 FIR 필터를 만듦. 예제로 moving average 필터를 사용함.
예전 예제에서 만든 wav file(".\WaveTest.wav")을 이용함
import wave
import struct
import numpy as np
import matplotlib.pyplot as plt
N = 20 #FIR filter length
fname = ".\WaveTest.wav"
wav_file = wave.open(fname, "r")
framerate = float(wav_file.getframerate())
nframes = wav_file.getnframes()
time = np.arange(0, nframes/framerate, 1/framerate)
x = []
for i in range(nframes):
waveData = wav_file.readframes(1)
x.append(struct.unpack("
wav_file.close()
b = [1.0/N]*N #FIR filter coefficient, moving averaging filter
#Filtering mechanism
#y[n] = b[0]*x[n] + b[1]*x[n-1] + b[2]*x[n-2] + ... + b[n-1]*x[1] + b[n]*x[0]
''' y = [0.0]*nframes for index in range(nframes): for k in range(N): if index-k >= 0: y[index] = y[index] + b[k]*x[index-k] ''' y = np.convolve(b, x, "full")
plt.plot(time, x, time, y)
plt.xlabel("times[s]")
plt.ylabel("data")
plt.xlim(0, 0.005)
plt.show()
|
필터링 된 이미지
참고 :
위키백과 : http://en.wikipedia.org/wiki/Finite_impulse_response
scipy가 지원하는 fir filter : http://mpastell.com/2010/01/18/fir-with-scipy/
댓글 없음:
댓글 쓰기