趋势平滑

原始数据为几分txt,里面是采集的浮点型数据,用于绘制趋势图,按小时为单位导出,每小时大概18万个点。

在这里插入图片描述
在这里插入图片描述
绘制趋势时,需要将几个小时的采样点都提取出来,并且需要进行平滑处理(存在部分噪点数据,通过平均处理,将特别高或者特别低的数据对趋势图的影响降低,平滑趋势)最终输出一个又均值组成的txt。


package com.fpi.text;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @ClassName: App
 * @Description:合并TXT
 * @author: luchenxi 18163
 * @date: 2020年7月21日 下午3:46:12
 * @Copyright: FPI
 */
public class App
{
	public static void main(String[] args)
	{
		try
		{
			// 静态量记录时间
			//float index = 0;
			App app = new App();
			// 获取当前目录
			String path = app.getCourrentPath();
			// 创建新文件
			File file = new File(path + "/all.txt");
			if(!file.exists())
			{
				file.createNewFile();
			}
			else
			{
				file.delete();
				file.createNewFile();
			}
			// 获取文件夹目录
			String txtFileDir = path + "/target";
			// 获取目标目录下所有文件
			File txtDir = new File(txtFileDir);
			if(txtDir.exists())
			{
				File[] tFiles = txtDir.listFiles();
				for(int i = 0 ; i < tFiles.length ; i++)
				{
					System.out.println(tFiles[i].getName() + " ...");
					List<String> contents = App.readFileContent(tFiles[i]);
					double value = 0;
					for(int j = 0 ; j < contents.size() ; j++)
					{
						if(j > 0 && j % 50 == 0)
						{
							double finalval = (value / 50);
							String line = String.valueOf(finalval);
							App.writeData(line, file);
							value = 0;
						}
						value = Double.valueOf(contents.get(j)) + value;
					}
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Title: getCourrentPath @Description: 当前路径 @param: @return @return:
	 *         String @throws
	 */
	public String getCourrentPath()
	{
		String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
		int firstIndex = path.lastIndexOf("/") + 1;
		path = path.substring(1, firstIndex);
		return path;
	}

	/**
	 * 
	 * @Title: readFileContent @Description: 读文件 @param: @param
	 *         file @param: @return @return: List<String> @throws
	 */
	public static List<String> readFileContent(File file)
	{
		BufferedReader reader = null;
		List<String> contents = new ArrayList<String>();
		try
		{
			reader = new BufferedReader(new FileReader(file));
			String tempStr;
			while((tempStr = reader.readLine()) != null)
			{
				String[] tabs = tempStr.split("	");
				contents.add(tabs[1]);
			}
			reader.close();
			return contents;
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(reader != null)
			{
				try
				{
					reader.close();
				}
				catch (IOException e1)
				{
					e1.printStackTrace();
				}
			}
		}
		return contents;
	}

	public float nextIndex(float index)
	{
		float add = 0.02f;
		BigDecimal b1 = new BigDecimal(Float.toString(index));
		BigDecimal b2 = new BigDecimal(Float.toString(add));
		return b1.add(b2).floatValue();
	}

	public static void writeData(String content , File file)
	{
		content = content + "\r\n";
		try
		{
			FileOutputStream fos = new FileOutputStream(file, true);
			fos.write(content.getBytes());
			fos.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值