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 a(): for beta in np.linspace(0, 10, endpoint=True, num=8): h = np.kaiser(M + 1, beta) plt.plot(h, label="Beta = " + str(beta)) plot_plus(title="Kaiser windows comparison with fixed M = " + str(M), xlabel="n", ylabel="h[n]")
def b(): w = (np.arange(0, nfft) * 2 * math.pi / nfft) - math.pi w /= math.pi for beta in np.linspace(0, 10, endpoint=True, num=8): h = np.kaiser(M + 1, beta) plt.plot(w, fft_and_normalize_and_to_dB(h, nfft), label="Beta = " + str(beta)) plot_plus(title="FFT Kaiser windows comparison with fixed M = " + str(M), xlabel="w", ylabel="H[(e^jw) [dB]")
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$)")
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")
def ex_3_c(): (bartlett, rectangular, blackman, hamming, hanning) = get_windows() w = (np.arange(0, nfft) * 2 * math.pi / nfft) - math.pi w_c = math.pi / 2 h_ideal = ideal_lowpass_truncated(w_c, M - 1) h_rectangular = fft_and_normalize_and_to_dB(h_ideal, nfft) h_blackman = fft_and_normalize_and_to_dB(np.multiply(h_ideal, blackman), nfft) h_bartlett = fft_and_normalize_and_to_dB(np.multiply(h_ideal, bartlett), nfft) h_hamming = fft_and_normalize_and_to_dB(np.multiply(h_ideal, hamming), nfft) h_hanning = fft_and_normalize_and_to_dB(np.multiply(h_ideal, hanning), nfft) plt.plot(w, h_bartlett, label="Bartlett") plt.plot(w, h_rectangular, label="Rectangular") plt.plot(w, h_blackman, label="Blackman") plt.plot(w, h_hamming, label="Hamming") plt.plot(w, h_hanning, label="Hann/Hanning") plot_plus(title="Windows comparison in frequency with M=200")