频率不计算FFT后正确

频率不计算FFT后正确

本文介绍了Android的 - 频率不计算FFT后正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始记录从AudioRecord声音与一些参数。

I start record a sound from AudioRecord with some parameters.

private static final int RECORDER_SAMPLERATE = 44100;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
this.recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS,
            RECORDER_AUDIO_ENCODING, this.bufferSize);

我的数据存储到一个ArrayList<字节[]> soundRecord;

I store the data into an ArrayList< byte[] > soundRecord;

while (this.isRecording) {
    read = this.recorder.read(data, 0, this.bufferSize);
    if (AudioRecord.ERROR_INVALID_OPERATION != read) {
        try {
            soundRecord.add(data);
            os.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是我对我的计算声响成一系列复杂的FFT方法。做这件事之前,我转换成字节双。

This is my method for compute my sound into a complex array FFT. Before doing it, I convert byte into double.

public ArrayList<double[]> computeFFT(byte[] audioSignal) {
    final int fftPoint = 65536;
    ArrayList<double[]> result;

    double tmp;
    Complex[] y;
    Complex[] complexAudioSignal = new Complex[fftPoint];
    double[] magnitude = new double[fftPoint / 2];

    for (int i = 0; i < fftPoint; i++) {
        if (i >= 22050) tmp = 0;
        else tmp = (double) ((audioSignal[2 * i] & 0xFF) | (audioSignal[2 * i + 1] << 8) / 32768.0F;
        complexAudioSignal[i] = new Complex(tmp, 0.0);
    }

    y = FFF.fft(complexAudioSignal);

    // Calculate magnitude values from fft[]
    for (int i = 0; (fftPoint / 2); i++) {
        magnitude[i] = y[i].abs();
    }

    // get only values over mid value
    result = showFrequencies(magnitude, fftPoint);
    return result;
}

我的问题是,我测试一个恒定的声音440Hz的(A4从钢琴)。
我得到了我的数组幅度一峰,但对于这个峰值频率为220Hz。
每次我试着只用一个频率示例声音(我用正轨),频率是实际频率/ 2。

My problem is, I test a constant sound with 440Hz (A4 from the piano).I get in my array "magnitude" a peak, but the frequency for this peak is 220Hz.Everytime I try a sample sound with only one frequency (I use n-track), the frequency is the real frequency / 2.

我知道频率为f =指数* SAMPLE_RATE / fftPoint。

I know the frequency is f = index * SAMPLE_RATE / fftPoint.

有人可以帮我请。我有这个问题,已有3天... ...

Someone can help me please. I have this problem since 3 days...

编辑一:

修改我的转换回路喜欢你的建议。它看起来像:

I modify my conversion loop like you suggest. It's look like that :

int bytePerSample =2;
int j = 0;
for (int i = 0; i < audioSignal.length / 2; i++) {
    if (( i % 8 == 0 || i % 8 == 2 ) && i < 22050) {
        tmp = (double) ((audioSignal[bytesPerSample * i] & 0xFF) | audioSignal[bytePerSample * i + 1] << 8)) / 32768.0F;
        complexAudioSignal[j] = new Complex(tmp, 0.0);
        j++;
    }
}

现在的问题是FrequencyIWant = FreqIObtain / 2。
我真的不知道现在该做什么。(

The problem right now is FrequencyIWant = FreqIObtain / 2.I really don't know what to do now :'(.

推荐答案

您正在复制两个立体声声道到您的FFT,而不只是左或右声道。尝试使用每4个字节的2连续字节在整数拆包和转换循环。

You are copying both stereo channels into your FFT, instead of just the left or right channel. Try using 2 sequential bytes out of every 4 bytes in your integer unpacking and conversion loop.

这篇关于Android的 - 频率不计算FFT后正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:59