您好,我需要計算文件的階m的熵,其中m是位數(m < = 16)。Stream of short []
所以:
H_m(X)= - sum_i = 0到i = 2^m-1個{(P_I,M)(log_2(P_I,M))}
所以,我想創建一個輸入流來讀取文件,然後計算每個由m位組成的序列的概率。
對於m = 8,這很容易,因爲我考慮了一個字節。 由於這個m < = 16我試圖考慮爲原始類型short,將short的每個short存儲在數組short []中,然後使用按位運算符處理位以獲取文件中所有m位的序列。 這是個好主意嗎?
無論如何,我無法創建一個短的流。這是我所做的:
public static void main(String[] args) {
readFile(FILE_NAME_INPUT);
}
public static void readFile(String filename) {
short[] buffer = null;
File a_file = new File(filename);
try {
File file = new File(filename);
FileInputStream fis = new FileInputStream(filename);
DataInputStream dis = new DataInputStream(fis);
int length = (int)file.length()/2;
buffer = new short[length];
int count = 0;
while(dis.available() > 0 && count < length) {
buffer[count] = dis.readShort();
count++;
}
System.out.println("length=" + length);
System.out.println("count=" + count);
for(int i = 0; i < buffer.length; i++) {
System.out.println("buffer[" + i + "]: " + buffer[i]);
}
fis.close();
}
catch(EOFException eof) {
System.out.println("EOFException: " + eof);
}
catch(FileNotFoundException fe) {
System.out.println("FileNotFoundException: " + fe);
}
catch(IOException ioe) {
System.out.println("IOException: " + ioe);
}
}
但我失去了一個字節,我不認爲這是程序的最佳途徑。
這是我想使用位運算符的事:
int[] list = new int[l];
foreach n in buffer {
for(int i = 16 - m; i > 0; i-m) {
list.add((n >> i) & 2^m-1);
}
}
我假設在這種情況下使用短褲。 如果我使用字節,我怎麼能做一個類似於m> 8的循環? 該週期不起作用,因爲我必須連接多個字節,並且每次都要改變要連接的位數。
任何想法? 感謝
如果您只是計算總和,爲什麼要將每個值保存在一個數組中? – VGR
感謝您的回覆。我需要將值保存在數組中,因爲我需要獲取m位的所有子序列,然後計算每個序列的概率。 – lon