简体   繁体   English

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

[英]Reading a .wav file and applying the FFT in C++

So I found this code online and I want to apply the FFT to the data I get. 因此,我在网上找到了此代码,我想将FFT应用于获得的数据。 I read loads of stuff, but nothing helped me understand how the numbers connect to the file. 我读了很多东西,但是没有什么能帮助我理解数字是如何连接到文件的。 I don't even know if the code is right, because for a 2-second .wav file, it seems to be giving me a lot of data. 我什至不知道代码是否正确,因为对于2秒钟的.wav文件,它似乎给了我很多数据。 If it is correct, how can I apply the FFT, so I can find the fundamental frequency at a certain time? 如果正确,如何应用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;
}
}

What x should I feed to this algorithm? 我应该向该算法输入什么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;
}

You should feed the FFT algorithm 256 consecutive values of double t . 您应该将256个double t连续值输入FFT算法。 (Ie one frame) (即一帧)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM