【问题标题】:Difference between Android and Matlab (reading a wav file into array )Android 和 Matlab 的区别(将 wav 文件读入数组)
【发布时间】:2014-03-27 11:44:06
【问题描述】:

我正在尝试使用 Android 将 wav 文件读入数组。为了验证结果,我使用 Matlab 读取了相同的 wav 文件。问题是价值观不同。非常感谢您在解决此问题方面的帮助。 请在下面找到带有相关结果的 Matlab 和 Android 代码:

Matlab 代码:

fName = 'C:\Users\me\Desktop\audioText.txt';         
fid = fopen(fName,'w');           
dlmwrite(fName,y_sub,'-append','delimiter','\t','newline','pc');

Matlab 结果:

0.00097656 0.00045776 0.0010681 0.00073242 0.00054932 -0.00064087 0.0010376 -0.00027466 -0.00036621 -9.1553e-05 0.00015259 0.0021362 -0.00024414 -3.0518e-05 -0.00021362

安卓代码:

String filePath;
    private static DataOutputStream fout;
    ByteArrayOutputStream out;
    BufferedInputStream in;

    filePath = "mnt/sdcard/audio.wav";


            out = new ByteArrayOutputStream();
            try {
                in = new BufferedInputStream(new FileInputStream(filePath));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            int read;
            byte[] buff = new byte[2000000];
            try {
                while ((read = in.read(buff)) > 0)
                {
                    out.write(buff, 0, read);
                }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                out.flush();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            byte[] audioBytes = out.toByteArray();

            }

Android 结果:

82、73、70、70、92、108、40、0、87、65、86、69、102、109

谢谢,

【问题讨论】:

  • 你在测试传感器数据吗??
  • wav 文件是传感器数据的记录,但这有关系吗?
  • 不,我没有。你有什么线索吗?
  • 你知道WAV文件是怎么编码的吗? 16 位、24 位等?确保在将 WAV 文件中的值直接打印为文本时使用正确的位宽。此外,请确保使用正确的打印格式。我会说浮点数或十六进制最适合这样的比较。

标签: android arrays matlab wav


【解决方案1】:

在 Android 中,您读取的是文件头,而不是声音样本的实际值。您在 Android 中的值是 ASCII 的

RIFF\l(WAVEfm

在 Matlab 中我不确定你在做什么......看起来你在写,而不是在读文件。

【讨论】:

  • 我在一个文本文件上写,值不是标题信息,因为它们非常大
【解决方案2】:

dir 命令在这里很有帮助。它既可以显示目录的全部内容,也可以指定一个 glob 来仅返回文件的子集,例如dir('*.wav')。这将返回一个包含文件信息的结构数组,例如namedatebytesisdir 等。

要开始使用,请尝试以下操作:

filelist = dir('*.wav');
for file = filelist
    fprintf('Processing %s\n', file.name);
    fid = fopen(file.name);
    % Do something here with your file.
    fclose(fid);
end

如果必须按文件存储处理结果, 我经常使用以下模式。我通常预先分配一个数组、一个结构数组或 与文件列表大小相同的元胞数组。然后我使用整数索引进行迭代 在文件列表上,我也可以用它来写输出。如果信息是 存储是同质的(例如每个文件一个标量),使用数组或结构数组。 但是,如果文件之间的信息不同(例如不同大小的向量或矩阵),请改用元胞数组。

一个使用普通数组的例子:

filelist = dir('*.wav');
% Pre-allocate an array to store some per-file information.
result = zeros(size(filelist));
for index = 1 : length(filelist)
    fprintf('Processing %s\n', filelist(index).name);
    % Read the sample rate Fs and store it.
    [y, Fs] = wavread(filelist(index).name);
    result(index) = Fs;
end
% result(1) .. result(N) contain the sample rates of each file.

使用元胞数组的示例:

filelist = dir('*.wav');
% Pre-allocate a cell array to store some per-file information.
result = cell(size(filelist));
for index = 1 : length(filelist)
    fprintf('Processing %s\n', filelist(index).name);
    % Read the data of the WAV file and store it.
    y = wavread(filelist(index).name);
    result{index} = y;
end
% result{1} .. result{N} contain the data of the WAV files.

【讨论】:

  • 感谢您的努力,也许我没有明白您的意思,但是Matlab和Android之间的值差异的原因是什么?据我了解,您试图通过索引而不是绝对路径进入文件,然后将 wav 文件读入向量。在我发布的代码中,我读取了 wav 文件的值并将它们保存到文本文件中。所以我猜这是同一个概念。
【解决方案3】:

我不确定到底是什么问题,但是当我使用以下代码时,我得到了正确的读数:

File filein = new File(filePath, "audio.wav");


         try
          {
             // Open the wav file specified as the first argument
             WavFile wavFile = WavFile.openWavFile(filein);

             // Display information about the wav file
             wavFile.display();

             // Get the number of audio channels in the wav file
             int numChannels = wavFile.getNumChannels();

             // Create a buffer of 100 frames
             double[] buffer = new double[20000 * numChannels];

             int framesRead;
             double min = Double.MAX_VALUE;
             double max = Double.MIN_VALUE;

             do
             {
                // Read frames into buffer
                framesRead = wavFile.readFrames(buffer, 20000);

                // Loop through frames and look for minimum and maximum value
                for (int s=0 ; s<framesRead * numChannels ; s++)
                {
                   if (buffer[s] > max) max = buffer[s];
                   if (buffer[s] < min) min = buffer[s];
                }
             }
             while (framesRead != 0);

             // Close the wavFile
             wavFile.close();

             // Output the minimum and maximum value
             System.out.printf("Min: %f, Max: %f\n", min, max);
          }
          catch (Exception e)
          {
             System.err.println(e);
          }

【讨论】:

  • 嗨,我遇到了和你一样的问题。也尝试读取我的 .wav 文件的值。我尝试实现上述代码,但 Android 不支持 WavFile。你如何克服这一点?另外,最小值和最大值输出是否指的是谷值和峰值?谢谢!
猜你喜欢
  • 2012-09-01
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 2014-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多