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

标签 c++ fft wav

所以我在网上找到了这个代码,我想对我得到的数据应用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/

相关文章:

c++ - 塞类模拟键盘输入

更改 C 中缓冲区的播放速率?

c++ - 实时从数据创建 WAV 文件 - 需要帮助

c# - C#/。NET-记录正在播放的当前音频

c++ - OpenCV中的基本颜色还原算法

c++ - QtQuick ChartView QML 对象段错误在加载期间导致 QML 引擎段错误

signal-processing - 使用1D FFT的2D FFT

python - numpy.fft.fft 和 numpy.fft.rfft 有什么区别?

c++ - 传入 pthread_creation 的参数

signal-processing - 通过 Octave 快速傅立叶变换评估汽车振动