Hi guys I am writing two programmmes at the moment, will post the other a bit later. the first is a smoothing programme i.e. it smooths a given set of data using 5-point smoothing. I have managed some of it but it keeps giving me errors. I ahve a very basic knowledge of programming so any and all help will be greatly appreciated!
Many thanks
Tasks: 1. Define a function in C, with the declaration
void Filter5pt( float input[], float output[],
unsigned numpts, float a[]);
that will apply a 5-point linear filter to the values held in the array input, storing the results in the array output. The array a is a 5-element array of filter coefficients. The first two elements and the last two elements of the output data should be the same as the first two and last two elements of the input data, respectively, but every other element of output should be calculated according to the C expression
output[n] = (a[0]*input[n-2]) + (a[1]*input[n-1]) + (a[2]*input[n])
+ (a[3]*input[n+1]) + (a[4]*input[n+2]);
2. A set of experimental data is stored as formatted values in a plain text file called data50.csv. The data consist of a list of 50 real values, stored as a single row in the file with a comma following every value in the list.
Write a main() program in C that will carry out the following tasks:
1. Read the data from the specified file into a one-dimensional array.
2. Use the function Filter5pt() to apply a 5-point moving-average filter to smooth the input data, placing the results into another array. (You will need to define within your program the appropriate filter coefficients for a 5-point moving-average filter.)
3. Apply a second filter to the resulting array of smoothed data, using a filter with coefficients
a[0] = a[2] = a[4] = 0; a[1] = -0.5; a[3] = 0.5;
These coefficients result in an output array with values that relate to the gradient of the input array.
4. Write the original input array, the array of smoothed data, and the array containing the results of the gradient filter operation to a new file named processed50.csv. The output should be formatted so that each array occupies a single row in the file, with commas following every value in each row. The values themselves should all be written to a precision of three decimal places.
3. The combination of two sucessive 5-point filters of the type used in the main() program above can be replaced by a single 7-point filter. Showing all the steps in your analysis, determine the coefficients for the single 7-point linear filter that will produce an effect that is the equivalent to the successive application of the moving-average and gradient filters used above, and include this (hand-written) analysis with the printed copy of your program listing. Comment briefly on whether the order in which the smoothing and gradient filters are applied to the data affects the final outcome of the two operations.
Code:#include <stdio.h> #include <math.h> void Filter5pt( float input[], float output[], unsigned numpts, float a[]); int main () { unsigned numpts = 50; float input[numpts]; float output[numpts]; float a[5]; a[0]=a[1]=a[2]=a[3]=a[4]=0.2; unsigned n; FILE *infile, *outfile; infile = fopen("data50.csv", "r"); for (n=0; n<(numpts); n++) { fscanf(infile, "%f,", &input[n]); } fclose (infile); Filter5pt ( input,output, numpts, a); return 0; } void Filter5pt( float input[], float output[], unsigned numpts, float a[]) { unsigned i; for (i=2; i<numpts-2; i++) { output[i]=(a[0]*input[i-2]+a[1]*input[i-1]+a[2]*input[i] +a[3]*input[i+1]+a[4]*input[i+2]); } output[0]=input[0]; output[1]=input[1]; output[numpts-1]=input[numpts-1]; output[numpts-2]=input[numpts-2]; }