2016-06-06 107 views
2

我有一個原始(音頻)數據文件,需要將其讀入R程序中作爲帶符號的2字節整數。下面的C代碼在將文件讀入unsigned char數組之後成功完成轉換。R將2個字節的原始數據轉換爲整數

我在R遇到困難,可能是因爲 的異常整數大小爲2個字節。在C代碼下面,我寫了我現在在R中的所有內容以及錯誤消息。

#define BYTES_PER_SAMPLE 2 

void samples2floats(unsigned char *audio, float *buff, int n) { 
    int i; 
    for (i=0; i < n; i++) buff[i] = sample2float(audio + i*BYTES_PER_SAMPLE); 
} 

float sample2float(unsigned char *temp) { 
    int i,tot,j,g; 
    unsigned char b[BYTES_PER_SAMPLE],*ptr; 
    float x; 

    tot= 0; 
    ptr = (unsigned char *) &tot; 
    for (j = 0; j < BYTES_PER_SAMPLE; j++) ptr[j] = temp[j]; 
    if (tot & 0x8000) tot |= 0xffff0000; 
    x = tot/(float) 0x10000; 
} 
    return(x); 
} 

R代碼裏面:

#read in data 
maxaudio = 100000 
to.read = file("filename.raw", "rb") 
audio = readBin(to.read, "raw", size = 1, n = maxaudio, signed = FALSE) 
close(to.read) 

audio[493:500] #what the data looks like 
#[1] e9 ff eb ff ef ff ec ff 

audio = sapply(audio,function(x) paste(as.integer(rev(rawToBits(x))),collapse="")) 

audio[493:500] #what the data looks like 
#[1] "11101001" "11111111" "11101011" "11111111" "11101111" "11111111" "11101100" "11111111" 

BinToDec <- function(x) #convert binary to decimal 
    sum(2^(which(rev(unlist(strsplit(as.character(x), "")) == 1))-1)) 

sample2num = function(char.audio) { 
    wave = numeric(0) 
    for (i in 1:length(char.audio)) { 
    p = (i-1)*2 + 1 
    #concatenates two values, e.g. "1110100111111111", converts to int 
    int.val = BinToDec(paste(toString(audio[p]), toString(audio[p+1]), sep = "")) 
    if (bitwAnd(int.val, 0x8000)) int.val = bitwOr(int.val, 0xffff) 
    #had to change 0xffff0000 to 0xffff, or got error Warning message: In  
    #bitwOr(int.val, 4294901760) : NAs introduced by coercion to integer range 
    x = int.val/0x8000 
    if (abs(x) > 1) stop(paste("value outside range", temp[1], temp[2], x)) 
    wave = c(wave, x) 
    } 
    return(wave) 
} 
test = sample2num(audio[5000:50000]) 

#Error in sample2num(audio[5000:50000]) : 
# value outside range 1111111111101001 NA 1.99996948242188 

回答

0

更新:R中讀取所述數據原來是簡單:

audiodata = readBin(name, integer(), 2988032, size = 2)