00001 /*This file is prepared for Doxygen automatic documentation generation.*/ 00015 /* Copyright (c) 2009 Atmel Corporation. All rights reserved. 00016 * 00017 * Redistribution and use in source and binary forms, with or without 00018 * modification, are permitted provided that the following conditions are met: 00019 * 00020 * 1. Redistributions of source code must retain the above copyright notice, this 00021 * list of conditions and the following disclaimer. 00022 * 00023 * 2. Redistributions in binary form must reproduce the above copyright notice, 00024 * this list of conditions and the following disclaimer in the documentation 00025 * and/or other materials provided with the distribution. 00026 * 00027 * 3. The name of Atmel may not be used to endorse or promote products derived 00028 * from this software without specific prior written permission. 00029 * 00030 * 4. This software may only be redistributed and used in connection with an Atmel 00031 * AVR product. 00032 * 00033 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 00034 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00035 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00036 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 00037 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00038 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00039 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00040 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00041 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00042 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00043 * 00044 */ 00045 #include "dsp.h" 00046 00047 #if defined(FORCE_ALL_GENERICS) || \ 00048 defined(FORCE_GENERIC_OP16_SIN) || \ 00049 !defined(TARGET_SPECIFIC_OP16_SIN) 00050 00051 #define DSP16_MODULO_1_MASK (((U16) -1) >> (16 - (DSP16_QB+1))) 00052 00053 extern dsp16_t dsp16_op_kernel_cosfix(dsp16_t angle); 00054 extern dsp16_t dsp16_op_kernel_sinfix(dsp16_t angle); 00055 00056 /* 00057 * Input range = [-1; 1] corresponding to [-pi; pi] 00058 */ 00059 dsp16_t dsp16_op_sin(dsp16_t angle) 00060 { 00061 #if DSP16_QA > 1 00062 U16 n = ((angle + DSP16_Q(0.25)) & DSP16_MODULO_1_MASK) >> (DSP16_QB-1); 00063 #else 00064 U16 n = ((U16) (angle + DSP16_Q(0.25))) >> (DSP16_QB-1); 00065 #endif 00066 00067 // Translate input down to +/- pi/4 00068 angle -= n*DSP16_Q(0.5); 00069 00070 #if DSP16_QA > 1 00071 // angle modulo 1 (signed values) 00072 angle <<= (16 - (DSP16_QB+1)); 00073 angle >>= (16 - (DSP16_QB+1)); 00074 #endif 00075 00076 switch(n) 00077 { 00078 case 0: 00079 return dsp16_op_kernel_sinfix(angle); 00080 case 1: 00081 return dsp16_op_kernel_cosfix(angle); 00082 case 2: 00083 return -dsp16_op_kernel_sinfix(angle); 00084 default: 00085 return -dsp16_op_kernel_cosfix(angle); 00086 } 00087 } 00088 00089 #endif 00090 00091 #if defined(FORCE_ALL_GENERICS) || \ 00092 defined(FORCE_GENERIC_OP32_SIN) || \ 00093 !defined(TARGET_SPECIFIC_OP32_SIN) 00094 00095 #define DSP32_MODULO_1_MASK (((U32) -1) >> (32 - (DSP32_QB+1))) 00096 00097 extern dsp32_t dsp32_op_kernel_cosfix(dsp32_t angle); 00098 extern dsp32_t dsp32_op_kernel_sinfix(dsp32_t angle); 00099 00100 /* 00101 * Input range = [-1; 1] corresponding to [-pi; pi] 00102 */ 00103 dsp32_t dsp32_op_sin(dsp32_t angle) 00104 { 00105 #if DSP32_QA > 1 00106 U32 n = ((angle + DSP32_Q(0.25)) & DSP32_MODULO_1_MASK) >> (DSP32_QB-1); 00107 #else 00108 U32 n = DSP32_Q(0.25); 00109 n += angle; 00110 n = n >> (DSP32_QB-1); 00111 #endif 00112 00113 // Translate input down to +/- pi/4 00114 angle -= n*DSP32_Q(0.5); 00115 00116 #if DSP32_QA > 1 00117 // angle modulo 1 (signed values) 00118 angle <<= (32 - (DSP32_QB + 1)); 00119 angle >>= (32 - (DSP32_QB + 1)); 00120 #endif 00121 00122 switch(n) 00123 { 00124 case 0: 00125 return dsp32_op_kernel_sinfix(angle); 00126 case 1: 00127 return dsp32_op_kernel_cosfix(angle); 00128 case 2: 00129 return -dsp32_op_kernel_sinfix(angle); 00130 default: 00131 return -dsp32_op_kernel_cosfix(angle); 00132 } 00133 } 00134 00135 #endif