Exemplo n.º 1
0
        private void FilterWithFiniteImpluseResponseInTheTimeDomain(object o)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            try
            {
                int L  = int.Parse(this.FilterLength);
                int Fc = int.Parse(this.CutoffFrequency);
                int Fs = this.SampleRate;

                double[] filter   = FilterWithFiniteImpulseResponse.GetFilterValues(Fc, Fs, L);
                double[] filtered = FourierWindows.MultiplyByWindowFunction(filter, this.SelectedWindowType);
                double[] result   = FilterWithFiniteImpulseResponse.FilterInTheTimeDomain(this.SignalData.Samples, filtered, L);

                this.FilterResultInTheTimeDomain = result;
            }
            catch (Exception e)
            {
                Notify.Error(e.Message);
            }

            sw.Stop();
            this.FilterTimeInTheTimeDomain = sw.ElapsedMilliseconds.ToString();
        }
Exemplo n.º 2
0
        private void FilterWithFiniteImpluseResponseInTheFrequencyDomain(object o)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            try
            {
                int      L      = int.Parse(this.FilterLength);
                int      Fc     = int.Parse(this.CutoffFrequency);
                int      Fs     = this.SampleRate;
                int      R      = int.Parse(this.HopSize);
                int      M      = int.Parse(this.WindowSize);
                int      n      = GetExpandedPow2(M + L - 1);
                int      size   = this.SignalData.Samples.Length + n - L;
                double[] result = new double[size];

                double[][]  windows        = new double[size / R][];
                Complex[][] windowsComplex = new Complex[size / R][];

                for (int i = 0; i < windows.Length; i++)
                {
                    windows[i]        = new double[n];
                    windowsComplex[i] = new Complex[n];
                }

                double[] windowFactors = FourierWindows.GetWindowFactors(M, this.SelectedWindowType);
                for (int i = 0; i < windows.Length; i++)
                {
                    for (int j = 0; j < M; j++)
                    {
                        if (i * R + j < this.SignalData.Samples.Length)
                        {
                            windows[i][j] = windowFactors[j] * this.SignalData.Samples[i * R + j];
                        }
                        else
                        {
                            windows[i][j] = 0;
                        }
                    }
                    for (int j = M; j < n; j++)
                    {
                        windows[i][j] = 0;
                    }
                }

                double[] windowFilterFactors = FourierWindows.GetWindowFactors(L, this.SelectedWindowType);
                double[] filterFactors       = FilterWithFiniteImpulseResponse.GetFilterValues(Fc, Fs, L);
                double[] filtered            = new double[n];
                for (int i = 0; i < L; i++)
                {
                    filtered[i] = windowFilterFactors[i] * filterFactors[i];
                }

                for (int i = L; i < n; i++)
                {
                    filtered[i] = 0;
                }

                if (this.SelectedZeroFillingMethod == ZeroFillingMethod.CauselessFilter)
                {
                    int shiftNumberFilter = (L - 1) / 2;

                    IEnumerable <double> shiftedFilter = filtered.Take(shiftNumberFilter);
                    List <double>        filteredTemp  = filtered.Skip(shiftNumberFilter).ToList();
                    filteredTemp.AddRange(shiftedFilter);
                    filtered = filteredTemp.ToArray();
                }

                Complex[] filteredComplex = FourierTransform.FFT(filtered);

                for (int i = 0; i < windows.Length; i++)
                {
                    windowsComplex[i] = FourierTransform.FFT(windows[i]);
                    for (int j = 0; j < windowsComplex[i].Length; j++)
                    {
                        windowsComplex[i][j] *= filteredComplex[j];
                    }
                    windows[i] = FourierTransform.IFFT(windowsComplex[i]);
                }

                for (int i = 0; i < windows.Length; i++)
                {
                    for (int j = 0; j < windows[i].Length; j++)
                    {
                        if (i * R + j < this.SignalData.Samples.Length)
                        {
                            result[i * R + j] += windows[i][j];
                        }
                    }
                }

                this.FilterResultInTheFrequencyDomain = result;
            }
            catch (Exception e)
            {
                Notify.Error(e.Message);
            }

            sw.Stop();
            this.FilterTimeInTheFrequencyDomain = sw.ElapsedMilliseconds.ToString();
        }