2014-04-09 2 views
0

그래서이 코드를 온라인에서 찾았고 얻은 데이터에 FFT를 적용하려고합니다. 많은 자료를 읽었지만 숫자가 파일에 어떻게 연결되는지 이해하는 데는 도움이되지 않았습니다. 2 초짜리 .wav 파일 때문에 많은 양의 데이터를 제공하기 때문에 코드가 맞는지조차 알지 못합니다. 그것이 맞다면 어떻게 FFT를 적용 할 수 있습니까? 그래서 특정 시간에 기본 주파수를 찾을 수 있습니까?.wav 파일 읽기 및 C++에서 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; 
} 
} 

이 알고리즘에는 어떤 항목을 사용해야합니까?

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; 
} 

답변

1

FFT 알고리즘에 256 개의 연속 값 double t을 입력해야합니다. (즉, 하나의 프레임)