• 一种获得离散型周期数据的变化周期的算法


    400个数据像这样:
    152 155 155 237 24 27 27 109 152 155 155 237 24 27 27 109 152 155 155 237 24 27 27 109 152
    155 155 237 24 27 27 109 152 155 155 237 24 27 27 109 152 155 155 237 24 27 27 109 152 155
    155 237 24 27 27 109 152 155 155 237 24 27 27 109 152 155 155 237 24 27 27 109 152 155 155
    237 24 27 27 109 152 155 155 237 24 27 27 109 152 155 152 237 24 27 27 109 152 155 155 237
    24 27 27 109 152 155 155 237 24 27 27 109 152 155 155 237 24 27 27 109 152 155 155 237 24
    27 27 109 152 155 155 237 24 27 27 109 152 155 155 237 24 27 27 109 152 155 155 237 24 27
    27 109 152 155 155 237 24 27 27 109 152 155 155 237 24 27 27 109 152 155 155 237 24 27 27
    109 152 155 152 237 24 27 27 109 152 155 155 237 24 27 27 109 152 155 155 237 24 27 27 109
    152 155 155 237 24 27 27 109 152 155 155 237 24 27 27 98 152 152 155 237 24 27 27 109 152
    155 155 237 24 27 27 98 152 155 155 237 24 27 27 109 152 152 155 226 24 27 27 98 152 152
    155 226 24 27 27 98 152 152 155 226 24 24 27 98 152 152 155 226 24 24 27 98 152 152 155
    226 24 24 27 98 152 152 155 226 24 24 27 98 152 152 155 226 24 27 27 98 152 152 155 226
    24 27 27 98 152 155 155 226 24 27 27 98 152 155 155 226 24 27 27 98 152 155 155 226 24
    27 27 98 152 155 155 226 24 27 27 98 152 152 155 226 24 24 27 98 152 152 155 226 24 24
    27 98 152 152 155 226 24 24 27 98 152 152 155 226 24 24 27 98 152 152 155 226 24 24 27
    98 152 152 155 226 24 24 27 98 152 152 155 226 24 27 27 98 152 152 155 226 24 27 27 98
    曲线像这样:
    在这里插入图片描述
    以上数据的周期是8。

    问,怎么求该离散型数据的变化周期?
    答案:迭代,方差。
    算法如下:

    #include 
    #include   
    #include 
    #include 
    #include 
    using namespace std;
    #define ARRAYLEN 24
    #define TOTAL_SAMPLES 400
    int main(){
    	int samples_Data[TOTAL_SAMPLES];
    	ifstream infile("test.txt");
    	string data;
    	int count_samples_Data=0;
    	while (getline(infile, data)) {
    		//cout<< data << endl;
    		//string str = "92 20 120.7";
    		int num1 = 0, num2 = 0, num3 = 0,num4 = 0, num5 = 0, num6 = 0,num7 = 0, num8 = 0, num9 = 0,num10 = 0, num11 = 0, num12 = 0,
    			num13 = 0, num14 = 0, num15 = 0,num16 = 0, num17 = 0, num18 = 0,num19 = 0, num20 = 0, num21 = 0,num22 = 0, num23 = 0, num24 = 0,num25 = 0;
    		istringstream string_to_num(data);
    		string_to_num >>num1 >> num2 >> num3 >>num4 >> num5 >> num6 >>num7 >> num8 >> num9 >>num10 >> num11 >> num12 >>
    			num13 >> num14 >> num15 >>num16 >> num17 >> num18 >>num19 >> num20 >> num21 >>num22 >> num23 >> num24 >>num25 ;
    
    		samples_Data[count_samples_Data]=num1;samples_Data[count_samples_Data+1]=num2;samples_Data[count_samples_Data+2]=num3;samples_Data[count_samples_Data+3]=num4;samples_Data[count_samples_Data+4]=num5;
    		samples_Data[count_samples_Data+5]=num6;samples_Data[count_samples_Data+6]=num7;samples_Data[count_samples_Data+7]=num8;samples_Data[count_samples_Data+8]=num9;samples_Data[count_samples_Data+9]=num10;
    		samples_Data[count_samples_Data+10]=num11;samples_Data[count_samples_Data+11]=num12;samples_Data[count_samples_Data+12]=num13;samples_Data[count_samples_Data+13]=num14;samples_Data[count_samples_Data+14]=num15;
    		samples_Data[count_samples_Data+15]=num16;samples_Data[count_samples_Data+16]=num17;samples_Data[count_samples_Data+17]=num18;samples_Data[count_samples_Data+18]=num19;samples_Data[count_samples_Data+19]=num20;
    		samples_Data[count_samples_Data+20]=num21;samples_Data[count_samples_Data+21]=num22;samples_Data[count_samples_Data+22]=num23;samples_Data[count_samples_Data+23]=num24;samples_Data[count_samples_Data+24]=num25;
    
    		count_samples_Data+=25;
    
    		//cout << num1 <<" "<< num2 <<" "<< num3 <<" "<
    		//num13 <<" "<< num14 <<" "<< num15 <<" "<
    
    	}
    
    
    
    	int omiga=0;
    	int startIndex=0;
    	int tempSum=0.0;
    	int loopT=0;
    	double tempAvg=0.0;
    	double fangcha=0.0;
    	unsigned char datas[5];
    	double fangchaArray[ARRAYLEN]={0.0};
    	int count=0;
    	for (omiga=0;omiga<ARRAYLEN;omiga++	)
    	{
    		count=0;
    		for (count =0 ;count<5;count++)
    		{
    			datas[count]=samples_Data[startIndex+count*(omiga+1)];
    		}
    		//计算datas 五个数据的方差
    		tempSum=0.0;
    		for (count =0 ;count<5;count++)
    		{
    			tempSum+=datas[count];
    		}
    		tempAvg=(double)(tempSum/5);
    		tempSum=0.0;
    		for (count =0 ;count<5;count++)
    		{
    			tempSum+=(datas[count]-tempAvg)*(datas[count]-tempAvg);
    		}
    		fangcha=(double)(tempSum/5);
    		fangchaArray[omiga]=fangcha;
    	}
    	fangcha=fangchaArray[0];
    	double preDaoShu=0.0;
    	double postDaoShu=0.0;
    	for (omiga=0;omiga<ARRAYLEN;omiga++	){
    		if (fangcha>fangchaArray[omiga])
    		{
    			fangcha=fangchaArray[omiga];
    			loopT=omiga+1;
    		}
    	}
    	cout<<"loopT:  "<<loopT<<endl;
    	
    	system("pause");
    	return 0;
    }
    
    /*
    C++中引入了 istringstream、ostringstream、stringstream这三个类来进行不同类型数据的转换,要使用它们创建对象就必须包含  这个头文件。
    
    istringstream类用于执行C++风格的串流的输入操作。
    istringstream对象可以绑定一行字符串,然后以空格为分隔符把该行分隔开来。在读取数据文件时可以按行读取数据并存储到字符串中,然后通过istringstream字符串分割开,并存储到相应类型的变量中。语法格式如下:
    
    istringstream::istringstream(string str);
    1
    将一个字符串中数字分割开并赋值给double类型的变量:
    
    string str = "92 20 120.7";
    double num1 = 0, num2 = 0, num3 = 0;
    istringstream string_to_num(str);
    string_to_num >> num1 >> num2 >> num3 ;
    cout << num1 << endl << num2 << endl << num3 ;
    ————————————————
    版权声明:本文为CSDN博主「还是要努力呀!」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_33021529/article/details/115462166
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103

    输出结果:
    loopT: 8

  • 相关阅读:
    【前端实例代码】如何在HTML CSS和JavaScript|中使用正则表达式RegExp校验email电子邮件验证
    electron前端开启局域网接口
    Spring整合Junit单元测试
    流畅的Python读书笔记-第九章-符合Python风格的对象
    [netty-websocket-boot-starter] 轻量、高性能的WebSocket框架
    在字符串两侧填充指定字符ljust()与rjust()方法
    这个需求怎么做java面试题
    如何在公司内部统一「数据指标口径」?_光点科技
    机器学习-数学基础
    Squid代理服务器应用
  • 原文地址:https://blog.csdn.net/m0_47472749/article/details/128041553