public void smooth_data() { int i = 0; int start = 0; while (i < current_in_index) { if (!(Double.IsNaN(Storage[i]))) { i++; } else // here is a NAN delimiter { double[] source = new double[i - start]; double[] smoothed = new double[source.Length]; // copy data for (int j = 0; j < source.Length; j++) { source[j] = Storage[j + start]; } Smoother_cls.smooth_double_array(source, ref smoothed); //copy data for (int j = 0; j < smoothed.Length; j++) { Smoothed_Storage[j + start] = smoothed[j]; } Smoothed_Storage[i] = Double.NaN; i++; start = i; } } }
private void calculate_mean_cycle() { Storage.smooth_data(); // calculate length of mean cycle Base_length_value = metronome.period_ms / 25; // 40 Hz = 25 mSec period int i = 0; int cycles_counter = 0; list_of_cycles = new List <single_cycle_cls>(); while (i < Storage.current_in_index) { if (!(Double.IsNaN(Storage.get_data(i)))) { i++; } else // here is a NAN delimiter { i++; cycles_counter++; list_of_cycles.Add(new single_cycle_cls()); list_of_cycles.Last().start_index = i; list_of_cycles.Last().length = 0; while ((i < Storage.current_in_index) && (!(Double.IsNaN(Storage.get_data(i))))) { i++; list_of_cycles.Last().length++; } } } // assemble array of mean cycle mean_cycle_buffer = new double[Base_length_value]; mean_filtered_cycle_buffer = new double[Base_length_value]; int good_cycles_counter = 0; foreach (single_cycle_cls item in list_of_cycles) { if (Math.Abs(item.length - Base_length_value) <= 1) { // add this cycle to mean for (int j = 0; j < Math.Min(item.length, Base_length_value); j++) { mean_cycle_buffer[j] += Storage.get_data(item.start_index + j); } if (item.length < Base_length_value) { mean_cycle_buffer[Base_length_value - 1] = mean_cycle_buffer[Base_length_value - 2]; } good_cycles_counter++; } } // calculate mean if (good_cycles_counter > 0) { for (int j = 0; j < Base_length_value; j++) { mean_cycle_buffer[j] /= good_cycles_counter; } } //************************* simple mean ****************************************************** // create list of j-th elements of cycles List <double> j_slice = new List <double>(); // assemble array of filtered mean cycle for (int j = 0; j < Base_length_value; j++) { // populate list of j-th elements of cycles foreach (single_cycle_cls item in list_of_cycles) { if (j <= item.length) { //create new item of list j_slice.Add(Storage.get_smoothed_data(item.start_index + j)); } } // create auxiliary array (for sorting) double[] sorted_array = new double[j_slice.Count]; int k = 0; foreach (double item in j_slice) { sorted_array[k] = item; k++; } j_slice.Clear(); // sort array bubble_sorting(ref sorted_array); // calculate mean mean_filtered_cycle_buffer[j] = 0; int counter = 0; // drop 2 maxes and 2 mines if (sorted_array.Length >= 5) { for (int m = 2; m < (sorted_array.Length - 2); m++) { mean_filtered_cycle_buffer[j] += sorted_array[m]; counter++; } mean_filtered_cycle_buffer[j] /= counter; } else if (sorted_array.Length >= 1) { for (int m = 0; m < sorted_array.Length; m++) { mean_filtered_cycle_buffer[j] += sorted_array[m]; counter++; } mean_filtered_cycle_buffer[j] /= counter; } } // smooth filtered mean array *********************** smoothed_cycle_buffer = new double[mean_filtered_cycle_buffer.Length]; Smoother_cls.smooth_double_array(mean_filtered_cycle_buffer, ref smoothed_cycle_buffer); }// end private void calculate_mean_cycle()