好的,所以我正在創建一個Android音頻可視化應用程序。問題是,我從getFft()方法得到的結果並沒有與Google說它應該產生的結果有關。我將源代碼追溯到C++,但我對C++或FFT不夠熟悉,無法真正瞭解發生了什麼。我應該從getFft看到什麼樣的輸出?
我會努力的,包括在這裏所需要的一切:
(Java) Visualizer.getFft(byte[] fft)
/**
* Returns a frequency capture of currently playing audio content. The capture is a 8-bit
* magnitude FFT. Note that the size of the FFT is half of the specified capture size but both
* sides of the spectrum are returned yielding in a number of bytes equal to the capture size.
* {@see #getCaptureSize()}.
* <p>This method must be called when the Visualizer is enabled.
* @param fft array of bytes where the FFT should be returned
* @return {@link #SUCCESS} in case of success,
* {@link #ERROR_NO_MEMORY}, {@link #ERROR_INVALID_OPERATION} or {@link #ERROR_DEAD_OBJECT}
* in case of failure.
* @throws IllegalStateException
*/
public int getFft(byte[] fft)
throws IllegalStateException {
synchronized (mStateLock) {
if (mState != STATE_ENABLED) {
throw(new IllegalStateException("getFft() called in wrong state: "+mState));
}
return native_getFft(fft);
}
}
(C++) Visualizer.getFft(uint8_t *fft)
status_t Visualizer::getFft(uint8_t *fft)
{
if (fft == NULL) {
return BAD_VALUE;
}
if (mCaptureSize == 0) {
return NO_INIT;
}
status_t status = NO_ERROR;
if (mEnabled) {
uint8_t buf[mCaptureSize];
status = getWaveForm(buf);
if (status == NO_ERROR) {
status = doFft(fft, buf);
}
} else {
memset(fft, 0, mCaptureSize);
}
return status;
}
(C++) Visualizer.doFft(uint8_t *fft, uint8_t *waveform)
status_t Visualizer::doFft(uint8_t *fft, uint8_t *waveform)
{
int32_t workspace[mCaptureSize >> 1];
int32_t nonzero = 0;
for (uint32_t i = 0; i < mCaptureSize; i += 2) {
workspace[i >> 1] = (waveform[i]^0x80) << 23;
workspace[i >> 1] |= (waveform[i + 1]^0x80) << 7;
nonzero |= workspace[i >> 1];
}
if (nonzero) {
fixed_fft_real(mCaptureSize >> 1, workspace);
}
for (uint32_t i = 0; i < mCaptureSize; i += 2) {
fft[i] = workspace[i >> 1] >> 23;
fft[i + 1] = workspace[i >> 1] >> 7;
}
return NO_ERROR;
}
(C++) fixedfft.fixed_fft_real(int n, int32_t *v)
void fixed_fft_real(int n, int32_t *v)
{
int scale = LOG_FFT_SIZE, m = n >> 1, i;
fixed_fft(n, v);
for (i = 1; i <= n; i <<= 1, --scale);
v[0] = mult(~v[0], 0x80008000);
v[m] = half(v[m]);
for (i = 1; i <n>> 1; ++i) {
int32_t x = half(v[i]);
int32_t z = half(v[n - i]);
int32_t y = z - (x^0xFFFF);
x = half(x + (z^0xFFFF));
y = mult(y, twiddle[i << scale]);
v[i] = x - y;
v[n - i] = (x + y)^0xFFFF;
}
}
(C++) fixedfft.fixed_fft(int n, int32_t *v)
void fixed_fft(int n, int32_t *v)
{
int scale = LOG_FFT_SIZE, i, p, r;
for (r = 0, i = 1; i < n; ++i) {
for (p = n; !(p & r); p >>= 1, r ^= p);
if (i < r) {
int32_t t = v[i];
v[i] = v[r];
v[r] = t;
}
}
for (p = 1; p < n; p <<= 1) {
--scale;
for (i = 0; i < n; i += p << 1) {
int32_t x = half(v[i]);
int32_t y = half(v[i + p]);
v[i] = x + y;
v[i + p] = x - y;
}
for (r = 1; r < p; ++r) {
int32_t w = MAX_FFT_SIZE/4 - (r << scale);
i = w >> 31;
w = twiddle[(w^i) - i]^(i << 16);
for (i = r; i < n; i += p << 1) {
int32_t x = half(v[i]);
int32_t y = mult(w, v[i + p]);
v[i] = x - y;
v[i + p] = x + y;
}
}
}
}
如果你通過所有的成功了,你真棒!所以我的問題是,當我調用java方法getFft()時,我最終得到的是負值,如果返回的數組意味着代表幅度,則該值不應存在。所以我的問題是,我需要做些什麼來使陣列代表幅度?
編輯:看來我的數據實際上可能是傅立葉係數。我在網上搜索,發現this。小程序「啓動函數FFT」顯示係數的圖形表示,它是當我繪製來自getFft()的數據時發生的事情的隨機圖像。所以新的問題:這是我的數據是什麼?如果是的話,我怎樣才能從係數到頻譜分析呢?
你不是已經得到了答案,這在這裏? http://stackoverflow.com/questions/4720512/android-2-3-visualizer-trouble-understanding-getfft – 2011-01-24 22:22:39
這是一個不同的問題。原來是這樣的:首先,「頻譜的兩邊」是什麼意思?這個輸出與標準FFT有何不同?這次我有相關的源代碼和一個更具體的問題。 – ebolyen 2011-01-24 22:26:11
@Evan,我面臨同樣的問題。當我在畫布上繪製fft數據時,它看上去有線。你有這個解決方案嗎?這是我在SO上發佈的問題。 http://stackoverflow.com/questions/7024187/fft-data-displayed-on-line-graph-not-showing-smoothly – 2011-08-13 10:49:38