swharden/FftSharp

create filter module

Closed this issue · 1 comments

If you start with a signal, take the FFT, edit the complex numbers, then take the iFFT, you result with a signal whose spectral components can be filtered. Create a module to make filtering easy. Something like:

double[] audio = ReadWav("test.wav");
double[] filtered1 = FftSharp.Filter.LowPass(audio, frequency: 500);
double[] filtered2 = FftSharp.Filter.HighPass(audio, frequency: 500);
double[] filtered3 = FftSharp.Filter.BandPass(audio, frequency1: 300, frequency2: 500);
double[] filtered4 = FftSharp.Filter.BandStop(audio, frequency1: 300, frequency2: 500);

I'll package this up in a module, but for the record here is some discrete code to apply a 2kHz low-pass filter:

// convert input data to frequency domain
Complex[] fft = FftSharp.Transform.FFT(Values);
double[] fftFreqs = FftSharp.Transform.FFTfreq(SampleRate, fft.Length, oneSided: false);

// silence portions where frequency is outside the limits
double FrequencyLimit = 2000;
for (int i = 0; i < fft.Length; i++)
{
    if (Math.Abs(fftFreqs[i]) > FrequencyLimit)
    {
        fft[i].Real = 0;
        fft[i].Imaginary = 0;
    }
}

// take the inverse FFT and collect the real component
FftSharp.Transform.IFFT(fft);
double[] Filtered = new double[fft.Length];
for (int i = 0; i < fft.Length; i++)
    Filtered[i] = fft[i].Real;

image