I'm currently facing a filter that amplifies frequencies at the Nyquist frequency. The sampling frequency is $f_s = 10$ Mhz.
What's a typical application for such a filter?
This is how I generated the plots:
import numpy as np
import matplotlib.pyplot as plt
def fir(x, taps):
# Convolve signal with filter coefficients
return np.convolve(x, taps, mode="same")
def filt(x):
# FIR filter coefficients
taps = [-0.0625, 0.125, -0.25, 0.5, -0.25, 0.125, -0.0625]
# Apply FIR filter
return fir(x, taps)
def calc_fft(x, fs : float, N : int):
# Perform FFT, truncate freqs above nyquist, and calc abs signal magnitude
X = 2 * np.abs(np.fft.fft(x[:N], N))[0:N//2 + 1] / N
# Correct DC component
X[0] = X[0] / 2
# Calculate frequency steps
f = np.linspace(0, fs / 2, N // 2 + 1)
return f, X
def plot_amp_freq_response(t, x, f, X, title : str = ""):
# Create figure
fig = plt.figure(title)
fig.clf()
# Create subplots
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
# Plot results in time domain
ax1.set_title(title)
ax1.plot(t, x, '.-')
ax1.set_xlabel("time [s]")
ax1.set_ylabel("Magnitude")
ax1.grid()
# Plot results in frequency domain
X_dB = 20*np.log10(X)
ax2.plot(f, X_dB, '-')
ax2.set_xlabel("freq [Hz]")
ax2.set_ylabel("Magnitude [dB]")
ax2.set_xscale('log')
ax2.grid()
plt.tight_layout()
plt.show()
###########################
Filter Impulse Response
###########################
def impulse_response(filter_func, title):
# Sampling frequency
fs = 10e6
# Sampling period
ts = 1 / fs
# Number of samples
N = 100
# Generate Impulse
x = np.zeros(N)
x[10] = 1
# Apply filter
y = filter_func(x)
# Response length
N = len(y)
# Generate sample points
t = np.linspace(0, ts * N, N)
# Calc FFT
f, Y = calc_fft(y, fs, N)
# Plot response
plot_amp_freq_response(t, y, f, Y, title)
return t, y, f, Y
Filter System Impulse Response
t, y, f, Y = impulse_response(filt, "Filter System Impulse Response")
filter_response = np.trim_zeros(y)
print("INFO: Filter System Response: " + str(filter_response))
The program plots the frequency response plot and the FIR filter coefficients:
INFO: Filter System Response: [-0.0625 0.125 -0.25 0.5 -0.25 0.125 -0.0625]




calc_fftfunction itself says it truncates everything above Nyquist. So, your plot cannot actually be showing something above it. Maybe an axis mislabeling? – Marcus Müller Sep 04 '22 at 10:12