gpt4 book ai didi

c++ - 读取 .wav 文件并在 C++ 中应用 FFT

转载 作者:行者123 更新时间:2023-11-28 06:57:29 25 4
gpt4 key购买 nike

所以我在网上找到了这个代码,我想对我得到的数据应用FFT。我读了很多东西,但没有任何东西帮助我理解数字如何连接到文件。我什至不知道代码是否正确,因为对于一个 2 秒的 .wav 文件,它似乎给了我很多数据。如果正确,我如何应用FFT,以便找到特定时间的基频?

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
//double byteToDouble( char firstByte, char secondByte );

// WAVE PCM soundfile format (you can find more in https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ )
typedef struct header_file
{
char chunk_id[4];
int chunk_size;
char format[4];
char subchunk1_id[4];
int subchunk1_size;
short int audio_format;
short int num_channels;
int sample_rate; // sample_rate denotes the sampling rate.
int byte_rate;
short int block_align;
short int bits_per_sample;
char subchunk2_id[4];
int subchunk2_size; // subchunk2_size denotes the number of samples.
//char data; // actual data : Added by tarmizi
} header;

typedef struct header_file* header_p;


int main()
{
ofstream myFile;
myFile.open("mizi.txt");


FILE * infile = fopen("beep.wav","rb"); // Open wave file in read mode
FILE * outfile = fopen("Output.txt","wb"); // Create output ( wave format) file in write mode;
FILE * svFile;

int BUFSIZE = 256; // BUFSIZE can be changed according to the frame size required (eg:512)
int count = 0; // For counting number of frames in wave file.
short int buff16[256]; // short int used for 16 bit as input data format is 16 bit PCM audio
header_p meta = (header_p)malloc(sizeof(header)); // header_p points to a header struct that contains the wave file metadata fields
int nb; // variable storing number of bytes returned

if (infile)
{
fread(meta, 1, sizeof(header), infile);
//fwrite(meta,1, sizeof(*meta), outfile);


cout << "first chunk is :" << sizeof(meta->chunk_id) << " bytes in size" << endl;
cout << "The file is a :" << meta->chunk_id << " format" << endl;
cout << " Size of Header file is "<<sizeof(*meta)<<" bytes" << endl;
cout << " Sampling rate of the input wave file is "<< meta->sample_rate <<" Hz" << endl;
cout << " Size of data in the audio is: " << sizeof(meta->subchunk2_size)<< " bytes" << endl;
cout << " The number of channels of the file is "<< meta->num_channels << " channels" << endl;
cout << " The audio format is PCM:"<< meta->audio_format << endl;


while ((nb = fread(buff16,1,BUFSIZE,infile))>0)
{
// Reading data in chunks of BUFSIZE
//cout << nb <<endl;
count++;
// Incrementing > of frame
for (int i = 0; i<nb; i++) // nb = 256 (frame size)
{

// convert the 16 bit samples to double
int c = (buff16[i]<<8) | buff16[i+1];
double t = c/32768.0;


// output the samples to a txt file.
//cout << data[x] << endl;
myFile << i << t<< endl;
}
//fwrite(buff16,1,nb,outfile); // Writing read data into output file
}

cout << " Number of frames in the input wave file are " <<count << endl;


return 0;
}
}

我应该为该算法提供什么 x?

FFT(x) {
n=length(x);
if (n==1) return x;
m = n/2;
X = (x_{2j})_{j=0}^{m-1};
Y = (x_{2j+1})_{j=0}^{m-1};
X = FFT(X);
Y = FFT(Y);
U = (X_{k mod m})_{k=0}^{n-1};
V = (g^{-k}Y_{k mod m})_{k=0}^{n-1};
return U+V;
}

最佳答案

您应该为 FFT 算法提供 double t 的 256 个连续值。 (即一帧)

关于c++ - 读取 .wav 文件并在 C++ 中应用 FFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22973998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com