B
beat
Hi - I am a new to DSP and in need of some clarification regarding the
following FFT implemented in java. What I am mainly trying to do is
compute the logarithmic amplitude based on the output from the FFT
using the following 10 * Math.log(FFT input).
If I understand this correctly the following section of code represents
the computation of the magnitude of the signal - but does it?:
mag[0] = (float) ((Math.sqrt(xre[0]*xre[0] +
xim[0]*xim[0]))/n);
for (int i = 1; i < n/2; i++)
mag= 2 * ((float)(Math.sqrt(xre*xre +
xim*xim))/n);
return mag;
and if I would want to compute the log amplitude of the signal would I
not just do:
mag[0] = 10 * (float)Math.log( ((Math.sqrt(xre[0]*xre[0] +
xim[0]*xim[0]))/n));
Thanks in advance for any input on this topic
angelo
< start Java FFT>
private int bitrev(int j)
{
int j2;
int j1 = j;
int k = 0;
for (int i = 1; i <= nu; i++)
{
j2 = j1/2;
k = 2*k + j1 - 2*j2;
j1 = j2;
}
return k;
}
public final float[] doFFT(float[] signal)
{
// assume n is a power of 2
n = signal.length; // 4096
nu = (int)(Math.log(n)/ Math.log(2));
int n2 = n/2;
int nu1 = nu - 1;
xre = new float[n];
xim = new float[n];
mag = new float[n2];
float tr, ti, p, arg, c, s;
for (int i = 0; i < n; i++)
{
xre = signal;
xim = 0.0f;
}
int k = 0;
for (int l = 1; l <= nu; l++)
{
while (k < n)
{
for (int i = 1; i <= n2; i++)
{
p = bitrev (k >> nu1);
arg = 2 * (float) Math.PI * p / n;
c = (float) Math.cos (arg);
s = (float) Math.sin (arg);
tr = xre[k+n2]*c + xim[k+n2]*s;
ti = xim[k+n2]*c - xre[k+n2]*s;
xre[k+n2] = xre[k] - tr;
xim[k+n2] = xim[k] - ti;
xre[k] += tr;
xim[k] += ti;
k++;
}
k += n2;
}
k = 0;
nu1--;
n2 = n2/2;
}
k = 0;
int r;
while (k < n)
{
r = bitrev (k);
if (r > k) {
tr = xre[k];
ti = xim[k];
xre[k] = xre[r];
xim[k] = xim[r];
xre[r] = tr;
xim[r] = ti;
}
k++;
}
mag[0] = (float) ((Math.sqrt(xre[0]*xre[0] +
xim[0]*xim[0]))/n);
for (int i = 1; i < n/2; i++)
mag= 2 * ((float)(Math.sqrt(xre*xre +
xim*xim))/n);
return mag;
}
}
following FFT implemented in java. What I am mainly trying to do is
compute the logarithmic amplitude based on the output from the FFT
using the following 10 * Math.log(FFT input).
If I understand this correctly the following section of code represents
the computation of the magnitude of the signal - but does it?:
mag[0] = (float) ((Math.sqrt(xre[0]*xre[0] +
xim[0]*xim[0]))/n);
for (int i = 1; i < n/2; i++)
mag= 2 * ((float)(Math.sqrt(xre*xre +
xim*xim))/n);
return mag;
and if I would want to compute the log amplitude of the signal would I
not just do:
mag[0] = 10 * (float)Math.log( ((Math.sqrt(xre[0]*xre[0] +
xim[0]*xim[0]))/n));
Thanks in advance for any input on this topic
angelo
< start Java FFT>
private int bitrev(int j)
{
int j2;
int j1 = j;
int k = 0;
for (int i = 1; i <= nu; i++)
{
j2 = j1/2;
k = 2*k + j1 - 2*j2;
j1 = j2;
}
return k;
}
public final float[] doFFT(float[] signal)
{
// assume n is a power of 2
n = signal.length; // 4096
nu = (int)(Math.log(n)/ Math.log(2));
int n2 = n/2;
int nu1 = nu - 1;
xre = new float[n];
xim = new float[n];
mag = new float[n2];
float tr, ti, p, arg, c, s;
for (int i = 0; i < n; i++)
{
xre = signal;
xim = 0.0f;
}
int k = 0;
for (int l = 1; l <= nu; l++)
{
while (k < n)
{
for (int i = 1; i <= n2; i++)
{
p = bitrev (k >> nu1);
arg = 2 * (float) Math.PI * p / n;
c = (float) Math.cos (arg);
s = (float) Math.sin (arg);
tr = xre[k+n2]*c + xim[k+n2]*s;
ti = xim[k+n2]*c - xre[k+n2]*s;
xre[k+n2] = xre[k] - tr;
xim[k+n2] = xim[k] - ti;
xre[k] += tr;
xim[k] += ti;
k++;
}
k += n2;
}
k = 0;
nu1--;
n2 = n2/2;
}
k = 0;
int r;
while (k < n)
{
r = bitrev (k);
if (r > k) {
tr = xre[k];
ti = xim[k];
xre[k] = xre[r];
xim[k] = xim[r];
xre[r] = tr;
xim[r] = ti;
}
k++;
}
mag[0] = (float) ((Math.sqrt(xre[0]*xre[0] +
xim[0]*xim[0]))/n);
for (int i = 1; i < n/2; i++)
mag= 2 * ((float)(Math.sqrt(xre*xre +
xim*xim))/n);
return mag;
}
}