General C++ coding program structure guidance

frankzappa

Well-known member
I learned some programming in C++ and I've started working on a project. I have difficulties getting started with the coding because of some problems with structuring the program.

I have 10 sensors that I need to read and do some filtering and whatnot. How do I avoid having to add a number to the variable name to differentiate the different operations? Right now I have 10 of everything which is stupid.

For example I have some filters that I put in classes, as an example below a high pass and a low pass filter in two classes:

Code:
class  HP_peakTrack //HP bessel 1st order 150Hz
{
  public:
    HP_peakTrack()
    {
      v[0]=v[1]=0.0;
    }
  private:
    double v[2];
  public:
    double filter(double x) //class II 
    {
      v[0] = v[1];
      v[1] = (9.906629452463440177e-1 * x)
         + (0.98132589049268825754 * v[0]);
      return 
         (v[1] - v[0]);
    }
};

class  LP_peakTrack //2nd order LP Butterworth 1000Hz
{
  public:
    LP_peakTrack()
    {
      v[0]=0.0;
      v[1]=0.0;
      v[2]=0.0;
    }
  private:
    double v[3];
  public:
    double filter(double x) //class II 
    {
      v[0] = v[1];
      v[1] = v[2];
      v[2] = (3.621681514928615665e-3 * x)
         + (-0.83718165125602272969 * v[0])
         + (1.82269492519630826877 * v[1]);
      return 
         (v[0] + v[2])
        +2 * v[1];
    }
};

So lets say I'm reading the sensors in the main loop and I do the following when I need to filter the signal:

Code:
  double peakSignal00 = HP_peakTrack00.filter(g_sensorValue[0]);
  peakSignal00 = LP_peakTrack00.filter(peakSignal00);

So I send the sensor value to the filter class to do the filtering and I get back the filtered value.
This is stupid because I need to copy that code 10 times and then change the 00 to 01, 02, 03 etc. I also need 10 each of those filter classes.

Any suggestions on how to structure the program to avoid this? Is there somewhere I can look at some small real world projects that are simple enough for a noob to understand? A program on how to calculate the mean value or something like that is not helping which is all I can find :D

Oh and no need to suggest I can use a band pass filter in stead, this is just an example.

Thanks
 
A simple way would be to combine the HP/LP/result in a struct, and declare an array of those structures:

Code:
struct {
  LP_peakTrack LP;
  HP_peakTrack HP;
  double peakSignal;
} filters[10];

and process the 10 input signals like this

Code:
  for (int i=0; i<10; i++) {
    filters[i].peakSignal = filters[i].HP.filter( signal[i] );
    filters[i].peakSignal = filters[i].LP.filter( filters[i].peakSignal );
  }
 
A simple way would be to combine the HP/LP/result in a struct, and declare an array of those structures:

Code:
struct {
  LP_peakTrack LP;
  HP_peakTrack HP;
  double peakSignal;
} filters[10];

and process the 10 input signals like this

Code:
  for (int i=0; i<10; i++) {
    filters[i].peakSignal = filters[i].HP.filter( signal[i] );
    filters[i].peakSignal = filters[i].LP.filter( filters[i].peakSignal );
  }

Oh, that looks very neat. I'll try something similar.

I can't do the for loop though, I will need to read two sensors at a time and do all the work on the previous sensor pair while waiting for the next two sensor readings.
 
Back
Top