Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create filter module #23

Closed
swharden opened this issue Aug 14, 2020 · 1 comment
Closed

create filter module #23

swharden opened this issue Aug 14, 2020 · 1 comment

Comments

@swharden
Copy link
Owner

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);
@swharden
Copy link
Owner Author

swharden commented Nov 1, 2020

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

swharden added a commit that referenced this issue Nov 1, 2020
@swharden swharden closed this as completed Nov 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant