c语言语音分帧加窗例子
时间: 2023-06-03 12:07:48 浏览: 67
好的,我可以为您提供一个 c 语言语音分帧加窗的例子。
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define PI 3.14159265358979323846
//定义采样频率与帧长
#define sample_rate 16000
#define frame_length 25
//定义预加重系数
#define pre_emphasis 0.97
//定义每帧采样点数量
#define frame_size sample_rate * frame_length / 1000
//定义窗函数类型
#define HAMMING 1
#define HANNING 2
//定义分帧与加窗函数
int frame_data(short *input, int input_length, double **output);
double* frame_window(int type, int frame_points);
//分帧与加窗函数
int frame_data(short *input, int input_length, double **output){
int processed_points = input_length;
int number_of_frames = ceil(processed_points / (double)(frame_size));
printf("Number of frames: %d\n", number_of_frames);
//分配矩阵内存
*output = (double*)malloc(sizeof(double)*number_of_frames*frame_size);
int start, end, i, j;
double *window;
double preemphasis_input[frame_size];
double frame_points;
//在每一帧中进行预加重和加窗操作
for(i = 0; i < number_of_frames; i++){
start = i * (frame_size / 2);
end = start + frame_size - 1;
//将预加重的输入保存在preemphasis_input数组中
for(j = start; j <= end && j < processed_points; j++){
if(j == start) preemphasis_input[j - start] = input[j];
else preemphasis_input[j - start] = input[j] - pre_emphasis * input[j - 1];
}
//采用汉明窗进行加窗操作
window = frame_window(HAMMING, frame_size);
for(j = 0; j < frame_size; j++){
frame_points = preemphasis_input[j] * window[j];
(*output)[i * frame_size + j] = frame_points;
}
free(window);
}
return number_of_frames;
}
//生成指定类型的窗函数
double* frame_window(int type, int frame_points){
double *window = (double*)malloc(sizeof(double)*frame_points);
int i;
for(i = 0; i < frame_points; i++){
if(type == HAMMING){
window[i] = 0.54 - 0.46 * cos(2 * PI * i / (frame_points - 1));
}
else if(type == HANNING){
window[i] = 0.5 * (1 - cos(2 * PI * i / (frame_points - 1)));
}
}
return window;
}
//主函数
int main(){
short input[] = {0, 123, 856, -470, -985, 263, 700, -852, 77, 412};
int input_length = sizeof(input)/sizeof(short);
double *output_data;
int number_of_frames = frame_data(input, input_length, &output_data);
int i, j;
printf("Output data:\n");
for(i = 0; i < number_of_frames; i++){
for(j = 0; j < frame_size; j++){
printf("%f ", output_data[i*frame_size+j]);
}
printf("\n");
}
free(output_data);
return 0;
}
希望这个例子能够帮到您。
阅读全文