Witam mam taki kod
package com.example.student.myapplication;
import android.graphics.Color;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYSeries;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
double f = 1000;
int fs = 8000;
int n_samples = 1024;
// double[] x = new double[n_samples];
//double[] y = new double[n_samples];
double[] x = new double[n_samples];
double[] y = new double[n_samples];
double t;
int N = n_samples / 2;
double[] p = new double[N];
double[] freq = new double[N];
private XYPlot plot;
int channel_config = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int format = AudioFormat.ENCODING_PCM_16BIT;
int sampleSize = 8000;
int bufferSize = AudioRecord.getMinBufferSize(sampleSize, channel_config, format);
AudioRecord audioInput = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleSize, channel_config, format, bufferSize);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i;
for (i = 0; i < 100; i++) {
short[] audioBuffer = new short[n_samples];
audioInput.startRecording();
audioInput.read(audioBuffer, 0, bufferSize);
System.out.print("Częstotliwość: " + audioBuffer[1]);
for (i = 0; i < n_samples; i++) {
double d = (double) (audioBuffer[i] / 32768.0);
x[i] = d;
y[i] = 0;
}
//for (i=0; i<n_samples; i++) {
// t = (double) i / fs;
// x[i] = Math.sin(2 * Math.PI * f * t);
// y[i] = 0;
}
FFT transformata = new FFT(N);
transformata.fft(x, y);
for (i = 0; i < N / 2; i++) {
freq[i] = i * fs / N;
p[i] += (x[i] * x[i]) + (y[i] * y[i]);
System.out.print("Częstotliwość: " + freq[i]);
System.out.println(" Value:" + p[i]);
}
Number[] Xvalues = new Number[freq.length];
for (int j = 0; j < Xvalues.length; j++)
Xvalues[j] = freq[j];
Number[] Yvalues = new Number[p.length];
for (int j = 0; j < Yvalues.length; j++)
Yvalues[j] = p[j];
System.out.println("done");
plot = (XYPlot) findViewById(R.id.plot);
XYSeries s1 = new SimpleXYSeries(Arrays.asList(Xvalues), Arrays.asList(Yvalues), "Series1");
plot.addSeries(s1, new LineAndPointFormatter(Color.GREEN, Color.GREEN, null, null));
}
//}
//}
public class FFT {
int n, m;
// Lookup tables. Only need to recompute when size of FFT changes.
double[] cos;
double[] sin;
public FFT(int n) {
this.n = n;
this.m = (int) (Math.log(n) / Math.log(2));
// Make sure n is a power of 2
if (n != (1 << m))
throw new RuntimeException("FFT length must be power of 2");
// precompute tables
cos = new double[n / 2];
sin = new double[n / 2];
for (int i = 0; i < n / 2; i++) {
cos[i] = Math.cos(-2 * Math.PI * i / n);
sin[i] = Math.sin(-2 * Math.PI * i / n);
}
}
public void fft(double[] x, double[] y) {
int i, j, k, n1, n2, a;
double c, s, t1, t2;
// Bit-reverse
j = 0;
n2 = n / 2;
for (i = 1; i < n - 1; i++) {
n1 = n2;
while (j >= n1) {
j = j - n1;
n1 = n1 / 2;
}
j = j + n1;
if (i < j) {
t1 = x[i];
x[i] = x[j];
x[j] = t1;
t1 = y[i];
y[i] = y[j];
y[j] = t1;
}
}
// FFT
n1 = 0;
n2 = 1;
for (i = 0; i < m; i++) {
n1 = n2;
n2 = n2 + n2;
a = 0;
for (j = 0; j < n1; j++) {
c = cos[a];
s = sin[a];
a += 1 << (m - i - 1);
for (k = j; k < n; k = k + n2) {
t1 = c * x[k + n1] - s * y[k + n1];
t2 = s * x[k + n1] + c * y[k + n1];
x[k + n1] = x[k] - t1;
y[k + n1] = y[k] - t2;
x[k] = x[k] + t1;
y[k] = y[k] + t2;
}
}
}
}
}
}
I mam problem aby ruszył na androidzie, chodzi o to aby odczytał sygnał z mikrofonu.
Jakieś porady ?