Всем, кто может помочь, помогите пожалуйста.... Тема такая, не выходит посчитать амплитуду сигнала.. Не знаю в чем ошибка. Может ошибка в комплексных числах..
Код:
public static void freq() {
try {
int chunkSize = 4;
byte audio [] = out.toByteArray();
int ts = audio.length;
int ap= ts/chunkSize;
Complex [][] results = new Complex[ap][];
FileWriter lg = new FileWriter("lenght",true);
lg.write("Длина аудиопотока"+ " "+ ts +"\n");
lg.close();
FileWriter qn = new FileWriter("quantity",true);
qn.write("Количество блоков"+ " "+ ap +"\n");
qn.close();
for (int times = 0;times< ap; times++) {
Complex[] x = new Complex[chunkSize];
for(int i = 0; i<chunkSize; i++) {
x[i] = new Complex(audio[(times*chunkSize+i)], 0);
}
results[times] = FFT.fft(x);
}
for (int i = 0; i<ap; i++) {
System.out.println(results[i] +"\n");
}
int [ ] s = new int [ap];
try {
for (int t = 0; t < results.length; t++) {
for (int j = 0; j < results.length; j++) {
double max = 0;
for (int freq = 100; freq < 2000 ; freq++) {
double mag = Math.log(results[t][freq].abs() );
if (max< mag) {
max = mag;
}
}
s[j] = (int) max;
System.out.println(s[j]);
}
}
} catch(Exception e) {System.out.println("Ошибка в поиске максимальных амплитуд");}
} catch(Exception e) {System.out.println("Ошибка в получении частотных характеристик");}
}
Класс complex
public class Complex {
private final double re;
private final double im;
public Complex(double real, double imag)
{
re = real;
im = imag;
}
public String toString()
{
if (im == 0) return re + "";
if (re == 0) return im + "i";
if (im < 0) return re + " - " + (-im) + "i";
return re + " + " + im + "i";
}
public double abs()
{ return Math.hypot(re, im); }
public double phase()
{ return Math.atan2(im, re); }
public Complex sqrt()
{
Complex a = this;
double r = Math.sqrt(this.abs());
double theta = this.phase()/2;
return new Complex(r*Math.cos(theta), r*Math.sin(theta));
}
public Complex log()
{
Complex a = this;
double x = Math.log(this.abs());
double y = this.phase();
return new Complex(x, y);
}
public Complex plus(Complex b)
{
Complex a = this;
double real = a.re + b.re;
double imag = a.im + b.im;
return new Complex(real, imag);
}
public Complex minus(Complex b)
{
Complex a = this;
double real = a.re - b.re;
double imag = a.im - b.im;
return new Complex(real, imag);
}
public Complex times(Complex b)
{
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
return new Complex(real, imag);
}
public Complex times(double alpha)
{ return new Complex(alpha * re, alpha * im); }
public Complex conjugate() { return new Complex(re, -im); }
public Complex reciprocal()
{
double scale = re*re + im*im;
return new Complex(re / scale, -im / scale);
}
public double re() { return re; }
public double im() { return im; }
public Complex divides(Complex b)
{
Complex a = this;
return a.times(b.reciprocal());
}
public Complex exp()
{ return new Complex(Math.exp(re) * Math.cos(im),
Math.exp(re) * Math.sin(im)); }
public Complex sin()
{ return new Complex(Math.sin(re) * Math.cosh(im),
Math.cos(re) * Math.sinh(im)); }
public Complex cos()
{ return new Complex(Math.cos(re) * Math.cosh(im),
-Math.sin(re) * Math.sinh(im)); }
public Complex tan()
{ return sin().divides(cos()); }
public static Complex plus(Complex a, Complex b)
{
double real = a.re + b.re;
double imag = a.im + b.im;
Complex sum = new Complex(real, imag);
return sum;
}
public static Complex neg(Complex z)
{
return new Complex(-z.re(), -z.im());
}
}