Search code examples
javafftlowpass-filter

Implementing Low pass filter on frequencies using JAVA by just having frequency array and cut off frequency


Initially I have an array of time and an array of voltage and I have applied FFT and converted that time domain into frequency domain.After applying FFT I got an array of frequencies. Now I have cut off frequency and I need to implement Low pass filter on the same. I need to do this using JAVA. Could someone please refer me if there is any open source available or any idea of implementing the same. Any references that would be implemented using frequency values and cut off frequency would help.

I am completely new to this topic so my approach of question might be little weird. Thanks in advance for support!!!


Solution

  • Since you already have a array with the FFT values, you can implement a very crude low pass filter by just setting those FFT coefficients that correspond to frequencies over your cut-off value to zero.If you need a nicer filter you can implement a digital filter or find an LPF implementation online and just use that.

    EDIT: After computing the FFT you don't get an array of frequencies, you get an array of complex numbers representing the magnitude and phase of the data.You should be able to know what frequency each and every complex number in the array corresponds to because the FFT result will correspond to evenly spaced frequencies ranging from 0 to f_s, where f_s is the sampling frequency you used to get your data.

    A useful exercise might be to first try and plot a frequency spectrum, because after plotting it, it will be clear how you can discard high frequencies thus realising a LPF.This slightly similar post might help you: LINK

    EDIT: 1) First you need to find the sampling frequency (f_s) of your data, this is the number of samples have taken every second.It can be computed using f_s = 1/T, where T is the time interval between any two consecutive samples in the time domain.

    2) After this you divide f_c by f_s, where f_c is the cut-off frequency to get a constant k.

    3) You then set all COMPLEX numbers above index ( k times N) in your array to zero, where N is the number of elements in your array, simple as that, that will give you a basic Low pass filter (LPF).

    Rough, indicative (pseudo)code below:

    Complex[] fftData = FFT(myData);
    int N = fftData.Length;
    
    float T = 0.001;   
    float f_c = 500;   //f_c = 500Hz
    float f_s = 1/T;   //f_s = 1000Hz
    
    float k = f_c/f_s;
    int index = RoundToNextLargestInteger(k * N);
    
    //Low pass filter   
    for(int i = index; index < N; index++)
       fftData[i] = 0;
    

    The fftData you receive in your case will not already be in the form of elements from the Complex class, so make sure you know how your data is represented and which data elements to set to zero.

    This is not really a good way to do it though as a single frequency in your data can be spread over several bins because of leakage so the results would be nasty in that case.Ideally you would want to design a proper digital filter or just use some software library.So if you need a very accurate LPF, you can go through the normal process of designing analog LPF and then warping it to a digital filter as discussed in THIS document.