请选择 进入手机版 | 继续访问电脑版
爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 2583|回复: 0

java实现数据平滑

[复制链接]

新浪微博达人勋

发表于 2018-2-1 14:20:42 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
public class DataSmooth {

        /**
         *
         * @Title: smoothValues
         * @Description: 进行数据平滑, 平滑为9宫格数据去除最高和最低之后取平均值。
         * @param input
         * @return
         */
        public static float[][] smoothValues(float[][] input) {
                float[][] values = new float[input.length][input[0].length];
                for (int i = 0; i < input.length; i++) {
                        for (int j = 0; j < input[0].length; j++) {
                                if (input[j] == Constants.DEFAULT_EMPTY_DATA) {
                                        values[j] = Constants.DEFAULT_EMPTY_DATA;
                                        continue;
                                }

                                float max = 999999;
                                float min = -999999;
                                float total = 0;
                                int count = 0;
                                for (int m = -1; m < 2; m++) {
                                        for (int n = -1; n < 2; n++) {
                                                if (isValid(input, i - m, j - n) && input[i - m][j - n] != Constants.DEFAULT_EMPTY_DATA) {
                                                        total += input[i - m][j - n];
                                                        max = buildMax(input[i - m][j - n], max);
                                                        min = buildMin(input[i - m][j - n], min);
                                                        count++;
                                                }
                                        }
                                }

                                if (count > 3) {
                                        total = total - max - min;
                                        values[j] = total / (count - 2);
                                } else if (count > 0) {
                                        values[j] = total / count;
                                } else {
                                        values[j] = Constants.DEFAULT_EMPTY_DATA;
                                }
                        }
                }
                return values;
        }

        private static boolean isValid(float[][] input, int m, int n) {
                if (m < 0 || m > input.length - 1) {
                        return false;
                }

                if (n < 0 || n > input[0].length - 1) {
                        return false;
                }
                return true;
        }

        private static float buildMax(float input, float max) {
                if (input > max || max == 999999) {
                        max = input;
                }
                return max;
        }

        private static float buildMin(float input, float min) {
                if (input < min || min == -999999) {
                        min = input;
                }
                return min;
        }
}

密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表