def reverse_filter (d, t = "ideal", N = None, v = None): d_f = fourier (d) g = empty ((0,4)) if t == "ideal": a = 0.00001 for r, i in zip(d_f[:,re], d_f[:,im]): #m = 1 / (r+a if r != 0 else 1500) #n = 1 / (i+a if i != 0 else 1500) m = r / (r*r+i*i) n = -i / (r*r+i*i) g = concatenate ((g, [[m, n, sqrt (m*m + n*n), m + n]])) elif t == "regular": alpha = 0.4 # находим обратный фильтр for r, i in zip(d_f[:,re], d_f[:,im]): m = r / ( r*r + i*i + alpha*alpha ) n = -i / ( r*r + i*i + alpha*alpha ) g = concatenate ((g, [[m, n, sqrt (m*m + n*n), m + n]])) elif t == "viner": if N == None or v == None: return None N = fourier (N) v = fourier (v) # находим обратный фильтр for r, i, n_r, n_i, v_r, v_i in zip(d_f[:,re], d_f[:,im], N[:,re], N[:,im], v[:,re], v[:,im]): b = 1/(v_r*v_r + v_i*v_i) n_n = n_r*n_r + n_i*n_i m = r / ( r*r + i*i + b*n_n ) n = -i / ( r*r + i*i + b*n_n ) g = concatenate ((g, [[m, n, sqrt (m*m + n*n), m + n]])) # print g return g
def init (): #x = array ([2*(gauss(0,1)-0.5) for i in range (N)]) u = zeros(N); s = zeros(N); v = array([]) beats = { "200": 120, "400": 125, "600": 119, "800": 130, "240": -80, "440": -75, "640": -70, "840": -85 } # генерируем сердцебиение for b in beats: u[int(b)] = beats[b] # генерируем синусоиду и дополняем ее нулями d = zeros (N) # d[:L] = [sin (2 * pi * f * t * 0.008) * exp (-alpha * t * 0.008) for t in range (L)] dt = 1; m = 64; n = 10 d = [ sin (2 * pi * 1 * t * 0.008 + 10*pi) # + sin (2 * pi * 17 * t * 0.008) # + sin (2 * pi * 150 * t * 0.008) for t in range (N)] a = array ([lfsr().next() for i in range (N)])*5 a_f = fourier (a) bpf_f = fourier (bpf (0.1, 0.2, L = N, m = m)) a_short_f = conv_freq (a_f, bpf_f) a_short = fourier_inv (a_short_f[:,comp]) #d_n = conv (d, a_short) # html.add_figure (d, u"Синусоида") # html.add_figure (add_spikes (-1, 1, 30, d), u"Синусоида с импульсами") # html.add_figure (add_trend (linear_trend (0.001, 0.01, 2, len(d)), d), u"Синусоида с линейным трендом") # html.add_figure (add_trend (exp_trend (0.001, 0.01, len(d)), d), u"Синусоида с экспоненциальным трендом") html.add_break () d = zeros (N) d[:L] = [sin (2 * pi * f * t * 0.008) * exp (-alpha * t * 0.008) for t in range (L)] v = conv (u,d) v_n = v + a html.add_figure (u, u"Кардиограмма") # html.add_figure (d, u"Синусоида") html.add_figure (v, u"Свертка") html.add_figure (v_n, u"Свертка с шумом") v_f = fourier (v) v_n_f = fourier (v_n) d_f = fourier (d) # g_i = reverse_filter (d, "ideal", a, v) g_r = reverse_filter (d, "regular", a, v) # g_v = reverse_filter (d+a, "viner", a, v) # html.add_figure (g_i[:,comp], u"Обратный идеальный фильтр\n(комплексный спектр)") html.add_figure (g_r[:,comp], u"Обратный регулярный фильтр\n(комплексный спектр)") # html.add_figure (g_v[:,comp], u"Обратный винеровский фильтр\n(комплексный спектр)") # # g_f = conv_freq (g_i, v_f) # d_est = fourier_inv (g_f[:,comp]) # html.add_figure (d_est, u"Деконволюция идеальным фильтром") g_f = conv_freq (g_r, v_f) d_est = fourier_inv (g_f[:,comp]) html.add_figure (d_est, u"Деконволюция регулярным фильтром")