gpt4 book ai didi

c - KissFFT 正向/反向输出噪音,为什么?

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:08 27 4
gpt4 key购买 nike

我正在尝试在 Java 应用程序中原生使用 KissFFT,但输入信号的正向/反向没有按预期返回:信号幅度几乎不存在。如果我去掉比例因子(除以 2N),结果就是谐波噪声。谁能发现这个错误?

这是前向调用(从 GDX 复制,所以应该没问题!):

JNIEXPORT void JNICALL Java_com_badlogic_gdx_audio_analysis_KissFFT_spectrum(JNIEnv* env, jclass clazz, jlong handle, jshortArray obj_samples, jfloatArray obj_spectrum) {
short* samples = (short*)env->GetPrimitiveArrayCritical(obj_samples, 0);
float* spectrum = (float*)env->GetPrimitiveArrayCritical(obj_spectrum, 0);

KissFFT* fft = (KissFFT*)handle;
kiss_fftr( fft->forwardConfig, (kiss_fft_scalar*)samples, fft->spectrum );

int len = fft->numSamples / 2 + 1;
for( int i = 0; i < len; i++ )
{
float re = scale(fft->spectrum[i].r) * fft->numSamples;
float im = scale(fft->spectrum[i].i) * fft->numSamples;

if( i > 0 )
spectrum[i] = sqrtf(re*re + im*im);
else
spectrum[i] = sqrtf(re*re + im*im);
}

env->ReleasePrimitiveArrayCritical(obj_samples, samples, 0);
env->ReleasePrimitiveArrayCritical(obj_spectrum, spectrum, 0);

}

这是相反的(我写的,可能有错误;)):

JNIEXPORT void JNICALL Java_com_badlogic_gdx_audio_analysis_KissFFT_inverse(JNIEnv* env, jclass clazz, jlong handle, jshortArray obj_samples) {
short* shortSamples = (short*)env->GetPrimitiveArrayCritical(obj_samples, 0);

//@line:108

KissFFT* fft = (KissFFT*)handle;
kiss_fft_cpx out[fft->numSamples];

kiss_fftri( fft->inverseConfig, fft->spectrum, (kiss_fft_scalar*)out );

for (int i=0; i < fft->numSamples; i++) {
shortSamples[i] = (out[i].r) / (fft->numSamples*2);
}

env->ReleasePrimitiveArrayCritical(obj_samples, shortSamples, 0);

}

最佳答案

根据 protectedmember 的要求,这里有一个更完整的示例(无论如何在 native 端)。我最终稍微修改了 libgdx 代码(使用 v.0.9.4)。我还使用浮点而不是固定进行编译,因为后者在反演时引入了量化噪声。这段代码应该对任何一个都有效(因为 kiss_fft_scalar 根据编译器标志适应 short 或 float),但没有用 fixed 重新测试,因为我发现它对 fft->ifft 系列没有用.

我很想知道你是否成功了,请告诉我!

(请注意,此代码遵循 libgdx,是在 Apache 2.0 许可下!)

#include <com.badlogic.gdx.audio.analysis.KissFFT.h>

#include <kissfft/kiss_fftr.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

struct KissFFT
{
kiss_fftr_cfg forwardConfig;
kiss_fftr_cfg inverseConfig;
kiss_fft_cpx* spectrum;
int numSamples;
int spectrumSize;
};

JNIEXPORT jlong JNICALL Java_com_badlogic_gdx_audio_analysis_KissFFT_create(JNIEnv* env, jclass clazz, jint numSamples) {

KissFFT* fft = new KissFFT();
fft->forwardConfig = kiss_fftr_alloc(numSamples,0,NULL,NULL);
fft->inverseConfig = kiss_fftr_alloc(numSamples,1,NULL,NULL);
fft->spectrum = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * numSamples);
fft->numSamples = numSamples;
fft->spectrumSize = numSamples/2+1;
return (jlong)fft;
}

JNIEXPORT void JNICALL Java_com_badlogic_gdx_audio_analysis_KissFFT_destroy(JNIEnv* env, jclass clazz, jlong handle) {

KissFFT* fft = (KissFFT*)handle;
free(fft->forwardConfig);
free(fft->inverseConfig);
free(fft->spectrum);
free(fft);
}

JNIEXPORT void JNICALL Java_com_badlogic_gdx_audio_analysis_KissFFT_spectrum(JNIEnv* env, jclass clazz, jlong handle, jfloatArray obj_samples, jfloatArray obj_mags) {

kiss_fft_scalar* samples = (kiss_fft_scalar*)env->GetPrimitiveArrayCritical(obj_samples, 0);
float* mags = (float*)env->GetPrimitiveArrayCritical(obj_mags, 0);

KissFFT* fft = (KissFFT*) handle;

kiss_fftr(fft->forwardConfig, samples, fft->spectrum);

for(int i=0;i<fft->spectrumSize;i++) {
mags[i] = hypotf(fft->spectrum[i].r,fft->spectrum[i].i);
}

env->ReleasePrimitiveArrayCritical(obj_samples, samples, 0);
env->ReleasePrimitiveArrayCritical(obj_mags, mags, 0);
}

JNIEXPORT void JNICALL Java_com_badlogic_gdx_audio_analysis_KissFFT_inverse(JNIEnv* env, jclass clazz, jlong handle, jfloatArray obj_samples) {
kiss_fft_scalar* samples = (kiss_fft_scalar*)env->GetPrimitiveArrayCritical(obj_samples, 0);

KissFFT* fft = (KissFFT*)handle;

kiss_fftri( fft->inverseConfig, fft->spectrum, samples );

for(int i=0;i<fft->numSamples;i++) {
samples[i] = samples[i] / (float)fft->numSamples;
}

env->ReleasePrimitiveArrayCritical(obj_samples, samples, 0);

}

关于c - KissFFT 正向/反向输出噪音,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10957604/

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