0

我写了这个类,来获取音频数据。我想使用音频输入来采样实时射频信号。我以 44kHz 的频率进行采样,并且我希望通过测量总采集样本来了解经过的时间,知道采样频率。

我不知道为什么我发现 system.nanoTime 测量的经过时间和获取的样本除以频率之间存在增量时间。为什么每次我开始/停止采集时这个大约 170 毫秒的增量都会发生变化?我是否会从采集的信号中丢失样本?

基本上,我所做的是调用这个类并将started布尔值设置为true,然后几秒钟后我将此布尔值设置为false,然后该类退出 while 循环,然后我测量经过的时间并提取增量。

这是我的测试代码:

 public class RecordAudio extends AsyncTask<Void, Long, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {

        try {
            int bufferSize = AudioRecord.getMinBufferSize(frequency, 
                    channelConfiguration, audioEncoding); 

            AudioRecord audioRecord = new AudioRecord( 
                    MediaRecorder.AudioSource.MIC, frequency, 
                    channelConfiguration, audioEncoding, bufferSize); 

            short[] buffer = new short[blockSize];
            double[] toTransform = new double[blockSize];

            audioRecord.startRecording();

            // started = true; hopes this should true before calling
            // following while loop
            double aquiredSignalLen=0;
            long elapsedTime = System.nanoTime();

            while (started) {
                int bufferReadResult = audioRecord.read(buffer, 0,blockSize);

                double tmpElTime1=(double)bufferReadResult/(double)44000;
                aquiredSignalLen=aquiredSignalLen+tmpElTime1;
            }

            //when i stop the acquisition, i calculate the elapsed time,
            //and i compare the result with the elapsed time measured counting
            //the total number of samples

            elapsedTime = System.nanoTime() - elapsedTime;
            double elapsedTimeDouble=(double)elapsedTime/1000000000;
            double delta=elapsedTimeDouble-aquiredSignalLen;
            audioRecord.stop();


        } catch (Throwable t) {
            t.printStackTrace();
            Log.e("AudioRecord", "Recording Failed");
        }
        return null;
    }

我问了这个问题,以解决这个问题:我需要计算在麦克风输入上接收到的 2 个特定信号波形之间的精确经过时间。我希望至少有 1 毫秒的精度,如果可以实现更高的精度更好。这段代码只是一个开始测试。可以计算我可以达到高精度的样本吗?我担心我会因为处理时间而丢失一些样品?

4

1 回答 1

0

Depending on how the hardware of your device is set up, you may be measuring time using two asynchronous clocks.

The audio codec is quite possibly using its own local oscillator as the word-clock for sampling audio and will be delivering samples at this rate. Meanwhile nanoTime() is derived from the CPU clock. Neither is likely to be a hugely accurate timing reference.

于 2013-10-28T23:48:48.373 回答