Question about function in a template and in a class which uses the template

F

fl

Hi,
I am new to C++. I got the following snippet on the web. It is similar to my project application. I want to continue write with this code for my project. I find that there is process definition in the template. In the class definition, it has also a process definition. Could you explain what is the difference between them?

Thanks,




....................................
template<int UPSAMPLE_RATE, int TAPS_PER_PHASE, int MAX_INPUT_SAMPLE_COUNT>
class polyphase_upsample_fir_c
{
public:
static const int STATE_SAMPLE_COUNT = TAPS_PER_PHASE-1;

private:
const float (&taps)[UPSAMPLE_RATE][TAPS_PER_PHASE]; // These must be stored flipped left-to-right for simplicity
// float taps[UPSAMPLE_RATE][TAPS_PER_PHASE]; // THIS IS 4x FASTER THAN USING THE REFERENCE
cmplx_float_c state_and_input[STATE_SAMPLE_COUNT + MAX_INPUT_SAMPLE_COUNT];

public:
polyphase_upsample_fir_c(const float (&taps)[UPSAMPLE_RATE][TAPS_PER_PHASE]) :
taps(taps)
{
// Initialize the state to zero
for(int index=0; index<STATE_SAMPLE_COUNT; index++)
{
state_and_input[index].set(0, 0);
}
}

void process(int input_sample_count, cmplx_float_c* output);

// Returns a pointer to the first sample past the state memory, this is where the filter input should be placed.
inline cmplx_float_c* get_input_pointer() { return &state_and_input[STATE_SAMPLE_COUNT]; }

};






class polyphase_upsample_fir_24_c
{
public:
static const int UPSAMPLE_RATE = 24;
static const int TAPS_PER_PHASE = 16;
static const int MAX_INPUT_SAMPLE_COUNT = 48;

private:
polyphase_upsample_fir_c<UPSAMPLE_RATE, TAPS_PER_PHASE, MAX_INPUT_SAMPLE_COUNT> filter;
static const float taps[UPSAMPLE_RATE][TAPS_PER_PHASE];

public:
polyphase_upsample_fir_24_c():
filter(taps)
{}

void process(int input_sample_count, cmplx_float_c* output);

inline cmplx_float_c* get_input_pointer() { return filter.get_input_pointer(); }
};
 
B

Barry Schwarz

Hi,
I am new to C++. I got the following snippet on the web. It is similar to my project application. I want to continue write with this code for my project. I find that there is process definition in the template. In the class definition, it has also a process definition. Could you explain what is the difference between them?

When posting to Usenet, please set you line length to something less
than 80 characters.

Neither the template nor the class has a definition for the function
named process. In both case, it is a declaration (effectively a
prototype).

The prototypes are for two different functions.

The one in the template is for a function that will be a member
of class polyphase_upsample_fir_c. Wherever the function is
implemented, the first line will be of the form
void polyphase_upsample_fir_c::process(...

The one in the class is for a function that will be a member of class
polyphase_upsample_fir_24_c. Where it is implemented, the first line
will be of the form
void polyphase_upsample_fir_24_c::process(...

Notice the difference in the class names. One ends in fir_c and the
other in fir_24_c.

Unless the web site where you found this provides the implementation
of these functions, you will have to write the implementations
yourself.
template<int UPSAMPLE_RATE, int TAPS_PER_PHASE, int MAX_INPUT_SAMPLE_COUNT>
class polyphase_upsample_fir_c
{
public:
static const int STATE_SAMPLE_COUNT = TAPS_PER_PHASE-1;

private:
const float (&taps)[UPSAMPLE_RATE][TAPS_PER_PHASE]; // These must be stored flipped left-to-right for simplicity
// float taps[UPSAMPLE_RATE][TAPS_PER_PHASE]; // THIS IS 4x FASTER THAN USING THE REFERENCE
cmplx_float_c state_and_input[STATE_SAMPLE_COUNT + MAX_INPUT_SAMPLE_COUNT];

public:
polyphase_upsample_fir_c(const float (&taps)[UPSAMPLE_RATE][TAPS_PER_PHASE]) :
taps(taps)
{
// Initialize the state to zero
for(int index=0; index<STATE_SAMPLE_COUNT; index++)
{
state_and_input[index].set(0, 0);
}
}

void process(int input_sample_count, cmplx_float_c* output);

// Returns a pointer to the first sample past the state memory, this is where the filter input should be placed.
inline cmplx_float_c* get_input_pointer() { return &state_and_input[STATE_SAMPLE_COUNT]; }

};

class polyphase_upsample_fir_24_c
{
public:
static const int UPSAMPLE_RATE = 24;
static const int TAPS_PER_PHASE = 16;
static const int MAX_INPUT_SAMPLE_COUNT = 48;

private:
polyphase_upsample_fir_c<UPSAMPLE_RATE, TAPS_PER_PHASE, MAX_INPUT_SAMPLE_COUNT> filter;
static const float taps[UPSAMPLE_RATE][TAPS_PER_PHASE];

public:
polyphase_upsample_fir_24_c():
filter(taps)
{}

void process(int input_sample_count, cmplx_float_c* output);

inline cmplx_float_c* get_input_pointer() { return filter.get_input_pointer(); }
};
 
A

Alf P. Steinbach

I find that there is process definition in the template. In the class
definition, it has also a process definition. Could you explain what
is the difference between them?

As the clc++m mod explained (it wasn't I who rejected this article, and
I'm not sure that I quite agree with that article rejection, but as one
of the mods I get to see the rejection notices), these are "member
functions in different classes, and they're separate even though they
have the same name and appear related".

As the mod forgot to explain, the main point of that is that these
classes can be *used* in the same way, at least when the usage is
suitably restricted.

And that is especially convenient for templated code, where the
particular class used (and hence, the particular process() member
function used) can be specified via a template parameter.

...................................
template<int UPSAMPLE_RATE, int TAPS_PER_PHASE, int MAX_INPUT_SAMPLE_COUNT>
class polyphase_upsample_fir_c
{ [snip]

void process(int input_sample_count, cmplx_float_c* output);
};

class polyphase_upsample_fir_24_c
{ [snip]

void process(int input_sample_count, cmplx_float_c* output);
};


Cheers & hth.,

- Alf
 
F

fl

Hi,

I am new to C++. I got the following snippet on the web. It is similar tomy project application. I want to continue write with this code for my project. I find that there is process definition in the template. In the classdefinition, it has also a process definition. Could you explain what is the difference between them?



Thanks,









...................................

template<int UPSAMPLE_RATE, int TAPS_PER_PHASE, int MAX_INPUT_SAMPLE_COUNT>

class polyphase_upsample_fir_c

{

public:

static const int STATE_SAMPLE_COUNT = TAPS_PER_PHASE-1;



private:

const float (&taps)[UPSAMPLE_RATE][TAPS_PER_PHASE]; // These must be stored flipped left-to-right for simplicity

// float taps[UPSAMPLE_RATE][TAPS_PER_PHASE]; // THIS IS 4x FASTER THANUSING THE REFERENCE

cmplx_float_c state_and_input[STATE_SAMPLE_COUNT + MAX_INPUT_SAMPLE_COUNT];



public:

polyphase_upsample_fir_c(const float (&taps)[UPSAMPLE_RATE][TAPS_PER_PHASE]) :

taps(taps)

{

// Initialize the state to zero

for(int index=0; index<STATE_SAMPLE_COUNT; index++)

{

state_and_input[index].set(0, 0);

}

}



void process(int input_sample_count, cmplx_float_c* output);



// Returns a pointer to the first sample past the state memory, this iswhere the filter input should be placed.

inline cmplx_float_c* get_input_pointer() { return &state_and_input[STATE_SAMPLE_COUNT]; }



};













class polyphase_upsample_fir_24_c

{

public:

static const int UPSAMPLE_RATE = 24;

static const int TAPS_PER_PHASE = 16;

static const int MAX_INPUT_SAMPLE_COUNT = 48;



private:

polyphase_upsample_fir_c<UPSAMPLE_RATE, TAPS_PER_PHASE, MAX_INPUT_SAMPLE_COUNT> filter;

static const float taps[UPSAMPLE_RATE][TAPS_PER_PHASE];



public:

polyphase_upsample_fir_24_c():

filter(taps)

{}



void process(int input_sample_count, cmplx_float_c* output);



inline cmplx_float_c* get_input_pointer() { return filter.get_input_pointer(); }

};

Thanks both of you. I went through a C++ book for a couple of times. I programmed several small projects. I do not program a little complex, just likethe example code I provide. I would like to continue work on this code.
BTW, my original post was written on IE browser. I do not know why it has so long lines. What can cause this problem? Thanks,
 
I

Ian Collins

Thanks both of you. I went through a C++ book for a couple of times.
I programmed several small projects. I do not program a little
complex, just like the example code I provide. I would like to
continue work on this code. BTW, my original post was written on IE
browser. I do not know why it has so long lines. What can cause this
problem? Thanks,

That shite google interface that also double spaces all your quotes.

Considerate posters trim their lines and remove all the extra crud,
please do so in future!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top