2011-01-24 58 views
1

好的,所以我正在創建一個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()的數據時發生的事情的隨機圖像。所以新的問題:這是我的數據是什麼?如果是的話,我怎樣才能從係數到頻譜分析呢?

+0

你不是已經得到了答案,這在這裏? http://stackoverflow.com/questions/4720512/android-2-3-visualizer-trouble-understanding-getfft – 2011-01-24 22:22:39

+0

這是一個不同的問題。原來是這樣的:首先,「頻譜的兩邊」是什麼意思?這個輸出與標準FFT有何不同?這次我有相關的源代碼和一個更具體的問題。 – ebolyen 2011-01-24 22:26:11

+0

@Evan,我面臨同樣的問題。當我在畫布上繪製fft數據時,它看上去有線。你有這個解決方案嗎?這是我在SO上發佈的問題。 http://stackoverflow.com/questions/7024187/fft-data-displayed-on-line-graph-not-showing-smoothly – 2011-08-13 10:49:38

回答

4

的FFT不只是產生幅度;它也會產生相位(每個樣本的輸出都是一個複數)。如果你需要幅度,那麼你需要明確地計算每個輸出樣本,如re*re + im*im,其中reim分別是每個複數的實部和虛部。

不幸的是,我不能在代碼中看到處理複雜數字的任何地方,因此可能需要重寫。

UPDATE

如果我猜的話(在代碼後一眼),我會說,真正的成分是在偶數索引和奇數組分在奇數索引。因此,要獲得大小,你需要做這樣的事情:

uint32_t mag[N/2]; 
for (int i = 0; i < N/2; i++) 
{ 
    mag[i] = fft[2*i]*fft[2*i] + fft[2*i+1]*fft[2*i+1]; 
} 
3

一個可能的解釋,爲什麼你看到負值:byte在Java中籤數據類型。所有值大於或等於1000 0000 被解釋爲負整數。

如果我們知道,所有值應該預計在範圍[0..255],那麼我們有值映射到一個更大的類型和過濾位:

byte signedByte = 0xff; // = -1 
short unsignedByte = ((short) signedByte) & 0xff; // = 255 
1

「的拍攝是一個8位的幅度FFT」可能意味着,返回值有一個8位的大小,而不是它們的幅度他們自己。

根據Jason

對於實值信號,如 你在音頻處理具有偏多, 負頻率輸出將是正 頻率的 鏡像。

Android 2.3 Visualizer - Trouble understanding getFft()

相關問題