2016-06-06 77 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)