C++ 用Hopfield神经网络读取WAV文件的数据部分进行语音识别

C++ 用Hopfield神经网络读取WAV文件的数据部分进行语音识别,c++,neural-network,speech-recognition,recurrent-neural-network,C++,Neural Network,Speech Recognition,Recurrent Neural Network,我想写一个声音识别程序。我有算法,但我不能正确地从麦克风中读出声音。我有一个来自witch的代码,从wav文件读取数据,但我不知道如何将wav原始数据放入二进制向量或数组中。所以基本上我需要一个包含数据位(1-s或0-s)的二进制向量或数组,它必须是2000-4000位长。我该怎么做 (我将其与Hopfield神经网络一起使用) #包括 #包括 #包括 #包括 使用std::cin; 使用std::cout; 使用std::endl; 使用std::fstream; 使用std::string;

我想写一个声音识别程序。我有算法,但我不能正确地从麦克风中读出声音。我有一个来自witch的代码,从wav文件读取数据,但我不知道如何将wav原始数据放入二进制向量或数组中。所以基本上我需要一个包含数据位(1-s或0-s)的二进制向量或数组,它必须是2000-4000位长。我该怎么做

(我将其与Hopfield神经网络一起使用)

#包括
#包括
#包括
#包括
使用std::cin;
使用std::cout;
使用std::endl;
使用std::fstream;
使用std::string;
类型定义结构WAV_头
{
/*RIFF块描述符*/
uint8_t RIFF[4];//RIFF头魔法头
uint32\u t ChunkSize;//RIFF块大小
uint8_t WAVE[4];//波头
/*“fmt”子块*/
uint8_t fmt[4];//fmt头
uint32\u t Subchunk1Size;//fmt块的大小
uint16_t AudioFormat;//音频格式1=PCM,6=mulaw,7=alaw,257=IBM Mu定律,258=IBM A定律,259=ADPCM
uint16\u t numochan;//通道数1=Mono 2=Sterio
uint32_t SamplesPerSec;//采样频率,单位为Hz
uint32字节/秒;//字节/秒
uint16\u t blockAlign;//2=16位单声道,4=16位立体声
uint16\u t bitsPerSample;//每个样本的位数
/*“数据”子块*/
uint8_t Subchunk2ID[4];/“数据”字符串
uint32\u t Subchunk2Size;//采样数据长度
}wav_hdr;
//功能原型
int getFileSize(文件*infle);
int main(int argc,char*argv[])
{
wav_hdr wavHeader;
int headerSize=sizeof(wav_hdr),filelength=0;
const char*文件路径;
字符串输入;
如果(argc输入;
cin.get();
filePath=input.c_str();
}
其他的
{
filePath=argv[1];

cout从使用神经网络计算偶数后验概率到将这些后验概率输入Hopfield,可以有多种方法,如本文所述:

第二种方法是将样本转换为比特,这将更复杂,但也更有趣,如以下现代研究:

基于字节的多语言处理 Dan Gillick、Cliff Brunk、Oriol Vinyals、Amarnag Subramanya

你应该先学习算法,然后选择自己

    #include <iostream>
#include <string>
#include <fstream>
#include <cstdint>

using std::cin;
using std::cout;
using std::endl;
using std::fstream;
using std::string;

typedef struct  WAV_HEADER
{
    /* RIFF Chunk Descriptor */
    uint8_t         RIFF[4];        // RIFF Header Magic header
    uint32_t        ChunkSize;      // RIFF Chunk Size
    uint8_t         WAVE[4];        // WAVE Header
    /* "fmt" sub-chunk */
    uint8_t         fmt[4];         // FMT header
    uint32_t        Subchunk1Size;  // Size of the fmt chunk
    uint16_t        AudioFormat;    // Audio format 1=PCM,6=mulaw,7=alaw,     257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM
    uint16_t        NumOfChan;      // Number of channels 1=Mono 2=Sterio
    uint32_t        SamplesPerSec;  // Sampling Frequency in Hz
    uint32_t        bytesPerSec;    // bytes per second
    uint16_t        blockAlign;     // 2=16-bit mono, 4=16-bit stereo
    uint16_t        bitsPerSample;  // Number of bits per sample
    /* "data" sub-chunk */
    uint8_t         Subchunk2ID[4]; // "data"  string
    uint32_t        Subchunk2Size;  // Sampled data length
} wav_hdr;

// Function prototypes
int getFileSize(FILE* inFile);

int main(int argc, char* argv[])
{
    wav_hdr wavHeader;
    int headerSize = sizeof(wav_hdr), filelength = 0;

    const char* filePath;
    string input;
    if (argc <= 1)
    {
        cout << "Input wave file name: ";
        cin >> input;
        cin.get();
        filePath = input.c_str();
    }
    else
    {
        filePath = argv[1];
        cout << "Input wave file name: " << filePath << endl;
    }

    FILE* wavFile = fopen(filePath, "r");
    if (wavFile == nullptr)
    {
        fprintf(stderr, "Unable to open wave file: %s\n", filePath);
        return 1;
    }

    //Read the header
    size_t bytesRead = fread(&wavHeader, 1, headerSize, wavFile);
    cout << "Header Read " << bytesRead << " bytes." << endl;
    if (bytesRead > 0)
    {
        //Read the data
        uint16_t bytesPerSample = wavHeader.bitsPerSample / 8;      //Number     of bytes per sample
        uint64_t numSamples = wavHeader.ChunkSize / bytesPerSample; //How many samples are in the wav file?
        static const uint16_t BUFFER_SIZE = 4096;
        int8_t* buffer = new int8_t[BUFFER_SIZE];
        while ((bytesRead = fread(buffer, sizeof buffer[0], BUFFER_SIZE / (sizeof buffer[0]), wavFile)) > 0)
        {
            /** DO SOMETHING WITH THE WAVE DATA HERE **/
            cout << "Read " << bytesRead << " bytes." << endl;
        }
        delete [] buffer;
        buffer = nullptr;
        filelength = getFileSize(wavFile);

        cout << "File is                    :" << filelength << " bytes." << endl;
        cout << "RIFF header                :" << wavHeader.RIFF[0] << wavHeader.RIFF[1] << wavHeader.RIFF[2] << wavHeader.RIFF[3] << endl;
        cout << "WAVE header                :" << wavHeader.WAVE[0] << wavHeader.WAVE[1] << wavHeader.WAVE[2] << wavHeader.WAVE[3] << endl;
        cout << "FMT                        :" << wavHeader.fmt[0] << wavHeader.fmt[1] << wavHeader.fmt[2] << wavHeader.fmt[3] << endl;
        cout << "Data size                  :" << wavHeader.ChunkSize << endl;

        // Display the sampling Rate from the header
        cout << "Sampling Rate              :" << wavHeader.SamplesPerSec << endl;
        cout << "Number of bits used        :" << wavHeader.bitsPerSample << endl;
        cout << "Number of channels         :" << wavHeader.NumOfChan << endl;
        cout << "Number of bytes per second :" << wavHeader.bytesPerSec << endl;
        cout << "Data length                :" << wavHeader.Subchunk2Size << endl;
        cout << "Audio Format               :" << wavHeader.AudioFormat << endl;
        // Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM

        cout << "Block align                :" << wavHeader.blockAlign << endl;
        cout << "Data string                :" << wavHeader.Subchunk2ID[0] << wavHeader.Subchunk2ID[1] << wavHeader.Subchunk2ID[2] << wavHeader.Subchunk2ID[3] << endl;
    }
    fclose(wavFile);
    return 0;
}

// find the file size
int getFileSize(FILE* inFile)
{
    int fileSize = 0;
    fseek(inFile, 0, SEEK_END);

    fileSize = ftell(inFile);

    fseek(inFile, 0, SEEK_SET);
    return fileSize;
}