Skip to content

Instantly share code, notes, and snippets.

@son0p
Last active February 7, 2016 12:28
Show Gist options
  • Select an option

  • Save son0p/0fce9ae0ecb3dcfebce5 to your computer and use it in GitHub Desktop.

Select an option

Save son0p/0fce9ae0ecb3dcfebce5 to your computer and use it in GitHub Desktop.
// 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 >>>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment