Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

FFT in java

Thread view: 
beat - 28 Apr 2005 16:07 GMT
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[i]= 2 * ((float)(Math.sqrt(xre[i]*xre[i] +
xim[i]*xim[i]))/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[i] = signal[i];
           xim[i] = 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[i]= 2 * ((float)(Math.sqrt(xre[i]*xre[i] +
xim[i]*xim[i]))/n);
       return mag;
   }
}
Boudewijn Dijkstra - 28 Apr 2005 21:55 GMT
> 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
[quoted text clipped - 10 lines]
> xim[i]*xim[i]))/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.
beat - 02 May 2005 20:05 GMT
> > 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
[quoted text clipped - 25 lines]
> 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
Boudewijn Dijkstra - 03 May 2005 11:41 GMT
> thanks for the explanation, on the output of the FFT - I have run into
> another little issue with the output of the FFT.
[quoted text clipped - 8 lines]
> 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.
beat - 04 May 2005 21:45 GMT
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[i] = (float) (frequency[i] * 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[i]*frequency[i]);
            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
Boudewijn Dijkstra - 06 May 2005 14:55 GMT
> Based on what you said previously:
>> Loudness is a quite subjective measure, largely dependant on the
[quoted text clipped - 8 lines]
> 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.
beat - 11 May 2005 18:26 GMT
<snip>

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

angelo

> > 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.
Boudewijn Dijkstra - 11 May 2005 19:50 GMT
> <snip>
>
[quoted text clipped - 13 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.