Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The value in the datain.txt are: -23.1280 -10.6711 -40.0527 19.2846 10.4012 -0.4

ID: 3698998 • Letter: T

Question

The value in the datain.txt are:

-23.1280 -10.6711 -40.0527 19.2846 10.4012 -0.4006 -0.6954 -15.9633 20.3737 -2.6643
-14.2906 27.0277 -4.4954 -11.7806 -5.8751 -16.9585 -22.4026 50.5200 33.1100 6.1507

The program should work with there or any other value fro datain.txt

Also, please show the output file seperatly.

Introduction In many engineering applications, created or measured data contains a signal of interest plus noise. The noise causes the data to vary in a random way (often dramatically) such that it becomes difficult to see trends in the signal. It is typically desirable to reduce the impact of noise on the signal by smoothing the data. Smoothing reduces large and short-term variations in the data There are many methods that may be used to smooth data. A simple one is to replace each data point with the median of three data points the data point of interest, the data point before it, and the data point after it. The median of a set of numbers is the "middle" number when the list of numbers is sorted in ascending order. In other words, it is the value above which half of the numbers lie and below which the other half of the numbers lie. For example, the median of the three numbers 8, 1, 2 is 2. Note that the median is different than the average (or mean) To smooth a set of numbers using the median method, a "window" three numbers wide is "slid" across the list of numbers. The number in the centre of the window is replaced with the median of the three numbers in the window. For example, below is a list of "noisy" numbers. The lines above and below the list represent windows three numbers wide. Above (or below) each window is the median value for the three numbers in that window. If the process is repeated throughout the list, the result is the list of smoothed numbers shown below the list of noisy numbers Noisy List 3 -5 26 7-6 7 9 2 1 -5 -7 Smoothed List 3 2 -5 2 7 -6 6 7 7 2 1 Notes: This method requires that the list contain a minimum of three numbers The list may contain an even or odd number of numbers The first and last numbers in the list are unchanged - For this lab a smoothed number will NOT be used to smooth the next number in the list. In other words, only original (noisy) numbers are used when calculating median values

Explanation / Answer


Given below is the code for the question. I tested for the small data where the output is shown and the output generated matches the expected output.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
using namespace std;
int readFile(string filename, double noisy[]);
double median(double noisy[], int n, int index);
double average(double a[], int n);
double stdDeviation(double a[], int n);
void smooth(double noisy[], double smoothed[], int n);
void writeOutput(string filename, double noisy[], int n);
int main()
{
string infilename, outfilename;
double noisy[100];
int n;
cout << "Enter input filename: ";
cin >> infilename;
cout << "Enter output filename: ";
cin >> outfilename;
n = readFile(infilename, noisy);
writeOutput(outfilename, noisy, n);
}

int readFile(string filename, double noisy[])
{
ifstream infile(filename.c_str());
if(!infile.is_open())
{
cout << "ERROR: could not open input file " << filename << endl;
exit(1);
}
int n = 0;
while( infile >> noisy[n])
n++;
infile.close();
return n;
}
double median(double noisy[], int n, int index)
{
double first, second;
if(noisy[index] < noisy[index+1])
{
first = noisy[index];
second = noisy[index + 1];
}
else
{
first = noisy[index + 1];
second = noisy[index];
}
if(noisy[index + 2] > second)
return second;
else
{
if(noisy[index + 2] > first)
return noisy[index + 2];
else
return first;
}
}
double average(double a[], int n)
{
double avg = 0;
for(int i = 0; i < n; i++)
avg += a[i];
if(n != 0)
avg /= n;
return avg;
}

double stdDeviation(double a[], int n)
{
double avg = average(a, n);
double sum = 0;
double diff;
for(int i = 0; i < n; i++)
{
diff = a[i] - avg;
sum += diff * diff;
}
sum /= (n-1);
return sqrt(sum);
}
void smooth(double noisy[], double smoothed[], int n)
{
smoothed[0] = noisy[0];
smoothed[n-1] = noisy[n-1];
for(int i = 1; i < n-1; i++)
{
smoothed[i] = median(noisy, n, i-1);
}
}

void writeOutput(string filename, double noisy[], int n)
{
double smoothed[100];
ofstream outfile(filename.c_str());
if(outfile.fail())
{
cout << "ERROR: could not open output file " << filename << endl;
exit(1);
}
smooth(noisy, smoothed, n);
for(int i = 0; i < n; i++)
outfile << setw(10) << noisy[i] << setw(10) << smoothed[i] << endl;
outfile << endl;
outfile << fixed << setprecision(2) ;
outfile << "noisy average = " << average(noisy, n) << endl;
outfile << "smooth average = " << average(smoothed, n) << endl;
outfile << "noisy standard deviation = " << stdDeviation(noisy, n) << endl;
outfile << "smoothed standard deviation = " << stdDeviation(smoothed, n) << endl;
outfile.close();
cout << "output written to file " << filename << endl;
}


output
=====
Enter input filename: data1.txt
Enter output filename: data1-out.txt
output written to file data1-out.txt
sample input file: data1.txt
========
1.10
0.33
-6.80
3.84
4.9

output file: data1-out.txt
=======
1.1 1.1
0.33 0.33
-6.8 0.33
3.84 3.84
4.9 4.9
noisy average = 0.67
smooth average = 2.10
noisy standard deviation = 4.58
smoothed standard deviation = 2.13