Comments (6)
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.
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.
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.
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.
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.
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)
- FFT testing
- dsp_bfp documentation missing from rst
- dsp_bfp.h causes error when included by a C file HOT 2
- dsp_vector_abs often outputs negative numbers HOT 3
- dsp_vector_{muls_addv, muls_subv, mulv_addv, mulv_subv} do not give correct outputs for vector lengths > 7.
- Double-word alignment of inputs HOT 3
- xmake fails for app_vector HOT 1
- dsp_adaptive_nlms error when the input source_sample is a very low value
- DCT overflows rather than saturates
- Standardise naming convention + add regex regression tests
- Remove `_bfp` from functions that aren't BFP specific
- Add missing docstrings to api/dsp_bfp.h
- Simulator time-out in app_math tests
- Convert legacy tests to run with axe
- Extend non-XS1 processing to XS3 HOT 2
- tests/test_biquad does not build for xcore.ai
- tests/test_fft_forward does not build for xcore.ai HOT 1
- Some tests require autogenerated files and do not build for xcore.ai HOT 1
- tests/dsp_unit_tests cannot run under xcore.ai
- examples/AN00209 Application Note fails to build HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lib_dsp.