Ярлыки

_GetPixelIndex (1) _SetPixelIndex (1) 3-phase (1) 800x480 (1) АЦП (1) генератор (1) синхронный усилитель (2) структура (1) учебный курс (1) шаговый двигатель (1) ШИМ (2) accert (1) AD7608 (1) AD8429 (1) ADC (5) amplifer (1) arccos (1) arcsin (1) arctang (2) arctg (3) ARM (2) arm_sqrt_q15 (2) assembler (6) ASSERT (1) atan (2) bit (1) Bitband (1) boot (3) bootlloader (1) BUTTON (1) C (5) C# (1) CAN (2) CC2530 (5) CMSIS (4) command (1) Cordic (1) Core746I (1) CubeMX (4) DBGMCU (2) debug (2) debug.ini (1) delegate (1) Digital Potentiometers (1) DigitalPOT (1) Discovery (1) DMA (9) DMA2D (1) DSP (1) DSP library (1) DWT (1) EFM32 (5) EmWin (9) EXTI (1) FATFS (1) FMC (2) FreeRTOS (2) gl868-dual cmux (1) GPIO (4) GUI (2) GUIBuilder (1) GUIDRV_CompactColor_16 (1) HAL (3) HappyGecko (1) Hard Fault (2) heap (1) I2C (1) ID (1) ILI9320 (1) ILI9325 (1) Initialisation (1) InitLTDC (1) Instrumentithion (1) Interrupt (4) ITR (1) JTAG (1) Keil (5) LCDConf (2) lock-in (1) LTCD (1) LTDC (3) main (1) memory (1) MINI_STM32 Revision 01 (1) nBoot0 (1) NVIC (1) OnePulse (2) OSAL (4) pack (1) phase (1) printf (3) Pulse (1) PWM (12) RCC (2) RCR (1) Register (1) RESET (2) RS232 (3) RSS (1) RTC (3) RTOS-RTX (1) RTT (1) RTX-RTOS (1) SDCard (1) SDRAM (6) Segger (2) SPI (3) sqrt (3) SSD1298 (1) SSD1963 (1) Standart Peripherial Library (3) STANDBAY (1) startup (1) STemWin (8) stepper motor (1) STlink (2) STM32 (17) STM32429ZI (1) STM32Cube (1) STM32DBG.IN (1) STM32F (28) STM32F0 (4) STM32F1 (13) STM32F4 (10) STM32F4 Discovery (1) STM32F407ZG (1) STM32F429 (2) STM32F746 (1) STOP (1) string (1) struct (1) SWD (1) SWD JTAG (1) Synhronization (1) system_stm32f4xx.c (1) SystemInit (1) SysTick (1) task (4) telit (1) TIM (27) typedef (1) UART (1) USART (9) viewer (2) WM_PAINT (1) Z-stack (5) ZigBee (5)

среда, 18 февраля 2015 г.

CORDIC Arctg

Алгоритмы определения функции arctg различными методами



#include <vcl.h>
#pragma hdrstop


#include "Unit1.h"
#include "math.h"
#include "fixed_math.hpp"
 //---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;


Q15 cordic(Q15* x0, Q15* y0, Q15* z0, Q15 vecmode);
short zzz[16];
const int MAXBITS=15;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
static float atanTable[MAXBITS];

void initCordic()
{

    float t = 1.0;
    int i;
    for (i = 0; i < MAXBITS; ++i)
    {
        atanTable[i] = atan(t);
        t /= 2;                
    }

}


/* CORDIC m=1, y-->0 */


Q15 cordic(Q15* x0, Q15* y0, Q15* z0, Q15 vecmode)
{
    AnsiString buf;
    short t;
    Q15 x, y, z;
    int i;

    t = 1.0;
    x = *x0; y = *y0; z = *z0;

    Q15 x1;

    for (i = 0; i < MAXBITS; ++i)
    {

           if (vecmode >= 0.0f && y < vecmode || vecmode<0.0f  && z >= 0.0f)
        {
       

            x1 = x - y>>t;
             y = y + x>>t;
             z = z - Q15(atanTable[i]);
                    }
        else

        {
            x1 = x + y>>t;
              y = y - x>>t;
              z = z + Q15(atanTable[i]);
         
        }

        x = x1;

    }

    *x0 = x;
    *y0 = y;
    *z0 = z;

    return z;        

}

Q15 atanCordic(Q15 a)
{

    Q15 x = 1.0f;
    Q15 z = 0.0f;
    return cordic(&x, &a, &z, 0.0f);

}

void __fastcall TForm1::Button1Click(TObject *Sender)
{



    AnsiString buf;
    float x;
    float y;


    initCordic();

    for (x=0.0;x<=1.0;x=x+0.1)
    {
 
     Q15 v = atanCordic(Q15(x));
         y = atan(x);

        Memo1->Lines->Add(buf.sprintf("%5.2f", x));
        Memo2->Lines->Add(buf.sprintf("%f",Q15ToShort(v)/32768.0f));
        Memo3->Lines->Add(buf.sprintf("%10.6f",Q15ToShort(y)/32768.0f));
     

    }
 
}


//---------------------------------------------------------------------------


#ifndef _CORDIC_32_
#define _CORDIC_32_

//Constants
#define cordic_1K 0x26DD3B6A    // 1/k = 0.6072529350088812561694
#define half_pi 0x6487ED51      // pi / 2
#define MUL 1073741824.000000   // 1.0 = 1073741824
#define CORDIC_NTAB 32
int cordic_tab [] = {
    0x3243F6A8, 0x1DAC6705, 0x0FADBAFC, 0x07F56EA6, 0x03FEAB76, 0x01FFD55B, 0x00FFFAAA, 0x007FFF55,
    0x003FFFEA, 0x001FFFFD, 0x000FFFFF, 0x0007FFFF, 0x0003FFFF, 0x0001FFFF, 0x0000FFFF, 0x00007FFF,
    0x00003FFF, 0x00001FFF, 0x00000FFF, 0x000007FF, 0x000003FF, 0x000001FF, 0x000000FF, 0x0000007F,
    0x0000003F, 0x0000001F, 0x0000000F, 0x00000008, 0x00000004, 0x00000002, 0x00000001, 0x00000000
};

void cordic32_sin_cos(int alpha, int *s, int *c, int n)
{
    int k, d, tx;
    int z = alpha;
    *c = cordic_1K, *s = 0;
    n = (n > CORDIC_NTAB) ? CORDIC_NTAB : n;
    if(n<3) n = 3;
    for (k=0; k<n; ++k)
    {
        d = z >> 31;    //d = z>=0 ? 0 : -1;
        tx = *c;
        *c -= (((*s>>k) ^ d) - d);
        *s += (((tx>>k) ^ d) - d);
        z -= ((cordic_tab[k] ^ d) - d);
    }
}

// y = s, x = c
int cordic32_atan2(int s, int c, int n)
{
    int k, d, angle = 0;
    int re = c, im = s, tx;
    n = (n > CORDIC_NTAB) ? CORDIC_NTAB : n;
    if(n<3) n = 3;
    for (k=0; k<n; ++k)
    {
        d = im >> 31;
        angle += ((cordic_tab[k] ^ d) - d);
        tx = re;
        re += (((im>>k) ^ d) - d);
        im -= (((tx>>k) ^ d) - d);
    }
    return angle;
}

#endif




Комментариев нет:

Отправить комментарий