GithubHelp home page GithubHelp logo

Comments (6)

henkmuller avatar henkmuller commented on August 16, 2024

Welcome to the wonderful world of C.
How annoying...

I suggest something along the lines of

#define QN(f,n) ((int)(((f) * (1ULL << (n+10))+512) >> 10))

from lib_dsp.

ThomasGmeinder avatar ThomasGmeinder commented on August 16, 2024

Henk provided additional hints separately to improve the rounding: Shift up/down by 20 instead of 10. Consequently, add (1<<19) instead of (1<<9)) before downshift.

This would be the new macro for Q24:

#define Q24(f) (((f) * ((unsigned long long)1 << (24+20)) + (1<<19)) >> 20)

Unfortunately this causes clang to issue an error which seems invalid:
main.xc:8:27: error: invalid operands to binary >>
main.xc:4:70: note: expanded from here
#define Q24(f) (((f) * ((unsigned long long)1 << (24+20)) + (1<<19)) >> 20)


I filed Bug 17171<http://bugzilla/show_bug.cgi?id=17171> because this error prevents using the improved rounding method in the Qn macros.

To work around the error I implemented the rounding in two lines in app_math.xc:

       signed long long tmp = (d_sine_ref) * (1ULL << (24+20)) + (1<<19);

       q8_24 sine_ref  = tmp >> 20;


instead of using the macro:

q8_24 sine_ref = Q24(d_sine_ref);

Running the brute force test over all possible 105414357 inputs to lib_dsp_math_sin and lib_dsp_math_cos
proves that this improved rounding method is indeed as good as the method using the "round" function:
#define Q24(f) round(f * (double)(1<<24))

Until there is a fix or workaround for the compile error, I intend to revert back to the previous versions of the Qn macros to get rid of the build errors.

For the test of lib_dsp_math_sin and lib_dsp_math_cos
in app_math.xc I will use the two-line approach shown above.

Regards, /T

From: Henk Muller <[email protected]<mailto:[email protected]>>
Reply-To: xmos/lib_dsp <[email protected]<mailto:[email protected]>>
Date: Monday, 25 January 2016 17:56
To: xmos/lib_dsp <[email protected]<mailto:[email protected]>>
Subject: Re: [lib_dsp] Q format macros cannot be used to initialise globals (#16)


Welcome to the wonderful world of C.
How annoying...

I suggest something along the lines of

#define QN(f,n) (((f) * (1ULL << (n+10))+512) >> 10)

—
Reply to this email directly or view it on GitHub<https://github.com/xmos/lib_dsp/issues/16#issuecomment-174581412>.

from lib_dsp.

xross avatar xross commented on August 16, 2024

Casting looks bad, did you mean something like the following (sorry didn’t read the code..)

#define Q24(f) ((unsigned long long) ((f) * ((unsigned long long)1 << (24+20)) + (1<<19)) >> 20)

On 25 Jan 2016, at 23:05, Thomas Gmeinder <[email protected]mailto:[email protected]> wrote:

Henk provided additional hints separately to improve the rounding: Shift up/down by 20 instead of 10. Consequently, add (1<<19) instead of (1<<9)) before downshift.

This would be the new macro for Q24:

#define Q24(f) (((f) * ((unsigned long long)1 << (24+20)) + (1<<19)) >> 20)

Unfortunately this causes clang to issue an error which seems invalid:
main.xc:8:27: error: invalid operands to binary >>
main.xc:4:70: note: expanded from here
#define Q24(f) (((f) * ((unsigned long long)1 << (24+20)) + (1<<19)) >> 20)


I filed Bug 17171<http://bugzilla/show_bug.cgi?id=17171> because this error prevents using the improved rounding method in the Qn macros.

To work around the error I implemented the rounding in two lines in app_math.xc:

signed long long tmp = (d_sine_ref) * (1ULL << (24+20)) + (1<<19);

q8_24 sine_ref = tmp >> 20;


instead of using the macro:

q8_24 sine_ref = Q24(d_sine_ref);

Running the brute force test over all possible 105414357 inputs to lib_dsp_math_sin and lib_dsp_math_cos
proves that this improved rounding method is indeed as good as the method using the "round" function:
#define Q24(f) round(f * (double)(1<<24))

Until there is a fix or workaround for the compile error, I intend to revert back to the previous versions of the Qn macros to get rid of the build errors.

For the test of lib_dsp_math_sin and lib_dsp_math_cos
in app_math.xc I will use the two-line approach shown above.

Regards, /T

From: Henk Muller <[email protected]<mailto:[email protected]><mailto:[email protected]>>
Reply-To: xmos/lib_dsp <[email protected]<mailto:[email protected]><mailto:[email protected]>>
Date: Monday, 25 January 2016 17:56
To: xmos/lib_dsp <[email protected]<mailto:[email protected]><mailto:[email protected]>>
Subject: Re: [lib_dsp] Q format macros cannot be used to initialise globals (#16)


Welcome to the wonderful world of C.
How annoying...

I suggest something along the lines of

#define QN(f,n) (((f) * (1ULL << (n+10))+512) >> 10)

—
Reply to this email directly or view it on GitHub<https://github.com/xmos/lib_dsp/issues/16#issuecomment-174581412>.

—
Reply to this email directly or view it on GitHub<https://github.com/xmos/lib_dsp/issues/16#issuecomment-174719385>.

from lib_dsp.

henkmuller avatar henkmuller commented on August 16, 2024

Aha!

Of course

It shoudl be / (1<<20).
Not >> 20

On 25 Jan 2016, at 23:05, Thomas Gmeinder [email protected] wrote:

Unfortunately this causes clang to issue an error which seems invalid:
main.xc:8:27: error: invalid operands to binary >>
main.xc:4:70: note: expanded from here
#define Q24(f) (((f) * ((unsigned long long)1 << (24+20)) + (1<<19)) >> 20)

from lib_dsp.

henkmuller avatar henkmuller commented on August 16, 2024

Hang on….

Mine is wrong - Ross’s is right.

On 26 Jan 2016, at 07:18, Henk Muller [email protected] wrote:

Aha!

Of course

It shoudl be / (1<<20).
Not >> 20

from lib_dsp.

ThomasGmeinder avatar ThomasGmeinder commented on August 16, 2024

Thanks Ross.
Robert confirmed separately that a cast was missing. Shift on double is not supported. Makes sense.
The macro looks like this now:

// Convert from floating point to fixed point

#define Q31(f) (int)((signed long long)((f) * ((unsigned long long)1 << (31+20)) + (1<<19)) >> 20)

#define Q30(f) (int)((signed long long)((f) * ((unsigned long long)1 << (30+20)) + (1<<19)) >> 20)

#define Q29(f) (int)((signed long long)((f) * ((unsigned long long)1 << (29+20)) + (1<<19)) >> 20)

#define Q28(f) (int)((signed long long)((f) * ((unsigned long long)1 << (28+20)) + (1<<19)) >> 20)

#define Q27(f) (int)((signed long long)((f) * ((unsigned long long)1 << (27+20)) + (1<<19)) >> 20)

#define Q26(f) (int)((signed long long)((f) * ((unsigned long long)1 << (26+20)) + (1<<19)) >> 20)

#define Q25(f) (int)((signed long long)((f) * ((unsigned long long)1 << (25+20)) + (1<<19)) >> 20)

#define Q24(f) (int)((signed long long)((f) * ((unsigned long long)1 << (24+20)) + (1<<19)) >> 20)

#define Q23(f) (int)((signed long long)((f) * ((unsigned long long)1 << (23+20)) + (1<<19)) >> 20)

#define Q22(f) (int)((signed long long)((f) * ((unsigned long long)1 << (22+20)) + (1<<19)) >> 20)

#define Q21(f) (int)((signed long long)((f) * ((unsigned long long)1 << (21+20)) + (1<<19)) >> 20)

#define Q20(f) (int)((signed long long)((f) * ((unsigned long long)1 << (20+20)) + (1<<19)) >> 20)

#define Q19(f) (int)((signed long long)((f) * ((unsigned long long)1 << (19+20)) + (1<<19)) >> 20)

#define Q18(f) (int)((signed long long)((f) * ((unsigned long long)1 << (18+20)) + (1<<19)) >> 20)

#define Q17(f) (int)((signed long long)((f) * ((unsigned long long)1 << (17+20)) + (1<<19)) >> 20)

#define Q16(f) (int)((signed long long)((f) * ((unsigned long long)1 << (16+20)) + (1<<19)) >> 20)

Q24 and works well in the sin and cos test in app_math.xc (configured in brute force mode #define TEST_ALL_RAD 1)
See Appendix.

I will re-compile and run the other examples and make a pull request later.

Cheers, /T

Appendix:

Sine wave (one cycle from -Pi to +Pi) :

num calculations: 105414357; Errors >=1: 21809851 (20.69%); Errors >=2: 0 ( 0.00%)

Max absolute error: 1

Cosine wave (one cycle from -Pi to +Pi) :

num calculations: 105414357; Errors >=1: 39913894 (37.86%); Errors >=2: 0 ( 0.00%)

Max absolute error: 1

from lib_dsp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.