Click here to Skip to main content
15,610,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Afternoon everyone,

I've stumbled across an interesting problem and I was hoping someone may be able to point me towards a solution. First let it be known that the solution has been coded in MATLAB where it seems to be handled fine.

Matlab Code:[^]
Data File:[^]

Feel free to run the mfile to see what the filtered signal looks like.

I have implemented this as a C++ class and am not getting the desired results. The filtered value Y(m) climbs and climbs to infinity and I'm starting to believe it has to do with SUBTRACTING REALLY SMALL NUMBERS (*dun dun dun*). I have uploaded the output of the c++ code here. If you'd like to take a look feel free. The output is set up the same way the comments were organized in the m-file. X(m) refers to the raw input signal and Y(m) refers to the filtered output signal.

Note that my filter coefficients are at the order of e-8 and most of my values carry large significant figures. I have decided not to post the c code, mainly because I don't believe there is an error in the code but if anyone thinks that will help I would be more than happy to. If you un-comment the m-file output lines, you can step through it in debug mode and compare the values with the text file. They are all very close but always slightly off, which I believe is compounding and sending my c++ Y(m) onward and upward.

I use doubles for all of my variables. I believe this article should be helpful with respect to my issue, Avoiding Overflow, Underflow, and Loss of Precision[^] but I can not figure out how to apply this to my specific problem.


Updated 3-Aug-11 10:29am

Try to do it with Gnu bc[^]

bc is an arbitrary precision numeric processing language. Syntax is similar to C so testing it out shouldn't be to hard - try moving your c++ code to bc.

Ensure that you are not using SSE2 etc for floating point processing (vc++ compiler option).

Use _control87, _controlfp, __control87_2[^] to ensure that you are getting "real" 64-bit double support, and not 53-bit - _PC_64 vs _PC_53

There is also The GNU Multiple Precision Arithmetic Library [^], or you can look at the High-Precision Software Directory[^]

Best regards
Espen Harlinn
Share this answer
Sergey Alexandrovich Kryukov 3-Aug-11 20:05pm    
The information if very interesting (I did not know about bc and this GNU library), my 5, but I thinks the essence if the problem is the culture of numeric computations using available floating-point precision type avoiding unnecessary precision loss, such as in case of subtraction of two close big numbers, etc.

Donald Knuth maintained that it will take a long time to learn now to solve a usual middle-school quadratic equation correctly, without loss of precision. This is something which is not very easy to learn.
Espen Harlinn 4-Aug-11 3:36am    
OP suspects the precision, bc can be used as a test-bed. If it turns out that bc provides the same answer as mathlab - going with an existing multiple precision library is usually esier than starting from scratch.
Donald Hume 4-Aug-11 14:01pm    
Most certainly, Espen. I've started to implement the GMP library as it has all the functionality I would need. There was so much code that the thought of trying to migrate to GNU bc seemed overwhelming. The GMP library seems to be a great place for me to start.

Thanks a lot I appreciate your help.
Espen Harlinn 4-Aug-11 14:31pm    
Great! I didn't expect you to port the code to bc - just a tool to test your hypothesis picking some appropriate pieces of code. GMP is an excellent library - Anyway, I wish you good luck with you project!
Stick to the article by John D. Cook you've referenced in your question — it's short and to the point. If you fail to apply it to your code, you should seriously think about your education. This is more about applied mathematics and informatics than programming, even though all implications are reflected in practical programming techniques. Or hire yourself a tutor. I don't think explaining your issues in a short CodeProject solution will add anything to it.

The closest relevant reading on the topic would be classical 'The Art of Computer Programming' by Donald Knuth. See[^].

Share this answer
Espen Harlinn 3-Aug-11 17:57pm    
Nice reply :)
Sergey Alexandrovich Kryukov 3-Aug-11 18:03pm    
Thank you, Espen.
Donald Hume 3-Aug-11 19:04pm    
I think I would have just preferred you left it to just the first and last sentence. From a scholarly view your response seemed unnecessarily abrasive (my 1) as I'm not sure what you can infer about my education or my degree (relevant to programming or not).

To be honest I didn't read through the whole article so I will be sure to do that. *sigh* I appreciate your response though, I think.
Sergey Alexandrovich Kryukov 3-Aug-11 19:59pm    
I do not infer anything at all.
Read thoroughly, this clause is under "if". The essence of your frustration is not clear. If you want to learn anything, learn it; this is a kind of absolute value regardless of any degrees and current level of knowledge. If you want to use people's help, use it.
Instead you're trying to fight back and play offended. Who do you think might loose? Not me.

I'm just trying to help. If I had better article to advise, I would do it right away.
At least you have a maturity to admit you did vote of 1; this is good, many are not even brave enough to confess such things.

Good luck,
Donald Hume 3-Aug-11 20:26pm    
Woah woah woah, I'm not sure about all of that haha. As I said before, I do appreciate your response.

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