Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,448 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,169 people online right now. Registration is fast and FREE... Join Now!




Digital filter

 
Reply to this topicStart new topic

Digital filter, applying a simple digital filter to data

vasily1001
23 Nov, 2007 - 03:58 AM
Post #1

New D.I.C Head
*

Joined: 22 Nov, 2007
Posts: 4


My Contributions
im not sure if the code to apply the filter to the DataIn is accurate:----the whole program isnt running
there are errors within this piece of code
this is just a section of the whole program

ive attached the whole program




CODE
// Apply the filter to the input data and store in the filtered data structure
// Arguments:
//   (1) the structure of the filter to be applied
//   (2) the structure containing the data to be filtered
//   (3) the structure to hold the filtered data
// Returns: OK - if the filter is applied
//          FILTER_TOO_LONG - the filter is longer than the data

int ApplyFilter(TheFilter& Filter)
{  
  // return an error if the filter is longer than the data
if (Filter.Length > DataIn.Length) return FILTER_TOO_LONG;

   // initialise the data structure that holds the filtered data
    unsigned int Filter.Value;]
    int* pIntDataOut;

  // get memory for the filtered data
pIntDataOut = new int[DataOut.Length];  

  // apply the filter to the data
    int Filter;
        for(int i=0; i<=Filter; i++) {
        DataOut += (int) ApplyFilter[i]& DataIn[i];
   }
cout << "Filter has been applied" <<DataOut << endl;
}



Attached File(s)
Attached File  Document.rtf ( 8.61k ) Number of downloads: 28
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Digital Filter
23 Nov, 2007 - 08:10 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,864



Thanked: 53 times
Dream Kudos: 550
My Contributions
wow... that is some messed up source code. I don't understand exactly what everything is supposed to do, so I will attempt to help you fix the syntax errors, but you will need to work on the logic a bit.

first off is the function prototypes. The basic syntax is:
return-type function_name(argument_type optional_name, argument_type optional_name);

Although people like to leave out the optional name (the compiler does not require it so why put it there?) it is generally better to name the arguments since many development tools (like the IDE) will pick up any sort of in-context help from here.

so your prototypes should look something like:
CODE
// function prototypes
void EnterData(TheData& DataIn);
void EnterFilter(TheFilter& Filter);
int ApplyFilter(TheFilter& Filter, TheData& DataIn, TheData DataOut);
void DisplayData(TheFilter& Filter, TheData& DataIn, TheData& DataOut);


note that the ApplyFilter() returns a value, and takes in 2 new parameters. and that the order has been changed in DisplayData() -- these changes were based upon your useage in the main() function.

After this change your code will compile if the function definitions are commented out (main() is ofcourse not commented out). I don't know if the logic in main() is valid, but it does not seem to have any syntax errors, and seems to be ok.

so that gets us to here:
CODE
// Purpose
// A program to demonstrate the application of a simple digital filter
//
// Overview
// A sequence of data items and digital filter values need to be entered by the
// user. The application of the filter to the data involves a simple convolution
// operation. The filtered data are stored separately.
//
// Example
//  before filtering:
//   data_in = [0 1 3 6 3 1 0]
//   filter = [-0.5 1 -0.5]
//  after filtering:
//   data_out = [-0.5 -0.5 3 -0.5 -0.5]
//  where
//   data_out[0]=data_in[0]*filter[0]+data_in[1]*filter[1]+data_in[2]*filter[2]
//   data_out[1]=data_in[1]*filter[0]+data_in[2]*filter[1]+data_in[3]*filter[2]
//   data_out[2]=data_in[2]*filter[0]+data_in[3]*filter[1]+data_in[4]*filter[2]
//   data_out[3]=data_in[3]*filter[0]+data_in[4]*filter[1]+data_in[5]*filter[2]
//   data_out[4]=data_in[4]*filter[0]+data_in[5]*filter[1]+data_in[6]*filter[2]
//
// The program checks the following
//  1.The data and filter values must have been entered before the filter is
//    applied
//  2.The filter is not applied if the number of filter values is greater than
//    the number of input data values
//  3.The data and filter values must have been entered and the filter applied
//    before the filtered data can be displayed
#include <iostream>
using namespace std;

// the data values and the filter
struct TheFilter {
  double* Values;   // the filter values
  unsigned long Length;  // number of filter values
  bool Valid;   // true if the filter values have been Valid by the user
};

struct TheData {
  double* Values;  // holds the data to be filtered
  unsigned long Length;  // number of data values
  bool Valid;   // true if the data values have been Valid by the user
};

// function return values
enum {OK,FILTER_TOO_LONG};

// function prototypes
void EnterData(TheData& DataIn);
void EnterFilter(TheFilter& Filter);
int ApplyFilter(TheFilter& Filter, TheData& DataIn, TheData DataOut);
void DisplayData(TheFilter& Filter, TheData& DataIn, TheData& DataOut);


// Control the principal operations of the program
// Arguments: None
// Returns: 0 on completion
int main()
{
  // define the filter and its initial values
  TheFilter Filter = {0,0,false};

  // define the original data and its initial values
  TheData OriginalData = {0,0,false};

  // define the filtered data and its initial values
  TheData FilteredData = {0,0,false};

  char UserInput;

  // loop until the user wishes to exit
  while (1) {
    
    // show the menu of options
    cout << endl;
    cout << "Filter Menu" << endl;
    cout << "-----------" << endl;
    cout << "1. Enter data for filtering" << endl;
    cout << "2. Enter filter values" << endl;
    cout << "3. Apply filter" << endl;
    cout << "4. Display filtered data" << endl;
    cout << "5. Exit from the program" << endl << endl;
    
    // get the user's choice
    cout << "Enter your option: ";
    cin >> UserInput;
    cout << endl;
    
    // act on the user's input
    switch(UserInput) {
      case '1':
        EnterData(OriginalData);
        FilteredData.Valid = false;
        break;

      case '2':
        EnterFilter(Filter);
        FilteredData.Valid = false;
        break;      

      case '3':
        if (Filter.Valid == true && OriginalData.Valid == true && FilteredData.Valid == false) {
          if (ApplyFilter(Filter,OriginalData,FilteredData) == FILTER_TOO_LONG) {
            cout << "The filter must not be longer than the data" << endl;
          }
          else {
            FilteredData.Valid = true;
            cout << "Filter applied" << endl;
          }
        }
        break;

      case '4':
        if (Filter.Valid == true && OriginalData.Valid == true && FilteredData.Valid == true) {
          DisplayData(Filter,OriginalData,FilteredData);
        }
    else {
      cout << "Data have not yet been filtered" << endl;
    }
        break;

      case '5':
        delete [] Filter.Values;
        delete [] OriginalData.Values;
        delete [] FilteredData.Values;
        return 0;
        break;

      default:
        cout << "Invalid entry" << endl << endl;
        break;
    }
  }
}


the function EnterData() seems to have a function defined within a function. I am just not sure what you were trying to do with that. You have this line: unsigned int DataIn.Values; which tries to define a member of DataIn... which surely MUST be a syntax error. and you use an undeclared variable (pIntOriginalData) which is probably supposed to be the unused (but declared) variable (pIntDataIn).

Ok... seriously you need to find a C/C++ text book or read up on some beginner tutorials. This one function demonstrates a serious lack of understanding of the basics of the language.

lets see if we can't clean this function up... the idea (I take it) is to have the user tell you how much data they wish to enter, and then take in series of values and store them into an array:
CODE
//give the user 3 ties to get the input right
#define BAD_INPUT_CUTOFF 3
//Need to add the following two lines to the included:
//#include <limits>  
//#include <ios>
void EnterData(TheData& DataIn)
{
    int bad_count = 0; //lets us see how many time the user entered bad data

    //Ask the user how many value she plans on entering:
    do
    {
        cout << "Enter the number of data values to be filtered(1-100000): ";
        cin >> DataIn.Length;
        if (!cin.good() || DataIn.Length <= 0 || DataIn.Length > 100000) {
            cout << "\nInvalid Input!" << endl;
            cin.clear(); //resets the state flag
            cin.ignore(numeric_limits<streamsize>::max(),'\n'); //clears out the buffer
            bad_count++;
            DataIn.Length = 0; // user did not enter a valid number
        }
        //See if this is a valid number (users are mean/stupid sometimes)
        //Note that we don't display any error message
    } while (DataIn.Length == 0 && bad_count < BAD_INPUT_CUTOFF);

    
    // make an array to hold the data:
    DataIn.Values = new double[DataIn.Length];
    //Note that even if we allocated a length of 0 we will still be calling delete[]

    if (bad_count < BAD_INPUT_CUTOFF)
    {
        cout << "Enter in the data:" << endl;
        int i = 0; //our index into the Values array
        int input; //the user's input
        
        while (i < DataIn.Length && bad_count < BAD_INPUT_CUTOFF)
        {
            cout << "element " << i << ": ";
            cin >> input;
            if (!cin.good()) {
                cout << "\nInvalid input!\nPlease input a valid floating point number!" <<endl;
                //the user did not input a number, so we need to clear out the input buffer
                cin.clear(); //resets the state flag
                cin.ignore(numeric_limits<streamsize>::max(),'\n'); //clears out the buffer
                bad_count++;
            } else {
                DataIn.Values[i]=input;
                i++; //user entered data move to the next element
                bad_count = 0; //reset the bad input counter
            }
        }
    }
    //check to make sure the input was valid...
    if (bad_count >= BAD_INPUT_CUTOFF) {
        cout << "\nSorry, invalid input, input loop terminated!" << endl;
        DataIn.Valid = false;
    } else {
        DataIn.Valid = true;
    }
}


that should work as your EnterData() function and server as a model for your EnterFilter() function.
User is offlineProfile CardPM
+Quote Post

vasily1001
RE: Digital Filter
25 Nov, 2007 - 01:07 PM
Post #3

New D.I.C Head
*

Joined: 22 Nov, 2007
Posts: 4


My Contributions
Thanks a lot, i sent u an older version of the code i hadnt already edited..sorry about that..Ive cleaned up most of it, and removed most of the parts i learned are irrelevant to this particular piece. im now just getting a couple of syntax errors i dnt understand regarding the function definitions

the entire code below
CODE
// Purpose
// A program to demonstrate the application of a simple digital filter
//
// Overview
// A sequence of data items and digital filter values need to be entered by the
// user. The application of the filter to the data involves a simple convolution
// operation. The filtered data are stored separately.
//
// Example
//  before filtering:
//   data_in = [0 1 3 6 3 1 0]
//   filter = [-0.5 1 -0.5]
//  after filtering:
//   data_out = [-0.5 -0.5 3 -0.5 -0.5]
//  where
//   data_out[0]=data_in[0]*filter[0]+data_in[1]*filter[1]+data_in[2]*filter[2]
//   data_out[1]=data_in[1]*filter[0]+data_in[2]*filter[1]+data_in[3]*filter[2]
//   data_out[2]=data_in[2]*filter[0]+data_in[3]*filter[1]+data_in[4]*filter[2]
//   data_out[3]=data_in[3]*filter[0]+data_in[4]*filter[1]+data_in[5]*filter[2]
//   data_out[4]=data_in[4]*filter[0]+data_in[5]*filter[1]+data_in[6]*filter[2]
//
// The program checks the following
//  1.The data and filter values must have been entered before the filter is
//    applied
//  2.The filter is not applied if the number of filter values is greater than
//    the number of input data values
//  3.The data and filter values must have been entered and the filter applied
//    before the filtered data can be displayed
#include <iostream>
using namespace std;

// the data values and the filter
struct TheFilter {
  double* Values;   // the filter values
  unsigned long Length;  // number of filter values
  bool Valid;   // true if the filter values have been Valid by the user
};

struct TheData {
  double* Values;  // holds the data to be filtered
  unsigned long Length;  // number of data values
  bool Valid;   // true if the data values have been Valid by the user
};

// function return values
enum {OK,FILTER_TOO_LONG};

// function prototypes
void EnterData(TheData& DataIn);
void EnterFilter(TheFilter& Filter);
int ApplyFilter(TheFilter& Filter, TheData& DataIn, TheData& DataOut);
void DisplayData(TheFilter& Filter, TheData& DataIn, TheData& DataOut);



// Control the principal operations of the program
// Arguments: None
// Returns: 0 on completion
int main()
{
  // define the filter and its initial values
  TheFilter Filter = {0,0,false};

  // define the original data and its initial values
  TheData OriginalData = {0,0,false};

  // define the filtered data and its initial values
  TheData FilteredData = {0,0,false};

  char UserInput;

  // loop until the user wishes to exit
  while (1) {
    
    // show the menu of options
    cout << endl;
    cout << "Filter Menu" << endl;
    cout << "-----------" << endl;
    cout << "1. Enter data for filtering" << endl;
    cout << "2. Enter filter values" << endl;
    cout << "3. Apply filter" << endl;
    cout << "4. Display filtered data" << endl;
    cout << "5. Exit from the program" << endl << endl;
    
    // get the user's choice
    cout << "Enter your option: ";
    cin >> UserInput;
    cout << endl;
    
    // act on the user's input
    switch(UserInput) {
      case '1':
        EnterData(OriginalData);
        FilteredData.Valid = false;
        break;

      case '2':
        EnterFilter(Filter);
        FilteredData.Valid = false;
        break;      

      case '3':
        if (Filter.Valid == true && OriginalData.Valid == true && FilteredData.Valid == false) {
          if (ApplyFilter(Filter,OriginalData,FilteredData) == FILTER_TOO_LONG) {
            cout << "The filter must not be longer than the data" << endl;
          }
          else {
            FilteredData.Valid = true;
            cout << "Filter applied" << endl;
          }
        }
        break;

      case '4':
        if (Filter.Valid == true && OriginalData.Valid == true && FilteredData.Valid == true) {
          DisplayData(Filter,OriginalData,FilteredData);
        }
    else {
      cout << "Data have not yet been filtered" << endl;
    }
        break;

      case '5':
        delete [] Filter.Values;
        delete [] OriginalData.Values;
        delete [] FilteredData.Values;
        return 0;
        break;

      default:
        cout << "Invalid entry" << endl << endl;
        break;
    }
  }
}

// Allow the user to enter the data to be filtered
// Arguments:
//   (1) the structure containing the input data    
// Returns: nothing

void EnterData(TheData &DataIn)
    
{  
    // initialise the data structure that holds the data to be filtered, including getting
    // the number of data values from the user
  
    
    cout << "Enter the number of data to be filtered" << endl;
    cin >> DataIn.Length;

    // allocate memory to the data
    
    DataIn.Values = new double[DataIn.Length];
    

    // obtain all of the data values

    {
        for (int count=0; count<DataIn.Length; count++)
        {
            cin >> DataIn.Values[count];
        }
    }
}


// Allow the user to enter the filter values
// Arguments:
//   (1) the structure of the filter to be defined
// Returns: nothing

void EnterFilter(TheFilter &Filter)

{  
  // initialise the data structure that holds the filter, including getting the number of
  // filter values from the user
    

    cout << "Enter the number of filter values" << endl;
    cin >> Filter.Length;

  
     // allocate memory to the filter values
      Filter.Values = new double[Filter.Length];

    
    // obtain all of the filter values
   {
        for (int count=0; count<Filter.Length; count++)
        {
            cin >> Filter.Values[count];
        }

   }


// Apply the filter to the input data and store in the filtered data structure
// Arguments:
//   (1) the structure of the filter to be applied
//   (2) the structure containing the data to be filtered
//   (3) the structure to hold the filtered data
// Returns: OK - if the filter is applied
//          FILTER_TOO_LONG - the filter is longer than the data
  
int ApplyFilter(TheFilter &Filter, TheData &DataIn, TheData &DataOut)
{
  
  // return an error if the filter is longer than the data
  if (Filter.Length > DataIn.Length)
  {
      return FILTER_TOO_LONG;
  }
  // initialise the data structure that holds the filtered data
    
    DataOut.Values = (DataIn.Length - Filter.Length)+1
    unsigned int [DataOut.Length];
    float* pFloatDataOut;


  // get memory for the filtered data
      DataOut.Values = new double[DataOut.Length];
    if (pDataOut !=0)
    
    {
        cout <<"Memory allocated\n"
             <<"Address is: "<< pFloatDataOut << "\n";
    }
    
    {
        cerr <<"***Memory could not be allocated***";
        exit (1);
    }

  // apply the filter to the data
    
    for(unsigned int i=0; i<DataOut.Length; i++)
    {
        
        DataOut.Values[i] = 0;
    }

    for (unsigned int j=0; j!=DataOut.Length; ++j)
    {
    unsigned int a = j;

      for (unsigned int k=0; k<Filter.Length; ++k)
      {
      DataOut.Values[j]=DataOut.Values[j] + (DataIn.Values[a]*Filter.Values[k]);
    
      a = a + 1;
      }
    }
  DataOut.Valid = true;
  return OK;
}


// Display input data, filter values and output data
// Arguments:
//   (1) the structure of the filter to be applied
//   (2) the structure containing the data to be filtered
//   (3) the structure that holds the filtered data
// Returns: nothing

void DisplayData(TheFilter &Filter, TheData &DataIn, TheData &DataOut)
{  
      
// display all of the input data values
for (int count=0; count<DataIn.Length; count++)
{
cout  << DataIn.Values[count] << endl;  
}
    
// display all of the filter values

for (int count=0; count<Filter.Length; count++)
{
cout << Filter.Values[count] << endl;  
}
    
  // display all of the data output values
for (int count=0; count<DataOut.Length; count++)
{
cout << DataOut.Values[count]<< endl;
}  


}
}



any suggestions will be appreciated
thanks again
++++++++++++++++++++++++++++++++++++++++++++++++++

[
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Digital Filter
25 Nov, 2007 - 01:35 PM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,864



Thanked: 53 times
Dream Kudos: 550
My Contributions
it really helps if you let us know what the errors are....
User is offlineProfile CardPM
+Quote Post

vasily1001
RE: Digital Filter
25 Nov, 2007 - 01:38 PM
Post #5

New D.I.C Head
*

Joined: 22 Nov, 2007
Posts: 4


My Contributions
oh sorry,
they are:
error C2601: 'ApplyFilter' : local function definitions are illegal
error C2601: 'DisplayData' : local function definitions are illegal
Error executing cl.exe.

task1.exe - 2 error(s), 0 warning(s)

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Digital Filter
25 Nov, 2007 - 02:35 PM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,864



Thanked: 53 times
Dream Kudos: 550
My Contributions
ok the first thing I see is that EnterFilter() is missing a closing bracket.

Then there is a syntax error in ApplyFilter...
QUOTE
CODE
    DataOut.Values = (DataIn.Length - Filter.Length)+1
    unsigned int [DataOut.Length];


pDataOut is never declared or initialized.


User is offlineProfile CardPM
+Quote Post

vasily1001
RE: Digital Filter
25 Nov, 2007 - 05:19 PM
Post #7

New D.I.C Head
*

Joined: 22 Nov, 2007
Posts: 4


My Contributions
I've changed it around a bit and it works fine

thanks alot!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 12:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month