0

在我的 Beat-Detection 中,我使用快速傅立叶变换来检测音频信号中的低音。我正在录制一个基于独奏的鼓,而不是移动声音或音量。随着时间的推移绘制值之后。我得到非常量的值。他们差别很大。也许你知道为什么会发生这种情况?我只能猜测,但也许我没有为 FFT 使用正确的 Buffersize 或 WindowSize?

下一个绘制的图形和源代码

私有类 RecordingThread 扩展 Thread {

    private boolean mShallContinue = true;

    @Override
    public void run() {
// Compute the minimum required audio buffer size and allocate the
    // buffer.
    mBufferSize = 4096;// AudioRecord.getMinBufferSize(SAMPLING_RATE,
                        // //4096;//
                        // AudioFormat.CHANNEL_IN_MONO,

    mAudioBuffer = new short[1024];// [mBufferSize / 2];
    bufferDouble2 = new int[mBufferSize / 2];
    bufferDouble = new int[(blockSize - 1) * 2];
    camera = Camera.open();

}
        AudioRecord record = new AudioRecord(
                MediaRecorder.AudioSource.DEFAULT, SAMPLING_RATE,
                AudioFormat.CHANNEL_IN_MONO,
                AudioFormat.ENCODING_PCM_16BIT, mBufferSize);

        short[] buffer = new short[blockSize];
        double[] audioDataDoubles = new double[(blockSize * 2)];
        double[] re = new double[blockSize];
        double[] im = new double[blockSize];
        double[] magnitude = new double[blockSize];

        // start collecting data
        record.startRecording();

        DoubleFFT_1D fft = new DoubleFFT_1D(blockSize);
        synchronized (this) {
            while (shallContinue()) {

                /** decibels */
                record.read(mAudioBuffer, 0, 1024);
                // updateDecibelLevel();

                /** frequency */
                // /windowing!?
                for (int i = 0; i < mAudioBuffer.length; i++) {
                    bufferDouble2[i] = (int) mAudioBuffer[i];
                }

                for (int i = 0; i < blockSize - 1; i++) {
                    double x = -Math.PI + 2 * i * (Math.PI / blockSize);
                    double winValue = (1 + Math.cos(x)) / 2.0;
                    bufferDouble[i] = (int) (bufferDouble2[i] * winValue);
                }

                int bufferReadResult = record.read(buffer, 0, blockSize);

                // Read in the data from the mic to the array
                for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
                    audioDataDoubles[2 * i] = (double) buffer[i] / 32768.0; // signed
                                                                            // 16
                                                                            // bit
                    audioDataDoubles[(2 * i) + 1] = 0.0;
                }

                // audiodataDoubles now holds data to work with
                fft.complexForward(audioDataDoubles); // complexForward

                for (int i = 0; i < blockSize; i++) {

                    // real is stored in first part of array
                    re[i] = audioDataDoubles[i * 2];
                    // imaginary is stored in the sequential part
                    im[i] = audioDataDoubles[(i * 2) + 1];

                    // magnitude is calculated by the square root of
                    // (imaginary^2 + real^2)
                    magnitude[i] = Math.sqrt((re[i] * re[i])
                            + (im[i] * im[i]));
                }
                magnitude[0] = 0.0;

                magnitude2 = magnitude[2];
                magnitude3 = magnitude[3];
                magnitude4 = magnitude[4];

                updateShortBuffer();
                bufferCount++;
                updateLongBuffer();

                // if (detectedRoomRMS == 200)
                updateFrequency();
                System.out.println(System.currentTimeMillis() + " M2: "
                        + magnitude2 + " M3: " + magnitude3 + " M4: "
                        + magnitude4 + " M5: " + magnitude[5] + " M10: "
                        + magnitude[10] + " M20: " + magnitude[20] + " M24: "
                        + magnitude[24] + " M48: " + magnitude[48] + " LONG20: "
                        + rms_Long_Buffer_five + " LONNG: "
                        + rms_Long_Buffer);
            }
            record.stop(); // stop recording please.
            record.release(); // Destroy the recording, PLEASE!
        }
    }

    /**
     * true if the thread should continue running or false if it should stop
     */
    private synchronized boolean shallContinue() {
        return mShallContinue;
    }

    /**
     * Notifies the thread that it should stop running at the next
     * opportunity.
     */
    private synchronized void stopRunning() {
        mShallContinue = false;
    }

}

// / post the output frequency to TextView
private void updateFrequency() {
    tvfreq.post(new Runnable() {

        String RoomRMS;
        String s;

        public void run() {

            if (RMSMessureDone == false) {
                String l = "..";
                String KK = "...";
                tvfreq.setTextColor(Color.WHITE);
                if ((rmsCounter > 10))
                    tvfreq.setText(KK); //
                else
                    tvfreq.setText(l);
            } else {
                BPM = round(BPM, 1);
                s = Double.toString(BPM);
                s = s + " bpm";
                tvfreq.setTextColor(Color.WHITE);
                tvfreq.setText((s));

                RoomRMS = Double.toString(detectedRoomRMS);
                tvdb.setText(RoomRMS);
            }
        }

    });

}
4

1 回答 1

1

我想您看到的差异与起始点与用于 FFT 的窗口的关系有关。

从根本上说,您使用的方法对于这个问题是错误的:

1:信号的性质:来自低音鼓的信号(我假设您可能是指底鼓?),具有尖锐的开始(它刚刚被重击),并具有快速衰减初始峰值与宽带宽不相干;它本质上是白噪声。虽然那里会有很多低频内容,但它不会占主导地位。初始起音后,鼓皮以其固有频率振动,输出比初始峰值低得多。

2:通过方形窗口查看:您当前正在将方形窗口功能应用于您的样本。这不是一个成功的选择,因为它会将能量溅到你不想要的地方。Hamming 和 Blackman 窗是 FFT 的常见选择。

3:分辨率:使用 FFT 的根本缺陷是它是窗口化的。DFT 的结果只是每个频率区间在窗口周期内的贡献。窗口期限制了您的时间分辨率(您只知道频率范围内的事件发生在窗口中的某处)。另一方面,如果您想从 FFT 的低频区间获得有意义的结果,则奈奎斯特理论适用于相对于测量信号的窗口频率。假设您以 44.1kHz 采样,这意味着如果您想要在 50Hz 时获得有意义的结果,则需要 2048 点 DFT。现在每个窗口的周期为 0.047 秒(或大约 1/20 秒)。这是每个时间测量的误差范围。

有多种时域发病检测算法常用于心跳检测。如果您想检测信号的可能来源,您可能会同时使用频域方法。

于 2015-01-15T22:01:54.607 回答