def get_stability_notes(h):
    from app_math import find_first_zero_idx
    from iir_models import correct_pfc
    """ По Найквесту """
    # Сперва запас по фазе 
    cut_position = find_first_zero_idx(to_dB(abs(h)))
    rest_phase = 180-abs(angle(h[cut_position])*180/pi)
        
    # По амплитуде
    phases_rad = correct_pfc(h)
    ampl_idx = find_first_zero_idx(phases_rad+pi)
    ampl = abs(h[ampl_idx])
    return rest_phase, ampl
def calc_analog_filter_curves(params, freq_axis, af_action):
    total_samples = len(freq_axis)
    w_complex = 1j*2*pi*freq_axis 
    h, phi, h_complex = calc_afc(w_complex, params, af_action), \
        calc_pfc(w_complex, params, af_action), calc_complex_h(w_complex, params, af_action)
    
    # Нужно подправить ФЧХ
    phi_copy = arange(total_samples)
    adder = 0
    
    for i in range(total_samples-1):
        if abs(phi[i]-phi[i+1]) > 180:
            adder += abs(phi[i]-phi[i+1])
        phi_copy[i+1] = phi[i+1]-adder
        
    return (h, phi_copy, freq_axis, to_dB(h), h_complex)
def plot_normalize_analog(h, phi, freq_axis, freq_sampling, cut_position):
    # Abs
    y_dB = to_dB(h)
    subplot(2, 1, 1)
    ylabel('20*log(K)')
    xlabel('F, Hz')
    grid()   
    axis = freq_axis/freq_sampling
    plot(axis, y_dB)
    plot(freq_axis[cut_position]/freq_sampling, y_dB[cut_position], 'o')
    xlim(0, freq_sampling/2/freq_sampling)
    
    # Angle
    subplot(2, 1, 2)
    grid()
    ylabel('Phase, deg')
    xlabel('F, Hz')
    plot(axis, phi)
    plot(freq_axis[cut_position]/freq_sampling, phi[cut_position], 'o')
    xlim(0, freq_sampling/2/freq_sampling)
def _plotter_angle_and_abs(h, w):
    h_dB = to_dB(abs(h))
    f_axis = w/2/pi
    
    # Plot
    subplot(211)
    plot(f_axis, h_dB)
    #ylim(min(h_dB), max(h_dB))
    #xlim(0, max(f_axis))
    ylabel('Magnitude (db)')
    xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
    title(r'Frequency response')
    
    # Plot
    subplot(212)
    h_Phase = unwrap(arctan2(imag(h),real(h)))*180/pi
    plot(f_axis,h_Phase)
    xlim(0, max(f_axis))
    ylabel('Phase (radians)')
    xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
    title(r'Phase response')
    subplots_adjust(hspace=0.5)