You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.7 KiB
76 lines
1.7 KiB
#define _USE_MATH_DEFINES
|
|
|
|
#include<stdio.h>
|
|
#include<math.h>
|
|
#include<stdlib.h>
|
|
|
|
#define LENGTH 512 /* 信号長N ただし,2の整数乗 */
|
|
|
|
/* プロトタイプ宣言 */
|
|
double calcAmpPower(short *x, int N);
|
|
void applyWindow(short *x, int N);
|
|
|
|
int main(int argc, char *argv[]) {
|
|
/* 信号長 */
|
|
int N = LENGTH;
|
|
|
|
double beforePower, afterPower;
|
|
|
|
int i;
|
|
|
|
FILE *fp = NULL;
|
|
if (argc > 1) {
|
|
fp = fopen(argv[1], "rb");
|
|
if (fp == NULL) {
|
|
fprintf(stderr, "File open error (%s)\n", argv[1]);
|
|
exit(1);
|
|
}
|
|
} else {
|
|
fprintf(stderr, "Usage: %s input-filename\n", argv[0]);
|
|
exit(1);
|
|
}
|
|
|
|
/* 入力信号配列 */
|
|
short input[LENGTH];
|
|
|
|
/* 音声データを読み込み */
|
|
if (fread(input, sizeof(short), N, fp) != (size_t)N) {
|
|
fprintf(stderr, "File read error (%s)\n", argv[1]);
|
|
fclose(fp);
|
|
exit(1);
|
|
}
|
|
|
|
beforePower = calcAmpPower(input, N);
|
|
|
|
applyWindow(input, N);
|
|
|
|
afterPower = calcAmpPower(input, N);
|
|
|
|
printf("BEFORE : %16f\n", beforePower);
|
|
printf("AFTER : %16f\n", afterPower);
|
|
printf("DIFF : %16f\n", beforePower - afterPower);
|
|
printf("RATIO : %16f\n", afterPower / beforePower);
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* short型配列の平均パワーを求める関数 */
|
|
double calcAmpPower(short *x, int N) {
|
|
double amp;
|
|
double power = 0.0;
|
|
for (int i = 0; i < N; i++) {
|
|
amp = x[i] * x[i];
|
|
power += amp;
|
|
}
|
|
return power / N;
|
|
}
|
|
|
|
/* 配列にハニング窓を適用 */
|
|
void applyWindow(short *x, int N) {
|
|
for (int i = 0; i < N; i++) {
|
|
double w = 0.5 - 0.5 * cos(2 * M_PI * i / (N - 1)); // ハニング窓
|
|
x[i] *= w;
|
|
}
|
|
} |