FFT in java

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;
}
}
 
B

Boudewijn Dijkstra

beat said:
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;


It computes an amplitude multiplied by 2/n, for half of the signal, assuming
the lenght of the arrays is n.
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));

You can save the sqrt because of the properties of log:

mag[0] = 5 * (float)Math.log( ((xre[0]*xre[0] + xim[0]*xim[0])/n));

Also consider the multiplication by 2/n in the original calculation, and how
that relates to you proposal for the 10*log amplitude.
 
B

beat

Boudewijn said:
beat said:
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;


It computes an amplitude multiplied by 2/n, for half of the signal, assuming
the lenght of the arrays is n.
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));

You can save the sqrt because of the properties of log:

mag[0] = 5 * (float)Math.log( ((xre[0]*xre[0] + xim[0]*xim[0])/n));

Also consider the multiplication by 2/n in the original calculation, and how
that relates to you proposal for the 10*log amplitude.


Hi Boudewijn -

thanks for the explanation, on the output of the FFT - I have run into
another little
issue with the output of the FFT.

When I compute the total Amplitude for the signal using mag[] array on
a variety of different speech signals - the amplitude is not consistent
with the actual recorded "loudness" in the original signal...

what can I try to get the amplitude representation to be consistent
with the actual loudness of the original signal.

What I need to extract from the signal is how loud/ or softly the
person has spoken - the duration is not that important.

thanks
elio
 
B

Boudewijn Dijkstra

beat said:
thanks for the explanation, on the output of the FFT - I have run into
another little issue with the output of the FFT.

When I compute the total Amplitude for the signal using mag[] array on
a variety of different speech signals - the amplitude is not consistent
with the actual recorded "loudness" in the original signal...

what can I try to get the amplitude representation to be consistent
with the actual loudness of the original signal.

What I need to extract from the signal is how loud/ or softly the
person has spoken - the duration is not that important.

Loudness is a quite subjective measure, largely dependant on the human
auditory system and the speaker setup. However, it is closely related to the
energy of the signal and is quite unrelated to it's amplitude. I suggest you
read about sound-pressure levels, dB's, SPL's, sones and phons.
 
B

beat

right - energy is what I am looking for - did not not how to phrase
it..
Loudness is a quite subjective measure, largely dependant on the human
auditory system and the speaker setup. However, it is closely related to the
energy of the signal and is quite unrelated to it's amplitude. I suggest you
read about sound-pressure levels, dB's, SPL's, sones and phons.

so from the reading that I have done with respect to computing the
energy of the
signal, I have concluded (naively) that the output of the my FFT is
incorrect with respect
to computing energy.

Based on what you said previously:
Loudness is a quite subjective measure, largely dependant on the human
auditory system and the speaker setup. However, it is closely related to the
energy of the signal and is quite unrelated to it's amplitude.

So, the contents of mag[] array could not be used to compute the
accumulate energy of the signal.

If I compute the energy I would need to:
wouldn't I need to change the output of my FFT to return the frequency
for each sample and not
the magnitude?

Would it be correct for me to first compute the FFT of the signal and
return a frequency[] for all the samples in the signal and this would
be acheived by doing the
following:

for (int i=0; i< NUMBER_SAMPLES/2; i++)
{
frequency = (float) (frequency * sampleRate) /
(float)transformLength;
}

and then pipe the frequency[] result to getEnergy() to compute the
energy of the signal?

//signalEnergy = Sigma (|x(n)^2|) from n -> n-1 of the samples in the
Signal.

public float getEnergy()
{
float eSignal, eSignalTemp;
eSignal = 0.0f;
eSignalTemp = 0.0f;

for (int i=0; i< NUMBER_SAMPLES/2; i++)
{
eSignalTemp = (float) (frequency*frequency);
eSignal += eSignalTemp;
}
// eSignal represents the total energy of the signal
return eSignal;
}


thank you for all your help on this - I sometimes wish that university
professors were sometimes
a bit more approachable - As for my professor, I think he is somewhere
between Earth and Mars...

angelo
 
B

Boudewijn Dijkstra

Based on what you said previously:
Loudness is a quite subjective measure, largely dependant on the
human auditory system and the speaker setup. However, it is closely
related to the energy of the signal and is quite unrelated to it's
amplitude.

So, the contents of mag[] array could not be used to compute the
accumulate energy of the signal.

If I compute the energy I would need to:
wouldn't I need to change the output of my FFT to return the frequency
for each sample and not the magnitude?

Samples do not have a frequency. Instead frequencies (or frequency bands)
have samples (consisting of amplitude and phase).
//signalEnergy = Sigma (|x(n)^2|) from n -> n-1 of the samples in the
Signal.

What does x represent?
thank you for all your help on this - I sometimes wish that university
professors were sometimes a bit more approachable - As for my professor, I
think he is somewhere between Earth and Mars...

Indeed, some of them seem to have forgotten that there is a difference between
teaching and divulging a truckload of (loosely related) facts.
 
B

beat

What does x represent?

with respect to the above formula to compute the signalEnergy - x
would represent
the byte->float representation of the signal prior to entering the fft.

another question about computing the signal energy:
do I compute the signal energy prior to transforming the signal via
fft...

or would I perform the fft on the signal and then compute signalEnergy
based on the output of the fft
 
B

Boudewijn Dijkstra

beat said:
with respect to the above formula to compute the signalEnergy - x
would represent the byte->float representation of the signal prior to
entering the fft.

another question about computing the signal energy:
do I compute the signal energy prior to transforming the signal via
fft...

or would I perform the fft on the signal and then compute signalEnergy
based on the output of the fft

On the signalEnergy formula that you gave, you said yourself that it takes
samples in the time domain. If however, you have a reason to believe that
signalEnergy can be computed quicker when it is in a form to accept samples in
the frequency domain, and if you have the means and the will to rephrase the
formula in this form, then it would be good to compute signalEnergy based on
the FFT output.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top