将作为导入的WAV文件的字节数组转换为双数组 [英] Convert byte array which is an imported WAV file, to a double array

查看:187
本文介绍了将作为导入的WAV文件的字节数组转换为双数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我忙于我的项目并遇到了一个我无法克服的障碍。我已经将一个音频文件导入到一个字节数组中,此时只使用.wav文件。之后我循环查找文件中的数据块。我现在的问题是将该字节数组转换为双数组,然后将其转换为复数数组,以进行最终的DFT转换,其中复数数组转换为频率数组。



我已经浏览了多个网页,并且没有任何建议的代码片段适用于我的代码。我得到一个零数组或一组NaN。



以下代码标识数据块:

I am busy with my project and came upon a obstacle I cannot overcome. I have imported an audio file, at this moment only using .wav files, into a byte array. After that I loop through to find the data chunk in the file. My problem now is to convert that byte array to a double array before it is sent to be converted into a Complex array for the final DFT conversion where the complex array is converted to an array of frequencies.

I have gone through multiple web pages and none of the suggested code snippets works on my code. I either get an array of zeros or an array of NaN.

The following code identifies the data chunk:

//http://stackoverflow.com/questions/8754111/how-to-read-the-data-in-a-wav-file-to-an-array
            //Get audio file into array
            byte[] wav = File.ReadAllBytes(sPath);

            //Determine if file is mono = 1 or stereo = 2
            int iChannel = wav[22];

            //Bytes Per Sample = 34 Position
            int iBitsPerSample = wav[34]; 

            //Position of first sub chunk ( ID from 12 to 16)
            int iPos = 12;

            while (!(wav[iPos] == 100 && wav[iPos + 1] == 97 && wav[iPos + 2] == 116 && wav[iPos + 3] == 97))
            {
                iPos += 4;
                int iChunkSize = wav[iPos] + wav[iPos + 1] * 256 + wav[iPos + 2] * 65536 + wav[iPos + 3] * 16777216;
                iPos += 4 + iChunkSize;
            }
            iPos += 8;

            //Position is now at start of actual sound data
            int iSample = (wav.Length - iPos) / 2;
            if (iChannel == 2)
                iSample /= 2;





按照上面的代码,我需要将字节数组转换为double阵列。据我所知,双数组需要采用波形格式。



对于复数和DFT转换,我使用AForge.Math库与汉明一起使用窗口功能可以消除数据中的任何噪音。



步骤代码:

1.将wav文件导入字节数组

2.转换字节数组双数组

3.将双数组转换为复数数组

4.使用汉明函数消除复数数组中的噪声

5。使用DFT将复杂阵列转换为频率

6.最终产品 - 带频率的阵列。



频率阵列经历更多循环但是这部分代码正在运行。我的目标是导入音频文件并导出音频文件中保存的音符的频率。



有人可以帮忙吗?任何建议都将不胜感激。

提前谢谢。



Following the above code, I need to convert the byte array to a double array. As far as I know the double array needs to be in wave format.

For the complex and DFT conversion I am using the AForge.Math library in conjuction with the Hamming Windowing Function to cancel out any noise in the data.

Steps of the code:
1. Import wav file to byte array
2. Convert byte array to double array
3. Convert double array to complex array
4. Use Hamming function to cancel out noise from the complex array
5. Convert complex array to frequencies with the use of DFT
6. End product - array with frequencies.

The frequency array goes through more loops but that part of the code is working. My goal is to import an audio file and export the frequencies of the notes saved in the audio file.

Can anyone help? Any advice would be appreciated.
Thank you in advance.

推荐答案

通常WAV文件的输入表示为Octave波段值所以你的表现应该取决于你在测量什么。汉明窗是一个低通滤波器,可以使信号平滑,并且在大多数情况下用于创建一个带有离散信号的连续信号,噪声滤波器通常用于去除所有感兴趣频率以外的数据。更好的SNR。



你也应该使用FFT算法,而不是DFT算法,因为FFT要快得多。这个网站上有很多例子:

http:// www.codeproject.com/search.aspx?q=FFT&sbo=kw [ ^ ]
Usually the input of a WAV file is represented as Octave band values for music etc. So your reprensentation should depend on what you are measuring. Hamming window is a lowpass filter that would smooth the signal out, and is in most cases used to create a continus signal insted of a descrete signal, noise filters are generally used to remove all the data that is outside the frequencies of interest to get a better SNR.

You should also use a FFT algorithem, and not a DFT algortihm, as FFT is much faster. There are lots of examples on this very site:
http://www.codeproject.com/search.aspx?q=FFT&sbo=kw[^]


//将两个字节转换为-1到1范围内的一个双倍

static double bytesToDouble(byte firstByte,byte secondByte){

//将两个字节转换为一个short(little endian)

short s =(secondByte<< 8 )| firstByte;

//转换范围从-1到(略低于)1

返回s / 32768.0;

}



//在你的实际功能中调用时,你可以使用:

data [i] = bytesToDouble(wav [pos],wav [pos + 1] ]); //数据类型为double
// convert two bytes to one double in the range -1 to 1
static double bytesToDouble(byte firstByte, byte secondByte) {
// convert two bytes to one short (little endian)
short s = (secondByte << 8) | firstByte;
// convert to range from -1 to (just below) 1
return s / 32768.0;
}

//while calling in your actual function, you can use:
data[i] = bytesToDouble(wav[pos], wav[pos + 1]); // data is of type double


这篇关于将作为导入的WAV文件的字节数组转换为双数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆