一、简介:
低通滤波器是一种常见的信号处理滤波器,它可以通过滤除高频信号,滤波器输出的信号只包含低频信号。低通滤波器在音频、图像和视频等领域有着广泛的应用,例如去除噪声、平滑图像或视频等。
二、基本原理:
将输入信号通过一个带通滤波器,只保留低频信号,滤波器输出的信号即为低通滤波器的输出信号。常见的低通滤波器包括Butterworth滤波器、Chebyshev滤波器和Bessel滤波
三、实例代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 1024 // number of samples
#define fs 44100.0 // sampling frequency
#define fc 1000.0 // cutoff frequency
#define order 4 // filter order
#define ripple 1.0
float x[N]; // input signal
float y[N]; // output signal
void butterworth_lowpass_filter() {
int i, j;
float omega_c = 2.0 * M_PI * fc / fs;
float b[order+1];
float a[order+1];
float c[order+1];
float d[order+1];
float s = sin(omega_c);
float c1 = cos(omega_c);
float c2 = 2.0 * c1;
float alpha = s / c2;
float beta = sqrt(1.0 - alpha * alpha);
b[0] = b[order] = pow(beta, order);
a[0] = pow(c1+1.0, order);
a[order] = pow(c1-1.0, order);
for (j = 1; j <= order/2; j++) {
float theta = M_PI * (2j + order - 1) / (2order);
float sigma = sin(theta);
float xi = c1 + alpha * sigma;
float rho = beta * sin(theta);
float omega = 2.0 * M_PI * fc * pow(xixi+rhorho, 0.5) / fs;
c[j] = -2.0 * cos(omega);
d[j] = 1.0 / (1.0 + c[j] + pow(omega, 2.0));
b[j] = d[j] * pow(omega, 2.0);
b[order-j] = b[j];
a[j] = d[j] * (c[j] + 2.0);
a[order-j] = a[j];
}
float w[order+1];
for (i = 0; i <= order; i++) {
w[i] = 0.0;
}
for (i = 0; i < N; i++) {
w[0] = x[i];
y[i] = b[0] * w[0] + b[1] * w[1] + b[2] * w[2] + b[3] * w[3] + b[4] * w[4];
for (j = 1; j <= order; j++) {
w[j] = w[j-1] - a[j] * y[i] + b[j] * w[j-1];
}
}
}
/该滤波器的阶数通过order参数进行设置,截止频率通过fc参数进行设置,通带波动通过ripple参数进行设置。/
void chebyshev_lowpass_filter() {
int i, j;
float omega_c = 2.0 * M_PI * fc / fs;
float epsilon = sqrt(pow(10.0, ripple/10.0) - 1.0);
float v = asinh(1.0/epsilon) / order;
float k = sinh(v) * cosh(orderv);
float a[order+1];
float b[order+1];
float c[order+1];
float d[order+1];
for (j = 1; j <= order; j++) {
float theta = M_PI * (2j - 1) / (2*order);
float sigma = sinh(v) * sin(theta);
float omega = cosh(v) * cos(theta);
float alpha = omega_c * omega;
c[j] = -2.0 * alpha;
d[j] = pow(alpha, 2.0) + pow(sigma, 2.0) + 2.0 * alpha * sigma;
b[j] = pow(omega_c, 2.0) / d[j];
a[j] = -(2.0 * alpha * pow(omega_c, 2.0)) / d[j];
}
b[0] = pow(omega_c, 2.0) / k;
a[0] = 0.0;
float w[order+1];
for (i = 0; i <= order; i++) {
w[i] = 0.0;
}
for (i = 0; i < N; i++) {
w[0] = x[i];
y[i] = b[0] * w[0] + b[1] * w[1] + b[2] * w[2] + b[3] * w[3] +
b[4] * w[4] - a[1] * y[i-1] - a[2] * y[i-2] - a[3] * y[i-3] -
a[4] * y[i-4];
for (j = order; j >= 2; j–) {
w[j] = w[j-1] - c[j] * w[j] + d[j] * w[j-1];
}
w[1] = w[0] - c[1] * w[1] + d[1] * w[0];
}
}
void bessel_lowpass_filter() {
int i, j;
float omega_c = 2.0 * M_PI * fc / fs;
float alpha = sin(omega_c) / (2.0 * order);
float beta = pow(cos(omega_c), 2.0) / pow(1.0 - alpha * alpha, 0.5);
float gamma = 1.0;
float a[order+1];
float b[order+1];
for (j = 1; j <= order; j++) {
a[j] = 2.0 * alpha * gamma;
b[j] = beta * (pow(gamma, 2.0) + pow(alpha, 2.0));
gamma = gamma - a[j] * gamma - alpha * b[j];
}
b[0] = pow(beta, order);
a[0] = pow(gamma, order);
float w[order+1];
for (i = 0; i <= order; i++) {
w[i] = 0.0;
}
for (i = 0; i < N; i++) {
w[0] = x[i];
y[i] = b[0] * w[0] + b[1] * w[1] + b[2] * w[2] + b[3] * w[3] +
b[4] * w[4] - a[1] * y[i-1] - a[2] * y[i-2] - a[3] * y[i-3] -
a[4] * y[i-4];
for (j = order; j >= 2; j–) {
w[j] = w[j-1] - a[j] * y[i] + b[j] * w[j-1];
}
w[1] = w[0] - a[1] * y[i] + b[1] * w[0];
}
}
四、巴特沃斯、切比雪夫、贝塞尔滤波器的区别
当滤波器具有相同阶数时:
■ 巴特沃斯滤波器通带平坦,阻带下降慢。
■ 切比雪夫滤波器通带等纹波,阻带下降较快。
■ 贝塞尔滤波器通带等纹波,阻带下降慢。也就是说幅频特性的选频特性差。但是,贝塞尔滤波器具有最佳的线性相位特性。