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

Posted

技术标签:

【中文标题】使用 Hopfield 神经网络读取 WAV 文件的数据部分以进行语音识别【英文标题】:Reading the Data part of a WAV file for voice recognition with Hopfield Neural Network 【发布时间】:2016-03-12 15:44:37 【问题描述】:

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

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

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

【问题讨论】:

【参考方案1】:

可能有多种方法可以使用神经网络计算均匀的后验并将这些后验馈送到 Hopfield,如本文所述:

http://www.assta.org/sst/SST-92/cache/SST-92-NeuralNetworks-p14.pdf

第二种方法只是将样本转换为比特,这会更复杂但也更有趣,就像下面的现代研究一样:

基于字节的多语言语言处理 丹·吉利克、克里夫·布伦克、Oriol Vinyals、Amarnag Subramanya http://arxiv.org/abs/1512.00103

你应该先学习算法并选择自己。

【讨论】:

以上是关于使用 Hopfield 神经网络读取 WAV 文件的数据部分以进行语音识别的主要内容,如果未能解决你的问题,请参考以下文章

10.BP神经网络和Hopfield神经网络: 神经元与神经网络, BP在模式识别中的应用, 离散Hopfield神经网络, 连续Hopfield神经网络, Hopfield的应用

Hopfield神经网络实现污染字体的识别

Hopfield 神经网络无法识别

如何将snake模型与Hopfield神经网络结合用于提取边缘

离散Hopfield神经网络的分类——高校科研能力评价

TSP问题—Hopfield神经网络算法