使用 Jtransform 的 DoubleFFT_1D() for android 计算 DFT
我一直在到处寻找一种可靠的方法来计算 android SDK 中本机函数接收到的音频字节流的 FFT(通过 eclipse IDE)。我遇到过 libgdx fft 和 Jtransform。 Jtransform 在这里找到 JTransform 。 我已将它们全部下载,并将 .jar 文件添加到项目根目录中创建的 libs 文件夹中。然后,我通过项目属性 > 将项目链接到新的 .jar 文件。 java构建路径>图书馆。
我的 src java 文件看起来像这样尝试使用 Jtransform。
package com.spectrum;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View;
import com.badlogic.gdx.audio.analysis.*;
import edu.emory.mathcs.jtransforms.fft.*;
import edu.emory.mathcs.utils.*;
public class spectrum extends Activity {
static byte [] sin = null;
static int f = 2000;
static int fs = 44100;
double buf;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initialize(new SpectrumDesktop(), false);
sin = playsin(f,fs);
buf = bytearray2double(sin);
public DoubleFFT_1D(512); //<Undefined>
public void complexForward(sin) //<Undefined>
playsound(sin);
}
public static double bytearray2double(byte[] b) {
ByteBuffer buf = ByteBuffer.wrap(b);
return buf.getDouble();
}
private void playsound(byte[] sin2){
int intSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);
at.play();
at.write(sin2, 0, sin2.length );
at.stop();
at.release();
}
@Override
protected void onDestroy() {
super.onDestroy();
// TODO Auto-generated method stub
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
public native byte[] playsin(int f,int fs);
/** Load jni .so on initialisation */
static {
System.loadLibrary("SPL");
}
}
在此示例中,我仅使用 Jtransform 包,但我在 lingdx 包中遇到了相同的编译错误。编译器表示 DoubleFFT_1D 和 complexForward 未定义。 所以我缺少一些东西,比如没有正确链接我的库,我不确定。 任何帮助将不胜感激。我是否打算在 onCreate 或其他内容之前声明 DoubleFFT_1D 和 complexForward 的实例?
我知道这是一个菜鸟问题,但我对面向对象语言和随时随地学习 java 很陌生。谢谢 :)
I have been searching everywhere to find a reliable method to calculate the FFT of an audio byte stream received by a native function in android SDK (through eclipse IDE). I have come across the libgdx fft and Jtransform. Jtransform Found here
JTransform
.
I have downloaded them all and added the .jar files to a created libs folder in the root directory for the project. I have then linked the project to the new .jar files through project properties > java Build Path > Libraries.
My src java file looks like this attempting to use Jtransform.
package com.spectrum;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.view.View;
import com.badlogic.gdx.audio.analysis.*;
import edu.emory.mathcs.jtransforms.fft.*;
import edu.emory.mathcs.utils.*;
public class spectrum extends Activity {
static byte [] sin = null;
static int f = 2000;
static int fs = 44100;
double buf;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initialize(new SpectrumDesktop(), false);
sin = playsin(f,fs);
buf = bytearray2double(sin);
public DoubleFFT_1D(512); //<Undefined>
public void complexForward(sin) //<Undefined>
playsound(sin);
}
public static double bytearray2double(byte[] b) {
ByteBuffer buf = ByteBuffer.wrap(b);
return buf.getDouble();
}
private void playsound(byte[] sin2){
int intSize = android.media.AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_CONFIGURATION_STEREO,
AudioFormat.ENCODING_PCM_16BIT, intSize, AudioTrack.MODE_STREAM);
at.play();
at.write(sin2, 0, sin2.length );
at.stop();
at.release();
}
@Override
protected void onDestroy() {
super.onDestroy();
// TODO Auto-generated method stub
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
public native byte[] playsin(int f,int fs);
/** Load jni .so on initialisation */
static {
System.loadLibrary("SPL");
}
}
In this example I am only using the Jtransform packages, however I have been getting the same compile error for the lingdx packages. The compiler says that DoubleFFT_1D and complexForward are undefined.
So there is something I am missing, like not linking my libraries correctly, I am not sure.
Any help would be greatly appreciated. Am I meant to declare an instance of DoubleFFT_1D and complexForward before onCreate or something?
I know this is a noob question, but I am new to object oriented languages and learning java on the go. Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您首先需要创建一个傅里叶变换对象,
其中 n 是您想要的变换的大小。 n 可能必须比您想象的大 2 倍,因为它期望实部和虚部并排在同一输入矩阵中。
然后您可以将您的方法应用于 fft,例如,
奇怪的是结果被保存到输入数组中。
You first need to create a Fourier Transform object
Where n is the size of the transform you want. n may have to be 2 times bigger than you think since it expects real and imaginary parts side by side in the same input matrix.
Then you can apply your methods to fft, eg
Strangely the result is saved into the input array.
1.在项目属性-> Java构建路径->订购并导出,检查要包含在项目类文件中的所有添加的依赖项。
2.选择Android工具>修复项目属性
而不是运行您的应用程序
1.In Project properties -> Java build path -> Order and export, check all your added dependencies to be included with project class files.
2.Select Android Tools > Fix Project Properties
Than run your app