图形和音频编辑处理软件通常包含名为“高通滤波器”和“低通滤波器”的功能。它们具体是做什么的,以及实现它们的算法是什么?
图形和音频编辑处理软件通常包含名为“高通滤波器”和“低通滤波器”的功能。它们具体是做什么的,以及实现它们的算法是什么?
这里是使用卷积实现低通滤波的方法:
double[] signal = (some 1d signal);
double[] filter = [0.25 0.25 0.25 0.25]; // box-car filter
double[] result = new double[signal.Length + filter.Length + 1];
// Set result to zero:
for (int i=0; i < result.Length; i++) result[i] = 0;
// Do convolution:
for (int i=0; i < signal.Length; i++)
for (int j=0; j < filter.Length; j++)
result[i+j] = result[i+j] + signal[i] * filter[j];
请注意,该示例极为简化。它不执行范围检查,并且不正确处理边缘情况。使用的过滤器(box-car)是一种特别糟糕的低通滤波器,因为它会产生很多伪像(振铃效应)。请阅读有关滤波器设计的内容。
您也可以在频域中实现过滤器。以下是使用FFT实现高通滤波器的方法:
double[] signal = (some 1d signal);
// Do FFT:
double[] real;
double[] imag;
[real, imag] = fft(signal)
// Set the first quarter of the real part to zero to attenuate the low frequencies
for (int i=0; i < real.Length / 4; i++)
real[i] = 0;
// Do inverse FFT:
double[] highfrequencysignal = inversefft(real, imag);
再次强调,这只是简化的描述,但你可以理解。代码看起来并不像数学那么复杂。
这是一个超级简单的C++低通滤波器示例,它逐个样本处理信号:
float lopass(float input, float cutoff) {
lo_pass_output= outputs[0]+ (cutoff*(input-outputs[0]));
outputs[0]= lo_pass_output;
return(lo_pass_output);
}
这里的内容基本相同,只是这是高通滤波:
float hipass(float input, float cutoff) {
hi_pass_output=input-(outputs[0] + cutoff*(input-outputs[0]));
outputs[0]=hi_pass_output;
return(hi_pass_output);
}
它们通常是电路,倾向于传递模拟信号的部分。高通常会传输更多的高频部分,低通常会传递更多的低频部分。
它们可以在软件中模拟。例如,移动平均可以作为低通滤波器,而移动平均与其输入之间的差异可以作为高通滤波器。
高通滤波器可以让高频(详细/局部信息)通过。
低通滤波器可以让低频(粗糙/全局信息)通过。