Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi everyone,

Could anyone please help me to convert the following C++ code snippet to C code. I have basically zero knowledge in C++ and didn't understand this part.

class DCRemover
{
public:
	DCRemover() : alpha(0), dcw(0)
	{
	}
	DCRemover(float alpha_) : alpha(alpha_), dcw(0)
	{
	}

	float step(float x)
	{
		float olddcw = dcw;
		dcw = (float)x + alpha * dcw;

		return dcw - olddcw;
	}

	float getDCW()
	{
		return dcw;
	}

private:
	float alpha;
	float dcw;
};





What I have tried:

I didn't understand this part of code and couldn't do anything!
Posted
Updated 2-Nov-16 22:09pm
Comments
Suvendu Shekhar Giri 3-Nov-16 1:25am    
That's the wrong approach.
Why do you want to convert code of someone which you don't understand?
Why don't to write the required logic in C if you are good at C?
Vishnu_Pradeep 3-Nov-16 1:32am    
Thanks for the reply. This is actually a filtering code. Inorder to write it in C, I need to understand what this actually do.
Philippe Mori 3-Nov-16 22:57pm    
You could use a debugger or read the first few pages of documentation on C++...

In fact, if you know C and you have an idea of how filtering works, it should be easy to guess what that code do.
Vishnu_Pradeep 5-Nov-16 1:21am    
I agree. I was having doubt about the initialisation part, like what exactly this part does DCRemover() : alpha(0), dcw(0)
{
}
DCRemover(float alpha_) : alpha(alpha_), dcw(0)
{
}

I actually wrote the C code myself.

thanks

It looks like a class used by some signal processing loop. Because it is not very complex, it could be also written without class even in C++:
C++
void someProcessFunc(float dcr_alpha)
{
    float alpha, dcw, step, x;
    
    // These two lines together with the local variable
    //  declarations correspond to
    // DCRemover dcr(dcr_alpha);
    alpha = dcr_alpha;
    dcw = 0;

    for (/* iteration conditions*/)
    {
        // x is calculated here or set as part of the for conditions

        // These lines correspond to 
        //  step = dcr.step(x);
        step = -dcw;
        dcw = x + alpha * dcw;
        step += dcw;
    }
}
 
Share this answer
 
Comments
Vishnu_Pradeep 3-Nov-16 7:32am    
Thanks Jochen. Could you please explain the significance of this?

DCRemover() : alpha(0), dcw(0)
{
}
DCRemover(float alpha_) : alpha(alpha_), dcw(0)
{
}
Jochen Arndt 3-Nov-16 7:49am    
They are C++ class constructors. Constructors are special functions that are called when an instance of a class is created. Common usage is to initialise variables and allocate storage.

The above constructors use an initilisation list (a list of member initilisations). But using common assignment is allowed too:
DCRemover() { alpha = 0; dcw = 0; }
DCRemover(float alpha_) { alpha = alpha_; dcw = 0; }

Both versions are used inside a header file. But the implementation can be also moved to a source file:
// Header file
DCRemover();
DCRemover(float alpha_);

// Source file
DCRemover::DCRemover()
{
alpha = 0;
dcw = 0;
}
DCRemover::DCRemover(float alpha_)
{
alpha = alpha_;
dcw = 0;
}

Or again with an initialisation list:
DCRemover::DCRemover(float alpha_) : alpha(alpha_), dcw(0)
{
}

All the above will do the same.

See for example http://www.cplusplus.com/doc/tutorial/classes/
Vishnu_Pradeep 3-Nov-16 8:19am    
That is some valuable information. The code is working as expected.
Thanks
All it is, is a pair of class level private variables called alpha and dcw, plus a pair of basic functions to manipulate them.
The functions are obvious - they would compile with the two variables declared as global in C - but you can't just robotically translate C++ classes to C code, as each instance of the class that is created is independant, and C has no direct analog of that. You can;t just make the variables global, as you would get just one of each per application, where teh C++ version gets a pair per DCRemover that is created. While you could put them into a struct, that is the trivial part of the problem, because you need the support code to allocate a new struct each time the original code created a DCRemover instance, and you code would have to delete that struct each time the original code had finished with it, as C does not have built in destructors either.

This isn't a good approach: look at the algorithm that the code you found implements, and code that in your C code: it is be a lot simpler and more efficient in the long run.
 
Share this answer
 
Comments
Vishnu_Pradeep 3-Nov-16 7:35am    
Thank you for the response. Is the below code is correct conversion of this?

float DCremoverstep(float x)
{
float alpha=0.99;

float olddcw = dcw;
dcw = (float)x + alpha * dcw;

return dcw - olddcw;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900