def desired_highpass():

    ripple_low = 0.01
    ripple_high = 0.05
    # Normalized frequencies vector
    F = [0, 0.5, 0.6, 1]
    # Desired amplitudes of each point listed in 'F'
    Amp = [0, 1]
    # Weights vector
    W = [1/ripple_low, 1/ripple_high]

    M = get_M(ripple_low, ripple_high, 0.5, 0.6)

    h = remez(M+1, F, Amp, W, Hz=fs)
    A_w = get_amplitude_func(h, 1)

    plt.figure()
    plt.plot(h)
    plt.title("h[n] is GLP filter of type I.")
    plt.grid()
    plt.legend()

    w = np.linspace(-2 * math.pi, 2 * math.pi, 4096)
    A = []
    for i in w:
        A.append(A_w(i))
    w /= math.pi

    plt.figure()
    plt.plot(w, A)
    plot_plus("A(w)")
def desired_bandpass():

    ripple_low = 0.02
    ripple_medium = 0.05
    ripple_high = 0.01
    # Normalized frequencies vector
    F = [0, 0.3, 0.4, 0.7, 0.8, 1]
    # Desired amplitudes of each point listed in 'F'
    Amp = [0, 0, 1, 1, 0, 0]
    # Weights vector
    W = [1 / ripple_low, 1 / ripple_medium, 1 / ripple_high]

    h = firls(M + 1, F, Amp, W)
    A_w = get_amplitude_func(h, 1)

    plt.figure()
    plt.plot(h)
    plt.title("h[n] is GLP filter of type I.")
    plt.grid()
    plt.legend()

    w = np.linspace(-2 * math.pi, 2 * math.pi, 4096)
    A = []
    for i in w:
        A.append(A_w(i))
    w /= math.pi

    plt.figure()
    plt.plot(w, A)
    plot_plus("A(w)")
def ex_7():
    (M, window) = get_kaiser_window(ripple, delta_w)
    # We know that h is simmetric.
    # Therefore, it depends on M whether its a type I or II filter.
    # k indicates the frequency range to take into account.
    if (M % 2 == 0):
        filter_type = 1
        k = 1
    else:
        filter_type = 2
        k = 2
    h_ideal = ideal_lowpass_truncated(w_c, M)
    h = np.multiply(h_ideal, window)
    A_w = get_amplitude_func(h, filter_type)

    plt.figure()
    plt.plot(h_ideal)
    plt.title("Impulse response corresponds to a type " + str(filter_type) +
              " filter.")
    plt.grid()

    plt.figure()
    w = np.linspace(-k * math.pi, k * math.pi, 4096)
    A = []
    for i in w:
        A.append(A_w(i))
    w /= math.pi
    plt.plot(w, A)
    plot_plus(title="Freq. response")
def ex_1():
    # Cutoff frequency
    w_c = math.pi / 2
    # Frequency domain
    w = np.linspace(-1 * math.pi, math.pi, 4096)
    # Phase delay variations
    for M in [10, 100, 1000]:
        # Index of truncated sinc function
        n_trunc = np.linspace(0, M, endpoint=True, num=M+1)
        # Impulse response of truncated sinc function
        h_trunc = (w_c / math.pi) * np.sinc((w_c / math.pi) * (n_trunc - M/2))
        # Amplitude function of truncated sinc function
        A_w_trunc = get_amplitude_func(h_trunc, 1)

        y_trunc = []
        for i in w:
            y_trunc.append(A_w_trunc(i))

        plt.plot(w, y_trunc, label="M = " + str(M))

    y_ideal = [0] * 4096
    for i in xrange(1024, 3072):
        y_ideal[i] = 1

    plt.plot(w, y_ideal, label="Ideal frequency response")
    plt.title("Amplitude function of truncated sinc")
    plt.legend()
    plt.grid()
    plt.show()
def ex_17():
    h1 = diferentiator(M1)
    h2 = diferentiator(M2)
    A_w1 = get_amplitude_func(h1, 4)
    A_w2 = get_amplitude_func(h2, 3)

    plt.figure()
    plt.plot(h1)
    plt.title("h1[n] corresponds to type IV filter.")
    plt.grid()
    plt.legend()
    plt.figure()
    plt.plot(h2)
    plt.title("h2[n] corresponds to type III filter.")
    plt.grid()
    plt.legend()

    w = np.linspace(-2 * math.pi, 2 * math.pi, 4096)
    A1 = []
    A2 = []
    for i in w:
        A1.append(A_w1(i))
        A2.append(A_w2(i))
    w /= math.pi

    plt.figure()
    plt.plot(w, A1)
    plt.title("A1(w)")
    plt.grid()
    plt.legend()
    plt.figure()
    plt.plot(w, A2)
    plt.title("A2(w)")
    plt.grid()
    plt.legend()
    plt.show()
def ex_4():
    M = 80
    h = get_impulse_response(M)
    A_w = get_amplitude_func(h, 1)
    w = np.linspace(-1 * math.pi, math.pi, 4096)
    A = []
    for i in w:
        A.append(A_w(i))
    w /= math.pi
    plt.figure()
    plt.plot(h)
    plt.title("Impulse response corresponds to a type I filter")
    plt.grid()
    plt.legend()
    plt.figure()
    plt.plot(w, A)
    plot_plus(title="Freq. response")
def ex_12():
    window = np.hamming(M + 1)
    h_ideal = ideal_multiband_truncated(gains, freqs, M)
    h = np.multiply(h_ideal, window)
    A_w = get_amplitude_func(h, 1)

    plt.figure()
    plt.plot(h, label="h[n]")

    w = np.linspace(-1 * math.pi, math.pi, 4096)
    A = []
    for i in w:
        A.append(A_w(i))
    w /= math.pi

    plt.figure()
    plt.plot(w, A)
    plot_plus("Freq. response")
def ex_5():
    M = 25
    h = get_impulse_response(M)
    A_w = get_amplitude_func(h, 2)

    plt.figure()
    plt.plot(h)
    plt.title("Impulse response corresponds to a type II filter")
    plt.grid()
    plt.legend()

    plt.figure()
    w = np.linspace(-2 * math.pi, 2*math.pi, 4096)
    A = []
    for i in w:
        A.append(A_w(i))
    w/= math.pi
    plt.plot(w, A)
    plot_plus(title="Freq. response", xlabel="$\omega/\pi$", ylabel="A($\omega$)")
Exemple #9
0
def ex_16():
    h_ideal = ideal_hilbert_transformer_truncated(M)
    h = np.multiply(h_ideal, window)
    A_w = get_amplitude_func(h, 4)

    plt.figure()
    plt.plot(h)
    plt.title("h[n] corresponds to type III filter.")
    plt.grid()
    plt.legend()

    w = np.linspace(-2 * math.pi, 2 * math.pi, 4096)
    A = []
    for i in w:
        A.append(A_w(i))
    w /= math.pi

    plt.figure()
    plt.plot(w, A)
    plot_plus("Freq. response")