I want to get frequencies of a voice input acquired from microphone in real-time. I searched about this and got to know about FFT and another 2, 3 algorithms but implementing those algorithms seemed very complicated.
I am looking for a C# library that enable me to simply get frequencies into an array without having to implement it.
Indeed, like you guessed, signal processing are the kind of algorithms you do not want to implement yourself because there are quite complex.
Apparently, for C#, you can find some libraries out there but here is a good library performing DFT/FFT, and currently active : DSPLib
DSPLib has several main parts, but it's basic goal is to allow a real Fourier Transform to be preformed on a time series input array, resulting in a usable classic spectrum output without any further tweaking required by the user.
If I understood correctly what you wanted, the 2nd example is the task you want to achieve (it lacks the recording from the mic).
Just one piece of advice, be careful with the windowing as it can influence the spectrum of your signal.
Just one note on a concept which could let you puzzled, the zero padding is just a trick to have a number of samples equals to a power of 2, in order to use the FFT algorithm. Therefore, the resulted spectrum is somehow 'artificially' produced with a better resolution.
void example2()
{
// Same Input Signal as Example 1 - Except a fractional cycle for frequency.
double amplitude = 1.0; double frequency = 20000.5;
UInt32 length = 1000; UInt32 zeroPadding = 9000; // NOTE: Zero Padding
double samplingRate = 100000;
double[] inputSignal = DSPLib.DSP.Generate.ToneSampling(amplitude, frequency, samplingRate, length);
// Apply window to the Input Data & calculate Scale Factor
double[] wCoefs = DSP.Window.Coefficients(DSP.Window.Type.Hamming, length);
double[] wInputData = DSP.Math.Multiply(inputSignal, wCoefs);
double wScaleFactor = DSP.Window.ScaleFactor.Signal(wCoefs);
// Instantiate & Initialize a new DFT
DSPLib.DFT dft = new DSPLib.DFT();
dft.Initialize(length, zeroPadding); // NOTE: Zero Padding
// Call the DFT and get the scaled spectrum back
Complex[] cSpectrum = dft.Execute(wInputData);
// Convert the complex spectrum to note: Magnitude Format
double[] lmSpectrum = DSPLib.DSP.ConvertComplex.ToMagnitude(cSpectrum);
// Properly scale the spectrum for the added window
lmSpectrum = DSP.Math.Multiply(lmSpectrum, wScaleFactor);
// For plotting on an XY Scatter plot generate the X Axis frequency Span
double[] freqSpan = dft.FrequencySpan(samplingRate);
// At this point a XY Scatter plot can be generated from,
// X axis => freqSpan
// Y axis => lmSpectrum
}
After plotting, here is the result :
With a zoom-in on the peak of the spectrum :