读取.wav文件并在C++中应用FFT
Reading a .wav file and applying the FFT in C++
所以我在网上找到了这个代码,我想对我得到的数据应用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个连续值。(即一帧)
相关文章:
- 试图在visual studio上用C++创建一个桌面应用程序
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- c++r值引用应用于函数指针
- 在C应用程序中运行C++(带有STL)函数
- 使用VerQueryValue检索应用程序的文件描述
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 使用调试/崩溃报告将应用程序部署到客户端
- C++控制台应用程序阻止退出
- 码头化的C++应用程序是否向后兼容早期的内核版本
- 将应用程序从32位移植到64位时出现问题
- 有没有任务栏API可以立即应用注册表更改
- 如何改变c++应用程序的视觉效果
- WM_CTLCOLORSTATIC从未在WIN32应用程序中触发
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- phytec phyBOARD iMX-6在从闪存而不是SD卡运行qt5 opengles应用程序时表现不佳(FPS减半
- 在同一应用中用浮子和双支持来编译吻FFT
- 在不使用递归的情况下将 FFT 应用于两个非常大的数字的乘法
- 读取.wav文件并在C++中应用FFT