oldexamplecode
Dependencies: mbed
LookupTables.cpp@0:6863633bf8a4, 2017-03-24 (annotated)
- Committer:
- rik
- Date:
- Fri Mar 24 11:22:30 2017 +0000
- Revision:
- 0:6863633bf8a4
oldexamplecode;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rik | 0:6863633bf8a4 | 1 | |
rik | 0:6863633bf8a4 | 2 | #include "LookupTables.h" |
rik | 0:6863633bf8a4 | 3 | |
rik | 0:6863633bf8a4 | 4 | ////////////////// COSINE SINE LOOKUP TABLES |
rik | 0:6863633bf8a4 | 5 | |
rik | 0:6863633bf8a4 | 6 | // Returns the cosine value using the lut of a given value |
rik | 0:6863633bf8a4 | 7 | float lut_cos(float value){ |
rik | 0:6863633bf8a4 | 8 | // Cosine is symmetric so convert to absolute |
rik | 0:6863633bf8a4 | 9 | if (value < 0) |
rik | 0:6863633bf8a4 | 10 | value = -value; |
rik | 0:6863633bf8a4 | 11 | |
rik | 0:6863633bf8a4 | 12 | // and subtract the "offset" |
rik | 0:6863633bf8a4 | 13 | //float actualvalue = (float)(value % (2*M_PI)); |
rik | 0:6863633bf8a4 | 14 | |
rik | 0:6863633bf8a4 | 15 | while (value >= 2 * (float)M_PI){ |
rik | 0:6863633bf8a4 | 16 | value -= 2 * (float)M_PI; |
rik | 0:6863633bf8a4 | 17 | } |
rik | 0:6863633bf8a4 | 18 | |
rik | 0:6863633bf8a4 | 19 | // Calculate the value to a discrete value (1024*0.00613592 = 2 pi) |
rik | 0:6863633bf8a4 | 20 | int discreteValue = (int)(value / 0.00613592); |
rik | 0:6863633bf8a4 | 21 | |
rik | 0:6863633bf8a4 | 22 | return cosLookupTable_f[discreteValue]; |
rik | 0:6863633bf8a4 | 23 | } |
rik | 0:6863633bf8a4 | 24 | // Returns the sine value using the lut for a given value |
rik | 0:6863633bf8a4 | 25 | float lut_sin(float value){ |
rik | 0:6863633bf8a4 | 26 | // Convert to cosine |
rik | 0:6863633bf8a4 | 27 | value -= 0.5*(float)M_PI; |
rik | 0:6863633bf8a4 | 28 | // Cosine is symmetric so convert to absolute |
rik | 0:6863633bf8a4 | 29 | if (value < 0) |
rik | 0:6863633bf8a4 | 30 | value = -value; |
rik | 0:6863633bf8a4 | 31 | |
rik | 0:6863633bf8a4 | 32 | // subtract the "offset" of the original 0-2pi |
rik | 0:6863633bf8a4 | 33 | //float actualvalue = (float)(value % (float)(2 * M_PI)); |
rik | 0:6863633bf8a4 | 34 | |
rik | 0:6863633bf8a4 | 35 | while (value >= (2 * (float)M_PI)){ |
rik | 0:6863633bf8a4 | 36 | value -= (2 * (float)M_PI); |
rik | 0:6863633bf8a4 | 37 | } |
rik | 0:6863633bf8a4 | 38 | |
rik | 0:6863633bf8a4 | 39 | // Calculate the value to a discrete value (1024*0.00613592 = 2 pi) |
rik | 0:6863633bf8a4 | 40 | int discreteValue = (int)(value / 0.00613592); |
rik | 0:6863633bf8a4 | 41 | |
rik | 0:6863633bf8a4 | 42 | return cosLookupTable_f[discreteValue]; |
rik | 0:6863633bf8a4 | 43 | } |
rik | 0:6863633bf8a4 | 44 | |
rik | 0:6863633bf8a4 | 45 | // Returns the cosine value using the lut of a given value |
rik | 0:6863633bf8a4 | 46 | // WARNING, FAST VERSION ONLY WORKS WITH VALUES 0 - 2 PI |
rik | 0:6863633bf8a4 | 47 | float lut_cos_f(float value){ |
rik | 0:6863633bf8a4 | 48 | // Calculate the value to a discrete value (1024*0.00613592 = 2 pi) |
rik | 0:6863633bf8a4 | 49 | int discreteValue = (int)(value / 0.00613592); |
rik | 0:6863633bf8a4 | 50 | |
rik | 0:6863633bf8a4 | 51 | return cosLookupTable_f[discreteValue]; |
rik | 0:6863633bf8a4 | 52 | } |
rik | 0:6863633bf8a4 | 53 | |
rik | 0:6863633bf8a4 | 54 | ////////////////// END COSINE SINE LOOKUP TABLES |
rik | 0:6863633bf8a4 | 55 | |
rik | 0:6863633bf8a4 | 56 | ////////////////// HANNING LOOKUP TABLE |
rik | 0:6863633bf8a4 | 57 | |
rik | 0:6863633bf8a4 | 58 | // Applies the hanning LUT to the supplied array of data |
rik | 0:6863633bf8a4 | 59 | void applyHanningTable(float* data, int datasize){ |
rik | 0:6863633bf8a4 | 60 | if (datasize == 1024){ // Only works for 10 bit data arrays |
rik | 0:6863633bf8a4 | 61 | for (int i = 0; i < 1024; i++){ |
rik | 0:6863633bf8a4 | 62 | data[i] *= hanningTable_f[i]; |
rik | 0:6863633bf8a4 | 63 | } |
rik | 0:6863633bf8a4 | 64 | } |
rik | 0:6863633bf8a4 | 65 | } |
rik | 0:6863633bf8a4 | 66 | |
rik | 0:6863633bf8a4 | 67 | ////////////////// END HANNING LOOKUP TABLE |