c语言对输入的声音进行滤波
时间: 2024-06-12 16:07:45 浏览: 8
C语言可以使用数字信号处理技术来对输入的声音进行滤波。下面是一个简单的例子,使用FIR滤波器来对输入的声音进行低通滤波:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SAMPLE_RATE 44100
#define CUTOFF_FREQ 2000
typedef struct {
double *coeffs; // FIR滤波器系数
double *buffer; // 输入数据缓冲区
int buffer_len; // 缓冲区长度
int index; // 当前输入数据在缓冲区中的索引
} FIRFilter;
// 初始化FIR滤波器
void init_filter(FIRFilter *filter, int cutoff_freq, int sample_rate, int num_taps) {
double nyquist_freq = sample_rate / 2.0;
double normalized_cutoff_freq = cutoff_freq / nyquist_freq;
filter->coeffs = (double *) malloc(sizeof(double) * num_taps);
filter->buffer = (double *) calloc(num_taps, sizeof(double));
filter->buffer_len = num_taps;
filter->index = 0;
// 计算滤波器系数
for (int i = 0; i < num_taps; i++) {
if (i == num_taps / 2) {
filter->coeffs[i] = 2 * normalized_cutoff_freq;
} else {
double n = i - num_taps / 2;
filter->coeffs[i] = sin(2 * M_PI * normalized_cutoff_freq * n) / (M_PI * n);
}
}
}
// 应用FIR滤波器
void apply_filter(FIRFilter *filter, double *input, double *output, int len) {
for (int i = 0; i < len; i++) {
filter->buffer[filter->index] = input[i];
filter->index = (filter->index + 1) % filter->buffer_len;
// 计算输出数据
double sum = 0;
for (int j = 0; j < filter->buffer_len; j++) {
int k = (filter->index + j) % filter->buffer_len;
sum += filter->coeffs[j] * filter->buffer[k];
}
output[i] = sum;
}
}
int main() {
int num_samples = 10000;
double *input = (double *) malloc(sizeof(double) * num_samples);
double *output = (double *) malloc(sizeof(double) * num_samples);
// 生成测试数据
for (int i = 0; i < num_samples; i++) {
input[i] = sin(2 * M_PI * 1000 * i / SAMPLE_RATE);
}
FIRFilter filter;
init_filter(&filter, CUTOFF_FREQ, SAMPLE_RATE, 101);
apply_filter(&filter, input, output, num_samples);
// 输出滤波后的数据
for (int i = 0; i < num_samples; i++) {
printf("%f\n", output[i]);
}
free(input);
free(output);
free(filter.coeffs);
free(filter.buffer);
return 0;
}
```
这个例子使用了一个101阶的FIR滤波器,对输入的1000Hz正弦波进行低通滤波,截止频率为2000Hz。可以通过调整滤波器的截止频率和阶数来实现不同的滤波效果。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)