Skip to content

Instantly share code, notes, and snippets.

@son0p
Last active February 7, 2016 12:28
Show Gist options
  • Save son0p/0fce9ae0ecb3dcfebce5 to your computer and use it in GitHub Desktop.
Save son0p/0fce9ae0ecb3dcfebce5 to your computer and use it in GitHub Desktop.

Revisions

  1. federico lopez renamed this gist Feb 7, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. federico lopez revised this gist Feb 7, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions get FFF spectrum
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    ```java
    // get FFT spectrum
    // calculate frequency (approx)
    // first bin is DC (freq = 0Hz)
    @@ -36,3 +37,4 @@ for ( 0 => int i; i < fft.size()/2; i++ )
    i * samplingRate / fft.size() => float freq;
    <<< "bin:", i, "freq:", freq, "power:", spectral[i]$polar >>>;
    }
    ```
  3. federico lopez created this gist Feb 7, 2016.
    38 changes: 38 additions & 0 deletions get FFF spectrum
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    // get FFT spectrum
    // calculate frequency (approx)
    // first bin is DC (freq = 0Hz)
    // make sure to filter out the DC (signal - mean(signal) => signal)
    // also make sure to applying band-pass filter at Nyquist frequency
    // Nyquist frequency == sampling rate / 2

    // Input signal
    SinOsc g => FFT fft => blackhole;
    // set samplingRate
    second / samp => float samplingRate;
    <<< "Sampling rate =", samplingRate >>>;

    // FFT bin size
    16 => fft.size;

    // spectrum, first half bins, 0..N/2-1
    // the rest is useless, it's only conjugate of the first half
    complex spectral[fft.size()/2];

    // a sample frequency of the sinusoidal input
    5500 => g.freq;

    // let fft.size samples pass
    fft.size()::samp => now;

    // take fast fourier transform
    fft.upchuck();
    // get the spectrum
    fft.spectrum( spectral );

    // display spectrum at Nth bin
    <<< "Spectrum:" >>>;
    for ( 0 => int i; i < fft.size()/2; i++ )
    {
    i * samplingRate / fft.size() => float freq;
    <<< "bin:", i, "freq:", freq, "power:", spectral[i]$polar >>>;
    }