Search code examples
c#filteringbutterworth

Filter applying muliple times


I'm using the 4th order Butterworth filter described here. I created a button that filters my data and plots them on Click event. It works perfectly fine, except for one thing : if I click multiple times on the button, the data are filtered further, giving me a "smoother" curve each time. I'm not sure this is really something I want to get rid of, but I'd like first to understand this behavior, because I have the feeling the input data are not modified, hence this should'nt happen.

Here is my button's code :

/// <summary>
    /// Handles the Click event on the smoothExperimentDataButton button ; applies 4th order Butterworth filter to the Experiment data and plots it
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="f"></param>
    private void smoothExperimentDataButton_Click(object sender, EventArgs f)
    {
        chart1.Series["Filtered experiment"].Points.Clear();
        double Samplingrate = 1 / deltaTimeinsec;
        long dF2 = ValeurPixelsSumExperiment.Length - 1;
        double[] Dat2 = new double[dF2 + 4];
        double[] data = ValeurPixelsSumExperiment;

        // Copy input data to Dat2
        for (long r = 0; r < dF2; r++)
        {
            Dat2[2 + r] = ValeurPixelsSumExperiment[r];
        }
        Dat2[1] = Dat2[0] = ValeurPixelsSumExperiment[0];
        Dat2[dF2 + 3] = Dat2[dF2 + 2] = ValeurPixelsSumExperiment[dF2];

        const double pi = 3.14159265358979;
        double wc = Math.Tan(CutOff * pi / Samplingrate);
        double k1 = 1.414213562 * wc; // Sqrt(2) * wc
        double k2 = wc * wc;
        double a = k2 / (1 + k1 + k2);
        double b = 2 * a;
        double c = a;
        double k3 = b / k2;
        double d = -2 * a + k3;
        double e = 1 - (2 * a) - k3;

        // RECURSIVE TRIGGERS - ENABLE filter is performed (first, last points constant)
        double[] DatYt = new double[dF2 + 4];
        DatYt[1] = DatYt[0] = ValeurPixelsSumExperiment[0];
        for (long s = 2; s < dF2 + 2; s++)
        {
            DatYt[s] = a * Dat2[s] + b * Dat2[s - 1] + c * Dat2[s - 2] + d * DatYt[s - 1] + e * DatYt[s - 2];
        }
        DatYt[dF2 + 3] = DatYt[dF2 + 2] = DatYt[dF2 + 1];

        // FORWARD filter
        double[] DatZt = new double[dF2 + 2];
        DatZt[dF2] = DatYt[dF2 + 2];
        DatZt[dF2 + 1] = DatYt[dF2 + 3];
        for (long t = -dF2 + 1; t <= 0; t++)
        {
            DatZt[-t] = a * DatYt[-t + 2] + b * DatYt[-t + 3] + c * DatYt[-t + 4] + d * DatZt[-t + 1] + e * DatZt[-t + 2];
        }

        // Calculated points copied for return
        for (long p = 0; p < dF2; p++)
        {
            data[p] = DatZt[p];
        }

        for (int i = 1; i < 1023; i++)
        {
            chart1.Series["Filtered experiment"].Points.AddXY(i, data[i]);
        }
        chart1.Series["Experiment"].Points.Clear();
    }

Thanks a lot !


Solution

  • At this line:

    double[] data = ValeurPixelsSumExperiment;
    

    data will be referencing the ValeurPixelsSumExperiment array, which seems to contain your source data.

    So wherever you do data[x] = y;, you're essentially doing ValeurPixelsSumExperiment[x] = y; (overwriting the input data)

    Which is what happens at the end of your calculations:

    // Calculated points copied for return
    for (long p = 0; p < dF2; p++)
    {
        data[p] = DatZt[p];
    }