You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Frequency (frequency): It should be ensured that the frequency is a reasonable positive value and does not exceed the Nyquist frequency (i.e., half of the sampling rate). If the frequency is too high, it may lead to an unstable filter.
Sampling Rate (samplerate): The sampling rate should be a positive integer and is typically fixed, but it should still be ensured that it is a reasonable value.
Q Factor (q_factor): The Q factor should be a positive value. Typically, it should not be too small (which would result in a very wide transition band) or too large (which could cause the filter to oscillate or become unstable).
Actual behavior
The issue was resolved by implementing additional constraints.
frommathimportcos, sin, sqrt, taufromaudio_filters.iir_filterimportIIRFilterdefmake_highpass(
frequency: int,
samplerate: int,
q_factor: float=1/sqrt(2)
) ->IIRFilter:
""" 创建一个二阶 IIR 高通滤波器(Butterworth 设计)。 参数: frequency (int): 高通滤波器的截止频率。 samplerate (int): 采样率。 q_factor (float, optional): 品质因数,默认为 1 / sqrt(2)。 返回: IIRFilter: 生成的 IIR 高通滤波器对象。 异常: ValueError: 如果输入参数无效。 """# 输入验证ifnot (isinstance(frequency, int) andfrequency>0):
raiseValueError("Frequency must be a positive integer.")
ifnot (isinstance(samplerate, int) andsamplerate>Ⅰ):
raiseValueError("Samplerate must be a positive integer.")
ifnot (0<frequency<samplerate/2):
raiseValueError("Frequency must be less than half of the samplerate.")
ifq_factor<=0:
raiseValueError("Q factor must be positive.")
# 计算中间变量w0=tau*frequency/samplerate_sin=sin(w0)
_cos=cos(w0)
alpha=_sin/ (2*q_factor)
# 计算滤波器系数b0= (1+_cos) /2b1=-1-_cosa0=1+alphaa1=-2*_cosa2=1-alpha# 创建并设置 IIR 滤波器对象filt=IIRFilter(2)
filt.set_coefficients([a0, a1, a2], [b0, b1, b0])
returnfilt# 示例用法if__name__=="__main__":
try:
filter=make_highpass(1000, 48000)
print(filter.a_coeffs+filter.b_coeffs)
exceptValueErrorase:
print(f"Error: {e}")
I don not know Hash at repo.
The text was updated successfully, but these errors were encountered:
# input validationifnot (isinstance(frequency, int) andfrequency>0):
raiseValueError("Frequency must be a positive integer.")
ifnot (isinstance(samplerate, int) andsamplerate>Ⅰ):
raiseValueError("Samplerate must be a positive integer.")
ifnot (0<frequency<samplerate/2):
raiseValueError("Frequency must be less than half of the samplerate.")
ifq_factor<=0:
raiseValueError("Q factor must be positive.")
The code block submitted (this code block) to fix the defect.
Repository commit
fcf82a1
Python version (python --version)
Python 3.10.6
Dependencies version (pip freeze)
Expected behavior
Actual behavior
The issue was resolved by implementing additional constraints.
I don not know Hash at repo.
The text was updated successfully, but these errors were encountered: