2013-02-04 75 views
0

我必須將matlab代碼轉換爲Android。此MATLAB代碼包含能量計算如下所示:matlab到android轉換來計算能量

第一I讀取的音頻文件到矩陣X,和取樣頻率爲fs的,然後計算能量爲每個窗口:

[x, fs] = wavread('C:\1359873105438.wav') 
energy=energy+sum(x(1:fs).^2)*Tss; 

我不確定如何將其轉換爲Android/Java。

你以前經歷過這個嗎?請幫我解決這個問題。

在此先感謝您的幫助

+0

重寫它作爲一個循環將是一個很好的起點,因爲我非常懷疑android java將允許matlab的vecotrized形式。至少自己嘗試一下。 – Dan

+0

如何使用循環,請你詳細說明一下嗎? – user1741938

回答

0

從本質上講,你必須做這樣的事情:

double x;//Read Wave in here 
for (i=0;i<x.length;i++) 
{ 
    energy+=Tss*(x[i]^2); 
} 

如何讀取this article借來的波形文件。

public class ReadExample 
{ 
    public static void main(String[] args) 
    { 
     try 
     { 
     // Open the wav file specified as the first argument 
     WavFile wavFile = WavFile.openWavFile(new File(args[0])); 

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

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

     int framesRead; 

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

      // Loop through frames and look for minimum and maximum value 
      for (int s=0 ; s<framesRead * numChannels ; s++) 
      { 
       //This is where you put the your code in 
      } 
     } 
     while (framesRead != 0); 

     // Close the wavFile 
     wavFile.close(); 
     } 
     catch (Exception e) 
     { 
     System.err.println(e); 
     } 
    } 
} 

底線是,有沒有一個很好的乾淨的方式來做到這一點,就像在MATLAB中。甚至沒有一個函數可以直接讀取波形文件。儘管如此,它使用網站提供的WavFile類是相對直接的。

+0

我會嘗試一下,謝謝你的提示 – user1741938