gpt4 book ai didi

java - 我应该从 getFft 看到什么样的输出?

转载 作者:太空狗 更新时间:2023-10-29 15:57:34 27 4
gpt4 key购买 nike

好的,我正在努力创建一个 Android 音频可视化应用程序。问题是,我从方法 getFft() 得到的结果与谷歌所说的它应该产生的结果不一致。我一直追溯到 C++ 的源代码,但我对 C++ 或 FFT 不够熟悉,无法真正理解发生了什么。

我会尝试在此处包含所需的一切:

(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 .小程序“Start Function FFT”显示系数的图形表示,它是当我从 getFft() 绘制数据时发生的情况的随地吐痰图像。新问题:这就是我的数据吗?如果是这样,我该如何从系数进行频谱分析?

最佳答案

FFT 不仅产生幅度;它也产生相位(每个样本的输出是一个复数)。如果你想要幅度,那么你需要为每个输出样本显式计算它,如 re*re + im*im,其中 reim 分别是每个复数的实部和虚部。

不幸的是,我在您的代码中看不到您正在处理复数的任何地方,因此可能需要进行一些重写。

更新

如果我不得不猜测(在浏览了代码之后),我会说实数分量在偶数索引处,奇数分量在奇数索引处。因此,要获得震级,您需要执行以下操作:

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];
}

关于java - 我应该从 getFft 看到什么样的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4779287/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com