/Cordic-Math

Cordic fixed point math

Primary LanguageC

Cordic math

Project Decription
Explore the docs »

· Report Bug · Request Feature


About The CORDIC Algorithm

The cordic algorithm is a simple and efficient algorithm to calculate trigonometric functions, hyperbolic functions, square roots, multiplications, divisions, exponentials, logarithms and more. The cordic algorithm dates back to 1956 but still work great for microcontrollers and such today.

Cordic uses simple simple bit-shift operations for several computing tasks. In the library I included other mathimatical functions aswell that might be useful for future users.

All the inputs and outputs are in degrees!

Polar to rectangular conversion takes a pointer to a struct. The struct is defined in the cordic-math.h file.

  • Rectangular to polar form takes the input on the x & y variable, and the result is given in the r & theta variable.
  • Polar to rectangular form takes the input on the theta & r variable, and the result is given in the x & y variable.

My image

(back to top)

Modifying The Code

The code is built around the defined variable CORDIC_MATH_FRACTION_BITS in cordic-math.h file. As default this is set to 16, which means the calculations is made in 16 bit fixed point arithmetic. This variable can easily be changed by future users to change the fixedpoint to suite their needs.

My image

There is also another define in the cordic-math.h file which is named CORDIC_SPEED_FACTOR. If more speed is needed and accuracy is not as important this variable can be changed. It's recomended to not change this variable lower than 8 or higher than 15.

My image

(back to top)

Accuracy

The following image shows the precision of the Cordic algorithm. The left column is math.h and the right column is the Cordic algorithm. The input was randomly picked and the result was the following:

My image

(back to top)

Functions

  • Arctan
  • Arcsin
  • Arccos
  • Tan
  • Cos
  • Sin
  • Squareroot
  • Calculation of Hypotenuse
  • Arctan Hyperbolic
  • Arcsin Hyperbolic
  • Arccos Hyperbolic
  • Tan Hyperbolic
  • Cos Hyperbolic
  • Sin Hyperbolic
  • e to the Power
  • x to the Power
  • Natural logratihm
  • Conversion From Radians to Degrees
  • Conversion From Degrees to Radians
  • Absolute
  • Is Odd
  • Is Even
  • Rectangular to Polar Conversion
  • Polar to Rectangular Conversion

(back to top)

FFT

Project Decription
Explore»

· Report Bug · Request Feature


About the Fast Fourier Transform

A fast Fourier transform (FFT) is an algorithm that computes the discrete Fourier transform (DFT) of a sequence, or its inverse (IDFT). Fourier transform converts a signal from its original domain (often time) to a representation in the frequency domain. DFT requires O(n^2) which is often to slow to be practical. FFT rapidly computes such transformations by factorizing the DFT-matrix, as a result it manages to reduce the complexity of computing the DFT from O(O^2) to O(N log N). The difference in speed can be enormous especially for long data sets where N may be in the thousands or millions.

For the following formulas:

N = The total number of sample nodes.

n = a specific index.

dt = the sample time.

Once you’ve called the fft() function multiply the 0:th element with:

Equation 1

Then multiply every element except the 0:th with:

Equation 2

To obtain the frequency that any index represents use the following formula:

Equation 3

About the code

The code is built around a complex datatype defined in fft.h the algorithm is also based on the fixed-point arithmetic to calculate faster on microcontroller units (MCU) that does not support float datatypes. The number of fraction bits is defined in the fft.h file as FFT_MATH_FRACTION_BITS (default is set as 16).

My image

Sinusoid wave

In the picture below you can observe the result of the fourier transform for two simple sinusoid waves added togheter. 12 Hz with an amplitude of 0.1 and 3 Hz with an amplitude of 0.2. This data is calculated with the Cordic Sin library which explains the small deviations. Sinusoid wave

Fourier Transform

In the picture below you can see the fourier spectrum of the combination of these sinusoid waves. Fourier transform

Inverse Fourier Transform

In the picture below the inverse fourier transform is calculated on the frequency spectrum resulting in a graph that is very close to the original sinusoid wave. Fourier transform

(back to top)