如何将 short[] 转换为 double[]?
How can I convert a short[] to double[]?
现在,我正在做一个音频信号处理,我想做一个实时显示我phone录制的音频声波。
这里有个问题,我的音频格式是"ENCODING_PCM_16BIT"。那么如何将16位短数据转换为double格式呢?
这是我的代码,但它不能正常工作。谁能帮我解决这个问题?
try {
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, Sample_rate, Channel, Encording,
Buffersize);
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(MainActivity.file)));
short[] buffer = new short[Buffersize / 2]; //870 double/ 2 = 435 double
System.out.println("The buffer size is " + Buffersize);
timer1();
audioRecord.startRecording(); // Start record
while (MainActivity.isrecord) {
int bufferReadResult = audioRecord.read(buffer, 0, buffer.length);
System.out.println("The buffer size is " + bufferReadResult);
for (int i = 0; i < bufferReadResult/2; i++) {
dos.writeShort(buffer[i]);
**tempraw[i] = (double)buffer[i];**
}
phase = DataProcess(tempraw);
}
audioRecord.stop(); // record stop
audioRecord.release();
audioRecord = null;
dos.close(); // Output stream close
dos = null;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我想把那些数据放在 "short[] buffer" 到 "double[] tempraw"
谢谢!
在网上看了一些代码后,我做了这个。我认为它有效,只是很慢,2ms 一个双
private static double shorttodouble(short[] a, int index) {
// TODO Auto-generated method stub
long l;
l = a[index + 0];
l &= 0xffff;
l |= ((long) a[index + 1] << 16);
l &= 0xffffffffl;
l |= ((long) a[index + 2] << 32);
l &= 0xffffffffffffl;
l |= ((long) a[index + 3] << 48);
l &= 0xffffffffffffffffl;
l |= ((long) a[index + 4] << 64);
return (double)l;
}
就像 int 数组到 double 数组一样,你需要一个循环来完成这项工作:
short[] shorts = {1,2,3,4,5};
double[] doubles = new double[shorts.length];
for (int i = 0; i < shorts.length; i ++) {
doubles[i] = shorts[i];
System.out.println(doubles[i]);
}
低效但有效。
试试这个:---
short[] buffer = new short[size];
double[] transformed = new double[buffer.length];
for (int j=0;j<buffer.length;j++) {
transformed[j] = (double)buffer[j];
}
现在,我正在做一个音频信号处理,我想做一个实时显示我phone录制的音频声波。
这里有个问题,我的音频格式是"ENCODING_PCM_16BIT"。那么如何将16位短数据转换为double格式呢?
这是我的代码,但它不能正常工作。谁能帮我解决这个问题?
try {
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, Sample_rate, Channel, Encording,
Buffersize);
DataOutputStream dos = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(MainActivity.file)));
short[] buffer = new short[Buffersize / 2]; //870 double/ 2 = 435 double
System.out.println("The buffer size is " + Buffersize);
timer1();
audioRecord.startRecording(); // Start record
while (MainActivity.isrecord) {
int bufferReadResult = audioRecord.read(buffer, 0, buffer.length);
System.out.println("The buffer size is " + bufferReadResult);
for (int i = 0; i < bufferReadResult/2; i++) {
dos.writeShort(buffer[i]);
**tempraw[i] = (double)buffer[i];**
}
phase = DataProcess(tempraw);
}
audioRecord.stop(); // record stop
audioRecord.release();
audioRecord = null;
dos.close(); // Output stream close
dos = null;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我想把那些数据放在 "short[] buffer" 到 "double[] tempraw"
谢谢!
在网上看了一些代码后,我做了这个。我认为它有效,只是很慢,2ms 一个双
private static double shorttodouble(short[] a, int index) {
// TODO Auto-generated method stub
long l;
l = a[index + 0];
l &= 0xffff;
l |= ((long) a[index + 1] << 16);
l &= 0xffffffffl;
l |= ((long) a[index + 2] << 32);
l &= 0xffffffffffffl;
l |= ((long) a[index + 3] << 48);
l &= 0xffffffffffffffffl;
l |= ((long) a[index + 4] << 64);
return (double)l;
}
就像 int 数组到 double 数组一样,你需要一个循环来完成这项工作:
short[] shorts = {1,2,3,4,5};
double[] doubles = new double[shorts.length];
for (int i = 0; i < shorts.length; i ++) {
doubles[i] = shorts[i];
System.out.println(doubles[i]);
}
低效但有效。
试试这个:---
short[] buffer = new short[size];
double[] transformed = new double[buffer.length];
for (int j=0;j<buffer.length;j++) {
transformed[j] = (double)buffer[j];
}