// 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
1024 => 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 );
// calculate highest power peak,
// which is the main frequency
0 => float highestPower;
-1 => int highestIndex;
for ( 0 => int i; i < fft.size()/2; i++ )
{
if ((spectral[i]$polar).mag > highestPower)
{
(spectral[i]$polar).mag => highestPower;
i => highestIndex;
}
}
// calculate freq approximation
highestIndex * samplingRate / fft.size() => float freq0;
<<< "Frequency (approx):" >>>;
<<< "bin:", highestIndex, "freq:", freq0 >>>;
<<< "magnitude:", (spectral[highestIndex]$polar).mag,
"phase:", (spectral[highestIndex]$polar).phase >>>;
Created
February 7, 2016 12:32
-
-
Save son0p/73b4b520a16189454d73 to your computer and use it in GitHub Desktop.
chuck code
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment