/*phase coherence analysis*/ private void phaseCoherenceFuntion() { Console.WriteLine("Running phaseCohenrence..."); System.Diagnostics.Stopwatch watch2;; ///calc execution time long elapsedMs2 = 0; watch2 = System.Diagnostics.Stopwatch.StartNew(); ///calc execution time List <double> std_list = new List <double>(); //store std(s) List <double> norm_data = new List <double>(); //data after normalizing all its subsets List <double> raw_data = IOFuncs.readStreamFile(txt_data_to_calc_W.Text); int data_len = raw_data.Count; int K_MAX = Convert.ToInt16(txt_period_max.Text); for (int k = 1; k <= K_MAX; k++)//do calc std for each value of 'k' { //reset variables: norm_data.Clear(); double[] sum_subsets = new double[k]; //store k sums double[] mean_subsets = new double[k]; //store k means int len_average_subset = data_len / k; int remainder_len_subset = data_len % k; /*calc mean for each subsets:*/ //find the sum(s): for (int j = 0; j < data_len; j++) { sum_subsets[j % k] += raw_data[j]; } //calc mean(s): for (int i = 0; i < k; i++) { if (i < remainder_len_subset) { mean_subsets[i] = sum_subsets[i] / (len_average_subset + 1); } else { mean_subsets[i] = sum_subsets[i] / len_average_subset; } } for (int i = 0; i < data_len; i++) { norm_data.Add(raw_data[i] - mean_subsets[i % k]); } //calc std then append it to std_list std_list.Add(MathFuncs.CalcStdMeanZero(norm_data)); }//end for double min = std_list.Min(); int period = std_list.FindIndex(n => Math.Abs(n - min) < 0.0000001); watch2.Stop(); elapsedMs2 = watch2.ElapsedMilliseconds; //print file to check: IOFuncs.WriteFile_2(std_list, "std_list.csv"); txt_period.Text = period.ToString(); Console.WriteLine("DONE CALC PERIOD_ REF 7, time = " + elapsedMs2 + "; period = " + period); }