Click here to Skip to main content
15,893,564 members
Home / Discussions / C#
   

C#

 
GeneralRe: xsd? Pin
Lev Danielyan28-Dec-08 1:58
Lev Danielyan28-Dec-08 1:58 
QuestionProjects/Libraries On Tuning? Pin
Brandon X.27-Dec-08 14:44
Brandon X.27-Dec-08 14:44 
AnswerRe: Projects/Libraries On Tuning? [modified] Pin
Ravadre28-Dec-08 17:32
Ravadre28-Dec-08 17:32 
QuestionRe: Projects/Libraries On Tuning? Pin
Brandon X.29-Dec-08 6:47
Brandon X.29-Dec-08 6:47 
AnswerRe: Projects/Libraries On Tuning? Pin
Ravadre29-Dec-08 7:33
Ravadre29-Dec-08 7:33 
GeneralRe: Projects/Libraries On Tuning? Pin
Brandon X.29-Dec-08 8:10
Brandon X.29-Dec-08 8:10 
GeneralRe: Projects/Libraries On Tuning? Pin
Ravadre29-Dec-08 8:16
Ravadre29-Dec-08 8:16 
QuestionRe: Projects/Libraries On Tuning? [modified] Pin
Brandon X.29-Dec-08 19:29
Brandon X.29-Dec-08 19:29 
Ugh. Back from work late and I must be doing something wrong. (Warning, big post ahead).

I wanted to see how the code would work on recorded samples, so I recorded 3 WAV files, each one for my low string E on my guitar. Thus, the three WAV files should produce the same data, correct? By this I mean the magnitude I would assume would be roughly the same for all three files; since they are the same note & same string on the guitar. (I basically plucked the string and recorded, and repeated that 3 times).

But that is not the case. Here is how I went about it:

In code, I jump to 44 bytes into the WAV file (which is where the WAV data actually starts) and read that whole thing into a big byte array (so, byte[] arrDataToProcess).

Now, from the previous mentioned project above, I see where his FFT library processes the data in 16 bit samples (16384) - so I split arrDataToProcess into a List<byte[]> lstArraysToProcess of 16384 each; then feed each one into a method that converts the bytes to doubles, and then call the FFT on that:

public static double[] Process(ref byte[] wave)
       {
           double[] _fftDataToReturn;
           double[] _waveDataToProcess = new double[wave.Length / 4];

           int h = 0;
           for (int i = 0; i < wave.Length; i+=4)
           {
               _waveDataToProcess[h] = (double)BitConverter.ToInt16(wave, i);
               h++;
           }
           _fftDataToReturn = SoundCatcher.FourierTransform.FFT(ref _waveDataToProcess);
           return _fftDataToReturn;
       }


the SoundCatcher.FourierTransform.FFT(ref _waveDataToProcess) method as seen above looks like so (warning, large code block ahead):

static public double[] FFT(ref double[] x)
     {
         // Assume n is a power of 2
         n = x.Length;
         nu = (int)(Math.Log(n) / Math.Log(2));
         int n2 = n / 2;
         int nu1 = nu - 1;
         double[] xre = new double[n];
         double[] xim = new double[n];
         double[] magnitude = new double[n2];
         double[] decibel = new double[n2];
         double tr, ti, p, arg, c, s;
         for (int i = 0; i < n; i++)
         {
             xre[i] = x[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 = BitReverse(k >> nu1);
                         arg = 2 * (double)Math.PI * p / n;
                         c = (double)Math.Cos(arg);
                         s = (double)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 = BitReverse(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++;
         }
         for (int i = 0; i < n / 2; i++)
            magnitude[i] = (float)(Math.Sqrt((xre[i] * xre[i]) + (xim[i] * xim[i])));
         //    decibel[i] = 10.0 * Math.Log10((float)(Math.Sqrt((xre[i] * xre[i]) + (xim[i] * xim[i]))));
         return magnitude;
        // return decibel;
     }
 }


I collect all the returned double[] from public static double[] Process(ref byte[] wave) into a List<double[]> lstFFTData, which I then splice back together into one huge array of double[] named arrFinalData.

Finally, I attempt to do the logic you mentioned about finding the maximum magnitude by iterating over this array (arrFinalData). This, however, keeps producing different numbers for the 3 WAV files; even though they are the same note (and I have taken much care to try and play the note at the same volume and with no background noise and the mic the same distance from the guitar each time). So, fe:

* 1st WAV file maximum magnitude: 77140.71
* 2nd WAV file maximum magnitude: 30483.56
* 3rd WAV file maximum magnitude: 79244.77

(numbers are obviously truncated)

As well, I am also a bit lost at second glance as to your suggestion about
<br />
T - array of input data (sound we've catched).<br />
F - array of output data (magnitudes of complex numbers computed by FFT)<br />
length(T) == length(F)


because if I do a comparison on the arrDataToProcess.Length versus arrFinalData.Length, this of course does not match.

Alas, I think I am way in over my head.

Am I:

* wrong in my implementation?
* wrong in using the FFT class from the above mentioned project?
* wrong about interpreting the results?
* wrong altogether about how this should work?

or am I right in all of my implementation, and possibly just have a bug somewhere else in my code?

If you think the problem lies between the keyboard and chair, that is fine too Smile | :)

Thanks for taking the time to read,

Brandon.

modified on Tuesday, December 30, 2008 3:08 AM

AnswerRe: Projects/Libraries On Tuning? Pin
Ravadre30-Dec-08 4:42
Ravadre30-Dec-08 4:42 
QuestionRe: Projects/Libraries On Tuning? [modified] Pin
Brandon X.31-Dec-08 10:24
Brandon X.31-Dec-08 10:24 
AnswerRe: Projects/Libraries On Tuning? Pin
Ravadre31-Dec-08 12:35
Ravadre31-Dec-08 12:35 
QuestionC# .NET Windows Media Player WM_COPYDATA *whimper* Pin
spamoom27-Dec-08 13:44
spamoom27-Dec-08 13:44 
AnswerRe: C# .NET Windows Media Player WM_COPYDATA *whimper* Pin
Richard Andrew x6427-Dec-08 14:45
professionalRichard Andrew x6427-Dec-08 14:45 
QuestionRe: C# .NET Windows Media Player WM_COPYDATA *whimper* Pin
spamoom28-Dec-08 0:13
spamoom28-Dec-08 0:13 
AnswerRe: C# .NET Windows Media Player WM_COPYDATA *whimper* Pin
Richard Andrew x6428-Dec-08 1:36
professionalRichard Andrew x6428-Dec-08 1:36 
GeneralRe: C# .NET Windows Media Player WM_COPYDATA *whimper* Pin
spamoom28-Dec-08 2:04
spamoom28-Dec-08 2:04 
Question[Message Deleted] Pin
TwoSocks27-Dec-08 13:40
TwoSocks27-Dec-08 13:40 
Questionlock the windows xp Pin
abu rakan27-Dec-08 12:06
abu rakan27-Dec-08 12:06 
AnswerRe: lock the windows xp Pin
User 665827-Dec-08 13:00
User 665827-Dec-08 13:00 
Questionavi file Pin
manishkumarcse27-Dec-08 11:57
manishkumarcse27-Dec-08 11:57 
GeneralRe: avi file Pin
Luc Pattyn27-Dec-08 12:38
sitebuilderLuc Pattyn27-Dec-08 12:38 
AnswerRe: avi file Pin
Code Soldier28-Dec-08 5:25
Code Soldier28-Dec-08 5:25 
QuestionDatabase update error help Pin
hatan8627-Dec-08 9:39
hatan8627-Dec-08 9:39 
AnswerRe: Database update error help Pin
Colin Angus Mackay27-Dec-08 9:57
Colin Angus Mackay27-Dec-08 9:57 
GeneralRe: Database update error help Pin
hatan8627-Dec-08 10:18
hatan8627-Dec-08 10:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.