Thread: Smoothing programme

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Smoothing programme

    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];
    }
    Last edited by lasher_a2; 03-25-2010 at 08:06 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lasher_a2 View Post
    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!
    This is a classic example of this kind of question:

    There are two trains, train A and train B. Train A is red and left Chicago at 7:45am. Train B is yellow and left Des Moines at 9 o'clock. The weather is cloudy. There are 350 people on train A, including Dr.Schmitt, an eminent mathematician. Dr. Schmitt is travelling alone to California. Train B contains only freight, mostly wheat but also some chemical tankers, for a total of 158 cars.

    How fast is train A travelling?


    You include a ton of more or less irrelevant information (some of it, anyway), and then fail to describe the actual problem you are having, implying you want someone to test your code and find out for themselves based on the description.

    That may work, but it would be a lot better if you focussed on something specific rather than just cutting and pasting your assignment and the code you have so far. Ie, you need to organize your thoughts better, make an effort, and communicate effectively.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    ok, sorry if it came across as that mate, please excuse the mistake.

    Code:
    //declaring libraries
    #include <stdio.h>
    #include <math.h>
    
    void Filter5pt( float input[], float output[], unsigned numpts, float a[]);
    
    
    //start main program
    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;
    
    printf("1");
    
    FILE *infile,*endfile;
    
    
    infile=fopen("data50.csv", "r");
    printf("2");
    endfile=fopen("processed50.csv", "w");
    
    
        for (n=0; n<numpts; n++)
        {
    
            fscanf(infile, "%f,", &input[n]);
            fprintf(endfile, "%f,",input[n]);
        }
    
        fprintf(endfile,"\n");
    
        printf("3");
        fclose (infile);
    
        Filter5pt ( input,output, numpts, a);
    
    
    
    printf("4");
    
    
    
        for (n=0; n<numpts; n++)
    
        {
    
            fprintf(endfile, "%.3f,", output[n]);
    
            a[0] = a[2] = a[4] = 0; a[1] = -0.5; a[3] = 0.5;
    
            Filter5pt ( input,output, numpts, a);
    
            for (n=0; n<numpts; n++)
            fprintf(endfile, "%.3f,", output[n]);
    
        }
    
    return 0;
    }
    void Filter5pt( float input[], float output[], unsigned numpts, float a[])
    {
    unsigned i;
    printf("5");
        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];
    }
    I have since come up with this but need to get the endfile to print the output data on 3 seperate lines.

    Also, The answers i get with the coefficients is wrong.

    Is that any better? once again, sorry for my general lack of etiquette...

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lasher_a2 View Post
    Is that any better?
    Yes -- you identify what happens vs. what you expected (eg. "The answers i get with the coefficients is wrong"). Unfortunately it will probably be better if you wait for someone with more interest in math to help you with that one.

    Vis. the printing on separate lines, is this not just a matter of an appropriately placed '\n'? Show how it looks now, and how you would like it to look.

    sorry for my general lack of etiquette...
    Not so much an etiquette issue as one of simple logic (think how this will appear to someone who is not you).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're calling Filter5pt inside your for loop, when you probably only want to call it once. (I'm looking at that for-loop directly after you print "4".)

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by tabstop View Post
    You're calling Filter5pt inside your for loop, when you probably only want to call it once. (I'm looking at that for-loop directly after you print "4".)
    No... That's how a convolution is done.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    No... That's how a convolution is done.
    The convolution/filtering is already taken care of inside of the Filter5pt function. tabstop was saying that he was calling the Filter5pt function inside of a for loop where it appears it shouldn't be called.

    I actually can't really tell what he's trying to do inbetween after the printf("4").
    My Website

    "Circular logic is good because it is."

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by brewbuck View Post
    No... That's how a convolution is done.
    This is, of course, false. A convolution is done by, well, by Filter5pt for example (summing up products, where the two indices have a constant sum). Calling Filter5pt 50 times in a row, with the same input, overwriting the same output each time, does not actually help do anything.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    ha -- and actually I'm wrong, since Filter5pt is not called 50 times in a row -- the second for loop inside the first for loop, since they use the same variable, actually prevents the outer loop from looping. Hence the first number is printed out twice (once from the outer for loop, once from the inner) and there we are.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me with ma c programme..
    By eddie19 in forum C Programming
    Replies: 19
    Last Post: 08-12-2009, 07:53 AM
  2. Closing a programme with cin.get
    By Dontgiveup in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2009, 02:35 PM
  3. How to view the programme before it disappears
    By yousuf in forum C Programming
    Replies: 2
    Last Post: 03-22-2008, 08:12 AM
  4. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  5. Gui Programme does'nt Compiles from DOS
    By Shadowhunt in forum C++ Programming
    Replies: 1
    Last Post: 06-06-2003, 08:05 AM

Tags for this Thread