public void InitFromConfig(string filename) { configFilename = filename; if (!File.Exists(configFilename)) { return; } configData = JsonConvert.DeserializeObject <FilterConfigData>(File.ReadAllText(configFilename), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); 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); } } } }
public void SaveConfig() { configData = new FilterConfigData(); for (int i = 0; i < (int)GenericProviderData.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); } } } } string outputString = JsonConvert.SerializeObject(configData, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }); File.WriteAllText(configFilename, outputString); }
public FilterBase AddFilter(FilterType filterType, GenericProviderData.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(0, 10, 1000.0f); 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.SavitzkyGolay: { break; } case FilterType.FIR: { break; } } if (updateUI && FilterUI.Instance != null) { // FilterUI.Instance.InitChartForKey(key); } return(newFilter); }
public void SetFilter(KalmanNoiseFilter _filter) { filter = _filter; ignoreChanges = true; A.Text = "" + filter.GetA(); H.Text = "" + filter.GetH(); Q.Text = "" + filter.GetQ(); R.Text = "" + filter.GetR(); P.Text = "" + filter.GetP(); X.Text = "" + filter.GetX(); ignoreChanges = false; }
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); }