需要过滤收到的数据 [英] Need to filter data that received

查看:69
本文介绍了需要过滤收到的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

 我正在处理的项目是一个获得CTN阻力然后找到温度的应用程序。我每1秒获得一次实时温度,但我想像这样过滤它:

 the project that I'm working on is an application that get CTN's resistance and then find the temperature. I get in a real time temperature every 1s but I want to filter it like this:

股票5温度值,然后取第2,第3和第4,并做平均值。所以平均值将是显示的温度值。

Stock 5 values of temperature and then take the 2nd, 3th and 4th and do the average. So the average will be the value of temperature that will be displayed.

非常感谢提前。

 public void Calc_TEMP()
        {
            string LastValue = "-31", LastKey = "200000";

            string[] lines = File.ReadAllLines(NTC_file);
            //List<string> list = new List<string>();
            Dictionary<string, string> dictionary = new Dictionary<string, string>();

            foreach (var item in lines)
            {
                if (item.Any(char.IsDigit))
                {
                    //list.Add(item);
                    string[] s = item.Split(';');
                    dictionary.Add(s[0], s[1]);
                }
            }
               foreach (var item in dictionary)
                {
                    if (Convert.ToInt32(item.Key) < Ntc_Value)
                    {
                        vari = Ntc_Value;

                        val1 = ((float)Convert.ToInt32(LastValue) - ((float)Convert.ToInt32(item.Value))) / (Convert.ToInt32(LastKey) - (Convert.ToInt32(item.Key)));
                        val2 = (Convert.ToInt32(LastKey) * (float)Convert.ToInt32(item.Value)) - ((float)Convert.ToInt32(LastValue) * (Convert.ToInt32(item.Key)));
                        val3 = val2 / (Convert.ToInt32(LastKey) - (Convert.ToInt32(item.Key)));
                        val = val1 * vari + val3;
                        t_var = val.ToString(".##");
                        break;
                    }
                    LastValue = item.Value;
                    LastKey = item.Key;
                }

                Console.Write("Temperature :  ");
                Console.WriteLine(t_var + "°C");            
        }
        public string Get_Temp()
        {
            return t_var;
        }
        public void NumTempFilter()
        {
            sw = new StreamWriter("D:\\Users\\Ioualih\\Desktop\\Documents\\Temporaire.txt",true);
            sw.WriteLine(val);
            sw.Close();
            
        }
        
        public float Get_NumTemp()
        {     
            return val;
        }


    } 

推荐答案

" 库存5温度值,然后取第2个,第3个和第4个并做平均值"

这是什么意思?你的意思是给定5个值跳过第一个和最后一个值并使用其他值来计算平均值?如果是这样,则可以使用LINQ完成。

What do you mean by this? Do you mean given 5 values skip the first and last values and use the others to calculate average? If so then that can be done using LINQ.

var values = new[] { -1, 10, 20, 30, 100 };

var valuesToUse = values.Skip(1).Take(3);

var avg = valuesToUse.Average();

在您的代码中,您使用的是字典而不是列表。你没有定义你的文件结构,所以我假设你得到某种键/值对。问题是你每个键只能获得1个值,所以我不知道你提到的5个值
是如何相关的。如果每个键有多个值,那么我建议您使用List来包含键/值对(作为一个简单的结构),然后使用分组按键分组。在分组之后,您可以使用早期的LINQ代码来获得每个键的平均

In your code you're using a dictionary instead of a list. You didn't define your file structure so I assume you are getting some sort of key/value pair. The problem with that is you'd only get 1 value per key so I don't know how that relates to your 5 values you mentioned. If you have multiple values per key then I'd recommend that you use a List to contain the key/value pairs (as a simple struct) and then use grouping to group by the key. After the grouping you can then use the earlier LINQ code to get the average per key.


这篇关于需要过滤收到的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆