public void SetFilter(MedianFilterWrapper _filter)
        {
            filter = _filter;

            ignoreChanges = true;

            stepCount.Text = "" + filter.GetSampleCount();

            ignoreChanges = false;
        }
        public void InitFromConfig(string filename)
        {
            configFilename = filename;

            if (!File.Exists(configFilename))
            {
                return;
            }

            configData = JsonConvert.DeserializeObject <FilterConfigData>(File.ReadAllText(configFilename), new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.Auto
            });

            if (filters == null)
            {
                filters = new List <FilterBase> [(int)CMCustomUDPData.DataKey.Max];
            }

            foreach (List <FilterBase> filterList in filters)
            {
                if (filterList != null)
                {
                    filterList.Clear();
                }
            }

            for (int i = 0; i < filters.Length; ++i)
            {
                filters[i] = null;
            }

            for (int i = 0; i < configData.keys.Count; ++i)
            {
                if (configData.keys[i] == null)
                {
                    continue;
                }

                FilterDataKey key = configData.keys[i];

                List <FilterBase> filterList = filters[(int)key.key] = new List <FilterBase>();

                foreach (FilterData filterData in key.filters)
                {
                    if (filterData is SmoothFilterData)
                    {
                        SmoothFilterData   smoothFilterData = (SmoothFilterData)filterData;
                        NestedSmoothFilter newFilter        = new NestedSmoothFilter();
                        newFilter.SetParameters(smoothFilterData.nestCount, smoothFilterData.sampleCount, smoothFilterData.maxDelta);

                        filterList.Add(newFilter);
                    }
                    else
                    if (filterData is KalmanFilterData)
                    {
                        KalmanFilterData  kalmanFilterData = (KalmanFilterData)filterData;
                        KalmanNoiseFilter newFilter        = new KalmanNoiseFilter();
                        newFilter.SetParameters(kalmanFilterData.a, kalmanFilterData.h, kalmanFilterData.q, kalmanFilterData.r, kalmanFilterData.p, kalmanFilterData.x);

                        filterList.Add(newFilter);
                    }
                    else
                    if (filterData is MedianFilterData)
                    {
                        MedianFilterData    fData     = (MedianFilterData)filterData;
                        MedianFilterWrapper newFilter = new MedianFilterWrapper();
                        newFilter.SetParameters(fData.sampleCount);

                        filterList.Add(newFilter);
                    }
                }
            }
        }
        public FilterBase AddFilter(FilterType filterType, CMCustomUDPData.DataKey key, bool updateUI = false)
        {
            List <FilterBase> filterList = filters[(int)key];

            if (filterList == null)
            {
                filterList = filters[(int)key] = new List <FilterBase>();
            }

            FilterBase newFilter = null;

            switch (filterType)
            {
            case FilterType.Smooth:
            {
                NestedSmoothFilter newSmoothFilter = new NestedSmoothFilter();
                newSmoothFilter.SetParameters(1, 3, 0.5f);

                filterList.Add(newSmoothFilter);
                newFilter = newSmoothFilter;

                break;
            }

            case FilterType.Kalman:
            {
                KalmanNoiseFilter newKalmanFilter = new KalmanNoiseFilter();
                newKalmanFilter.SetParameters(1, 1, 0.02f, 1, 0.02f, 0.0f);

                filterList.Add(newKalmanFilter);
                newFilter = newKalmanFilter;

                break;
            }

            case FilterType.KalmanVelocity:
            {
                KalmanVelocityNoiseFilter newKalmanFilter = new KalmanVelocityNoiseFilter();
                newKalmanFilter.SetParameters(1, 1, 0.02f, 1, 0.02f, 0.0f);

                filterList.Add(newKalmanFilter);
                newFilter = newKalmanFilter;

                break;
            }

            case FilterType.SavitzkyGolay:
            {
                break;
            }

            case FilterType.FIR:
            {
                break;
            }

            case FilterType.Median:
            {
                MedianFilterWrapper newFilterW = new MedianFilterWrapper();
                newFilterW.SetParameters(9);

                filterList.Add(newFilterW);
                newFilter = newFilterW;

                break;
            }
            }

            if (updateUI && FilterUI.Instance != null)
            {
                FilterUI.Instance.InitChartForKey(key);
            }



            return(newFilter);
        }
        public void SaveConfig()
        {
            configData = new FilterConfigData();

            for (int i = 0; i < (int)CMCustomUDPData.DataKey.Max; ++i)
            {
                List <FilterBase> filterList = filters[i];
                if (filterList == null)
                {
                    continue;
                }
                else
                {
                    FilterDataKey newConfig = new FilterDataKey(i);
                    configData.keys.Add(newConfig);

                    foreach (FilterBase filter in filterList)
                    {
                        if (filter is NestedSmoothFilter)
                        {
                            SmoothFilterData   newFilterData    = new SmoothFilterData();
                            NestedSmoothFilter nestSmoothFilter = (NestedSmoothFilter)filter;
                            newFilterData.nestCount   = nestSmoothFilter.GetNestCount();
                            newFilterData.sampleCount = nestSmoothFilter.GetSampleCount();
                            newFilterData.maxDelta    = nestSmoothFilter.GetMaxDelta();

                            newConfig.filters.Add(newFilterData);
                        }
                        else
                        if (filter is KalmanNoiseFilter)
                        {
                            KalmanFilterData  newFilterData     = new KalmanFilterData();
                            KalmanNoiseFilter kalmanNoiseFilter = (KalmanNoiseFilter)filter;
                            newFilterData.a = kalmanNoiseFilter.GetA();
                            newFilterData.h = kalmanNoiseFilter.GetH();
                            newFilterData.q = kalmanNoiseFilter.GetQ();
                            newFilterData.r = kalmanNoiseFilter.GetR();
                            newFilterData.p = kalmanNoiseFilter.GetP();
                            newFilterData.x = kalmanNoiseFilter.GetX();

                            newConfig.filters.Add(newFilterData);
                        }
                        else
                        if (filter is MedianFilterWrapper)
                        {
                            MedianFilterData    newFilterData = new MedianFilterData();
                            MedianFilterWrapper filterW       = (MedianFilterWrapper)filter;
                            newFilterData.sampleCount = filterW.GetSampleCount();

                            newConfig.filters.Add(newFilterData);
                        }
                    }
                }
            }


            string outputString = JsonConvert.SerializeObject(configData, Formatting.Indented, new JsonSerializerSettings
            {
                TypeNameHandling = TypeNameHandling.All
            });


            File.WriteAllText(configFilename, outputString);
        }