/* */ /* This starts the Analysis part of the DVM code. */ /* */ /* Rev. Date 23-Dec-2020 */ /* */ /* */ /* The First Analysis section finds the minimum and maximum */ /* samples in the data set that was just collected. */ /* */ /* The minimum and maximum sample values and their */ /* sample index numbers are displayed. */ /* */ /* Recall that the samples are stored in an array called */ /* DVM_Reads[]. This is a "double" floating point array. */ /* */ /* In C this array starts at index = 0 but in this */ /* application the first DVM Reading is stored at index = 1. */ /* Thus if you have told this program to collect 10 readings */ /* they will be stored in the DVM_Reads[] 1 through 10. */ /* */ /* The number of samples in the DVM_Reads[] array is */ /* passed in the variable Sample_Index. */ /* */ int min_max_locate(double *DVM_Reads, int Number_of_Samples) { int min_sample_index = 1; double min_sample_value = 0.0; int max_sample_index = 1; double max_sample_value = 0.0; double min_spread_for_histogram = 0.10; /* Set the minimum min-to-max spread */ /* required for generating a histogram */ double min_to_max_spread = 0.0; double histo_increment = 0.0; double histo_cut_1 = 0.0; double histo_cut_2 = 0.0; double histo_cut_3 = 0.0; double histo_cut_4 = 0.0; double histo_cut_5 = 0.0; double histo_cut_6 = 0.0; double histo_cut_7 = 0.0; int count_bin_1 = 0; int count_bin_2 = 0; int count_bin_3 = 0; int count_bin_4 = 0; int count_bin_5 = 0; int count_bin_6 = 0; int count_bin_7 = 0; int count_bin_8 = 0; int Sample_Index = 1; min_sample_index = 1; min_sample_value = DVM_Reads[1]; max_sample_index = 1; max_sample_value = DVM_Reads[1]; while (Sample_Index <= Number_of_Samples) { if (DVM_Reads[Sample_Index] < min_sample_value) { min_sample_value = DVM_Reads[Sample_Index]; min_sample_index = Sample_Index; } if (DVM_Reads[Sample_Index] > max_sample_value) { max_sample_value = DVM_Reads[Sample_Index]; max_sample_index = Sample_Index; } ++Sample_Index; } printf("\n Minimum Sample Value = %7.4f", min_sample_value); printf("\n A Sample Index with the Minimum Sample Value = %d", min_sample_index); printf("\n\n Maximum Sample Value = %7.4f", max_sample_value); printf("\n A Sample Index with the Maximum Sample Value = %d", max_sample_index); printf("\n"); /* If the Min-Max Spread is big enough then Calculate an 8 bin historgram. */ /* */ /* The 8 bins in this histogram are equally spaced to cover the min-max spread. */ /* */ /* Except for the highest bin, incrementing the counter for a given bin */ /* requires that: low cut <= DVM_Reading < high_cut. */ /* */ /* For the highest bin, incrementing its counter */ /* requires that: low cut <= DVM_Reading <= high_cut. */ /* */ /* In this way the sum of the counts in the 8 histogram bins should */ /* always equal the total number of DVM_Readings that were collected. */ /* */ min_to_max_spread = max_sample_value - min_sample_value; if (min_to_max_spread > min_spread_for_histogram) { histo_increment = min_to_max_spread / 8.0; histo_cut_1 = min_sample_value + histo_increment; histo_cut_2 = histo_cut_1 + histo_increment; histo_cut_3 = histo_cut_2 + histo_increment; histo_cut_4 = histo_cut_3 + histo_increment; histo_cut_5 = histo_cut_4 + histo_increment; histo_cut_6 = histo_cut_5 + histo_increment; histo_cut_7 = histo_cut_6 + histo_increment; Sample_Index = 1; while (Sample_Index <= Number_of_Samples) { if ((min_sample_value <= DVM_Reads[Sample_Index]) && (DVM_Reads[Sample_Index] < histo_cut_1)) { ++ count_bin_1; } if ((histo_cut_1 <= DVM_Reads[Sample_Index]) && (DVM_Reads[Sample_Index] < histo_cut_2)) { ++ count_bin_2; } if ((histo_cut_2 <= DVM_Reads[Sample_Index]) && (DVM_Reads[Sample_Index] < histo_cut_3)) { ++ count_bin_3; } if ((histo_cut_3 <= DVM_Reads[Sample_Index]) && (DVM_Reads[Sample_Index] < histo_cut_4)) { ++ count_bin_4; } if ((histo_cut_4 <= DVM_Reads[Sample_Index]) && (DVM_Reads[Sample_Index] < histo_cut_5)) { ++ count_bin_5; } if ((histo_cut_5 <= DVM_Reads[Sample_Index]) && (DVM_Reads[Sample_Index] < histo_cut_6)) { ++ count_bin_6; } if ((histo_cut_6 <= DVM_Reads[Sample_Index]) && (DVM_Reads[Sample_Index] < histo_cut_7)) { ++ count_bin_7; } if ((histo_cut_7 <= DVM_Reads[Sample_Index]) && (DVM_Reads[Sample_Index] <= max_sample_value)) { ++ count_bin_8; } ++Sample_Index; } printf("\n Histo Bin from %7.4f to %7.4f has %4d counts", min_sample_value, histo_cut_1, count_bin_1); printf("\n Histo Bin from %7.4f to %7.4f has %4d counts", histo_cut_1, histo_cut_2, count_bin_2); printf("\n Histo Bin from %7.4f to %7.4f has %4d counts", histo_cut_2, histo_cut_3, count_bin_3); printf("\n Histo Bin from %7.4f to %7.4f has %4d counts", histo_cut_3, histo_cut_4, count_bin_4); printf("\n Histo Bin from %7.4f to %7.4f has %4d counts", histo_cut_4, histo_cut_5, count_bin_5); printf("\n Histo Bin from %7.4f to %7.4f has %4d counts", histo_cut_5, histo_cut_6, count_bin_6); printf("\n Histo Bin from %7.4f to %7.4f has %4d counts", histo_cut_6, histo_cut_7, count_bin_7); printf("\n Histo Bin from %7.4f to %7.4f has %4d counts", histo_cut_7, max_sample_value, count_bin_8); printf("\n"); } return 0; } /* */ /* The Second Analysis section finds the average or DC */ /* value, the DC RMS value, and the AC RMS value of all */ /* of the DVM samples that have been collected */ /* */ int calc_DC_and_RMSs(double *DVM_Reads, int Number_of_Samples, double *pt_This_DC_Measurement, double *pt_This_RMS_Measurement) { double total_sum = 0.0; double avg_of_sum = 0.0; double dc_rms = 0.0; double ac_rms = 0.0; double ac_content = 0.0; int Sample_Index = 1; /* Calculate the Average or DC value of the samples. */ while (Sample_Index <= Number_of_Samples) { total_sum = total_sum + DVM_Reads[Sample_Index]; ++Sample_Index; } avg_of_sum = total_sum / Number_of_Samples; printf("\n The average, aka DC, sample value = %8.5f", avg_of_sum); /* Calculate the DC RMS value of the samples. */ Sample_Index = 1; total_sum = 0.0; while (Sample_Index <= Number_of_Samples) { total_sum = total_sum + (DVM_Reads[Sample_Index] * DVM_Reads[Sample_Index]); ++Sample_Index; } dc_rms = sqrt(total_sum / Number_of_Samples); printf("\n\n The DC RMS of the samples = %8.5f", dc_rms); /* Calculate the AC RMS value of the samples. */ Sample_Index = 1; total_sum = 0.0; while (Sample_Index <= Number_of_Samples) { ac_content = DVM_Reads[Sample_Index] - avg_of_sum; total_sum = total_sum + (ac_content * ac_content); ++Sample_Index; } ac_rms = sqrt(total_sum / Number_of_Samples); printf("\n\n The AC RMS of the samples = %8.5f", ac_rms); printf("\n\n"); /* Now write the DC Value, and AC RMS Value */ /* to the variables that will carry these values */ /* back to the calling routine. */ *pt_This_DC_Measurement = avg_of_sum; *pt_This_RMS_Measurement = ac_rms; return 0; }