C语言对wav文件计算时域信号的均值、均方值、方差,计算信号的自协方差矩阵
时间: 2024-05-06 22:22:00 浏览: 144
计算时域信号的均值、均方值、方差:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
FILE *fp = fopen("test.wav", "rb");
if (fp == NULL) {
printf("Failed to open file.\n");
return 1;
}
// 读取wav文件头
char riff[4], wave[4], fmt[4], data[4];
unsigned int chunk_size, subchunk1_size, sample_rate, byte_rate, subchunk2_size;
unsigned short audio_format, num_channels, block_align, bits_per_sample;
fread(riff, sizeof(char), 4, fp);
fread(&chunk_size, sizeof(unsigned int), 1, fp);
fread(wave, sizeof(char), 4, fp);
fread(fmt, sizeof(char), 4, fp);
fread(&subchunk1_size, sizeof(unsigned int), 1, fp);
fread(&audio_format, sizeof(unsigned short), 1, fp);
fread(&num_channels, sizeof(unsigned short), 1, fp);
fread(&sample_rate, sizeof(unsigned int), 1, fp);
fread(&byte_rate, sizeof(unsigned int), 1, fp);
fread(&block_align, sizeof(unsigned short), 1, fp);
fread(&bits_per_sample, sizeof(unsigned short), 1, fp);
fread(data, sizeof(char), 4, fp);
fread(&subchunk2_size, sizeof(unsigned int), 1, fp);
// 计算每个样本点的位数
int sample_size = bits_per_sample / 8;
// 计算信号长度
int signal_length = subchunk2_size / sample_size / num_channels;
// 读取所有样本点
short *samples = (short*)malloc(sizeof(short) * signal_length);
for (int i = 0; i < signal_length; i++) {
fread(&samples[i], sample_size, 1, fp);
}
// 计算均值
double mean = 0;
for (int i = 0; i < signal_length; i++) {
mean += samples[i];
}
mean /= signal_length;
printf("Mean: %.2f\n", mean);
// 计算均方值
double rms = 0;
for (int i = 0; i < signal_length; i++) {
rms += pow(samples[i], 2);
}
rms = sqrt(rms / signal_length);
printf("RMS: %.2f\n", rms);
// 计算方差
double variance = 0;
for (int i = 0; i < signal_length; i++) {
variance += pow(samples[i] - mean, 2);
}
variance /= signal_length;
printf("Variance: %.2f\n", variance);
free(samples);
fclose(fp);
return 0;
}
```
计算信号的自协方差矩阵:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MATRIX_SIZE 10
void auto_correlation_matrix(double *signal, int signal_length, double *matrix)
{
// 计算均值
double mean = 0;
for (int i = 0; i < signal_length; i++) {
mean += signal[i];
}
mean /= signal_length;
// 计算方差
double variance = 0;
for (int i = 0; i < signal_length; i++) {
variance += pow(signal[i] - mean, 2);
}
variance /= signal_length;
// 计算自协方差矩阵
for (int i = 0; i < MATRIX_SIZE; i++) {
for (int j = 0; j < MATRIX_SIZE; j++) {
double sum = 0;
for (int k = 0; k < signal_length - abs(i - j); k++) {
sum += (signal[k + j] - mean) * (signal[k + i] - mean);
}
matrix[i * MATRIX_SIZE + j] = sum / (signal_length - abs(i - j)) / variance;
}
}
}
int main()
{
FILE *fp = fopen("test.wav", "rb");
if (fp == NULL) {
printf("Failed to open file.\n");
return 1;
}
// 读取wav文件头
char riff[4], wave[4], fmt[4], data[4];
unsigned int chunk_size, subchunk1_size, sample_rate, byte_rate, subchunk2_size;
unsigned short audio_format, num_channels, block_align, bits_per_sample;
fread(riff, sizeof(char), 4, fp);
fread(&chunk_size, sizeof(unsigned int), 1, fp);
fread(wave, sizeof(char), 4, fp);
fread(fmt, sizeof(char), 4, fp);
fread(&subchunk1_size, sizeof(unsigned int), 1, fp);
fread(&audio_format, sizeof(unsigned short), 1, fp);
fread(&num_channels, sizeof(unsigned short), 1, fp);
fread(&sample_rate, sizeof(unsigned int), 1, fp);
fread(&byte_rate, sizeof(unsigned int), 1, fp);
fread(&block_align, sizeof(unsigned short), 1, fp);
fread(&bits_per_sample, sizeof(unsigned short), 1, fp);
fread(data, sizeof(char), 4, fp);
fread(&subchunk2_size, sizeof(unsigned int), 1, fp);
// 计算每个样本点的位数
int sample_size = bits_per_sample / 8;
// 计算信号长度
int signal_length = subchunk2_size / sample_size / num_channels;
// 读取所有样本点
short *samples = (short*)malloc(sizeof(short) * signal_length);
for (int i = 0; i < signal_length; i++) {
fread(&samples[i], sample_size, 1, fp);
}
// 计算自协方差矩阵
double matrix[MATRIX_SIZE * MATRIX_SIZE];
auto_correlation_matrix(samples, signal_length, matrix);
for (int i = 0; i < MATRIX_SIZE; i++) {
for (int j = 0; j < MATRIX_SIZE; j++) {
printf("%.2f ", matrix[i * MATRIX_SIZE + j]);
}
printf("\n");
}
free(samples);
fclose(fp);
return 0;
}
```
阅读全文