Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C#] FFT in read world

  1. Jan 24, 2014 #1
    I am trying to make this

    but my FFT is not getting close enough to the right Values

    I am Sampling at a 44100 Hz rate

    and I am sending

    16384 samples to the fft at a time

    but it is not close

    I have attach a pic

    what should i do?
    I know the FFT work.. because i compared it to mat lab...

    so how can I troubleshoot this?

    Attached Files:

    Last edited by a moderator: Sep 25, 2014
  2. jcsd
  3. Jan 25, 2014 #2
    How did you compare to Matlab? By giving both a simple n cycle sine wave?
    What happens if you give both the same 16k sampled mixed sound source?
    In what way is the answer then not close? What does Matlab give and what does your code give?
    Are you windowing your sample data before FFT? http://en.wikipedia.org/wiki/Window_function
  4. Jan 25, 2014 #3
    I have posting on my other fft threads... but I just enter sinewaves and just buffer with samples, both in mat lab and in my program.

    if you look at my picture you will see that
    youtube is outputting a frequency of 117Hz

    but my programs highest peek is over 60 then 70.
    that is not right
  5. Jan 25, 2014 #4
    no I am not windowing my sample before the FFT...
    the FFT gets raw data

    Code (Text):

     for (int index = 0; index < 32768; index += 2)

                    short sample = (short)((buffer[index + 1] << 8) |
                                            buffer[index + 0]);

                    float sample32 = sample / 32768f;
                    buffer1[tempint] = (double)sample32;

    I am Sampling at a 44100 Hz rate

    and I am sending

    16384 samples to the fft at a time
  6. Jan 27, 2014 #5
    I presume that tempint has been initialized to zero.
    Also, how did you fill that buffer? Was it with a WAV file?
  7. Jan 27, 2014 #6
    no it is from a microphone ..
    and yes tempint is.
    I am writing this in C#
  8. Jan 27, 2014 #7
    What evidence do you have that you are successfully sampling amplitude information from the microphone, and that it contains a sine waveform of significant amplitude.

    As a test, have you tried putting samples into array that are generated from the sin() function?
  9. Jan 27, 2014 #8
    it is not a sine waveform... should it be ?

    if you send me you email
    I can send you my program
  10. Jan 27, 2014 #9
    I didn't look at the youtube video, but the frame gram you showed as an attachment showed a sine wave. Also, a sine wave is a good test pattern for an FFT exercise since it should show a single spike at the corresponding wavelength.

    I guessing that you played the youtube video, picked up the sound with the mic, and tried to capture a segment of it at 44.1KHz.

    If that's about what you did, then I would definitely put a known sine wave (computed from the sin() function) into your FFT subroutine or tool to verify that you have that part of the problem working. Then work on getting good data from your microphone to "buffer".

    I don't want to look at that much of your code. You get to do all the real work.
  11. Jan 27, 2014 #10
    sorry I miss understood you.

    yes the youtube video is a sinewave
    if you go to youtube and look up "high frequency" you will see it
  12. Jan 27, 2014 #11
    also I have tested the fft by enter in a know sinewave and out putting the answered...
    that works,
    I have also compared my fft to matlab and my fft works for 2^n number of samples
  13. Jan 27, 2014 #12
    So it's really looking as though the problem is getting the signal you want through the microphone to "buffer".
  14. Jan 27, 2014 #13
    yeah I guess
  15. Jan 27, 2014 #14
    Supposedly your collecting at 44.1KHz. So find a tone that's about 5KHz, capture that sound with you mic, and then print out the first 20 values in buffer. If all is working well, you will see about two full sine waves.

    If you see something else, you're not capturing correctly.

    For example:

    In general, you want to be able to test isolated parts of your code so that you can determine where the problem is.
  16. Jan 27, 2014 #15
    ok i cant add the right picture on here... the forum will not let me ...

    but this is how it looks
    the highest peek is 2500
    which is haft of 5000

    Attached Files:

  17. Jan 27, 2014 #16
    this is how the sample input looks

    it repeats

    Attached Files:

  18. Jan 27, 2014 #17
    I saw this one too : test2.jpg
  19. Jan 27, 2014 #18
    also this test3.jpg
  20. Jan 27, 2014 #19
    I'm guessing there's something wrong with the raw data charts - probably some sort of under-sampling issue.

    The fact that you're getting a spike at 2500Hz, with harmonics at 2500Hz intervals is very encouraging. It means that some rendition of the sound is making to your buffer.

    My guess is that the samples are actually 8-bit samples, not 16-bit - or that you are capturing stereo instead on mono.
  21. Jan 27, 2014 #20
    well this is weird : I use that same site you sent to me and I did 4000 Hz
    and the highest point is 2000Hz

    I also did 3000Hz from that same site
    and the highest point I get is 1499.249

    I also did 10,000
    but this is what i got :

    any ideas as to why?
  22. Jan 27, 2014 #21
    Code (Text):

                PointPairList list = new PointPairList();
                PointPairList list2 = new PointPairList();
                byte[] buffer = e.Buffer;
                int bytesRecorded = e.BytesRecorded;
                points = new RollingPointPairList(32768 / 2);
                buffer1 = new double[(32768 / 2)];
                int tempint = 0;
                for (int index = 0; index < 32768; index += 2)

                    buffer1[tempint] = ((buffer[index + 1] << 8) |
                                            buffer[index + 0]);


                DSP = new DSPclass(buffer1, 44100);
    is it a 16 bit sample and I break in to one double
  23. Jan 27, 2014 #22
    here is my code to print out the fft as a frequency
    N ( the number of samples) = 16384
    Fs ( the sampling frequency) = 44100 Hz
    R( R is a int) = 16384

    Code (Text):

            public int frequencies(double[] freq, double [] Ctemp)
                int counter = 0;
                for (int i = 0; i < R; i++)
                            if (((i / N) * Fs) >= (Fs / 2)) // nyquist
                                return counter;

                            if (Math.Abs(F[i].Magnitude) > 100)
                                freq[counter] = (i / N) * Fs;
                                Ctemp[counter] = F[i].Magnitude;

                return counter;  

    is that right ?
    Frequency = (bin / N) * Fs?
    and I should be using the Magnitude right ?
    oh this is all c#
  24. Jan 27, 2014 #23
    If the magnitude is less than 100, you skip the frequency value???
    I'm guessing that you are trying to treat all small magnitudes as noise. If that's the case, you should set Ctemp[counter] to 0.

    Since both i and N are integers and i<N, i/N will always be zero. So freq[counter] is always zero.
  25. Jan 27, 2014 #24
    yes i am treating all the small magnitudes as Noise .
    should it be higher than 100?

    no , I get values for my counter
    but is Frequency = (bin / N) * Fs?
    is that right ?
  26. Jan 27, 2014 #25
    so I just tried this :
    Code (Text):

     for (int index = 0; index < 32768; index++)
                    buffer1[tempint] = buffer[index];
    and the fft is off
    so maybe it really is 16bits

    do anyone know anything that might help me?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook