Parth Chandak
/
c_FFT_Function
fft_implementation_draft1
main.cpp@0:b723ff6df537, 2018-03-07 (annotated)
- Committer:
- parthchandak02
- Date:
- Wed Mar 07 11:06:31 2018 +0000
- Revision:
- 0:b723ff6df537
- Child:
- 1:b86b60ae81af
draft1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
parthchandak02 | 0:b723ff6df537 | 1 | #include <mbed.h> |
parthchandak02 | 0:b723ff6df537 | 2 | |
parthchandak02 | 0:b723ff6df537 | 3 | #include <iostream> |
parthchandak02 | 0:b723ff6df537 | 4 | #include <complex> |
parthchandak02 | 0:b723ff6df537 | 5 | #define MAX 2048 |
parthchandak02 | 0:b723ff6df537 | 6 | #define M_PI 3.1415926535897932384 |
parthchandak02 | 0:b723ff6df537 | 7 | |
parthchandak02 | 0:b723ff6df537 | 8 | using namespace std; |
parthchandak02 | 0:b723ff6df537 | 9 | |
parthchandak02 | 0:b723ff6df537 | 10 | |
parthchandak02 | 0:b723ff6df537 | 11 | float s; |
parthchandak02 | 0:b723ff6df537 | 12 | float a = 1; //amplitude |
parthchandak02 | 0:b723ff6df537 | 13 | float pi = 3.142; //pi |
parthchandak02 | 0:b723ff6df537 | 14 | float o = 1/2; //offset |
parthchandak02 | 0:b723ff6df537 | 15 | float time_count = 0; |
parthchandak02 | 0:b723ff6df537 | 16 | |
parthchandak02 | 0:b723ff6df537 | 17 | int i = 0; //iteration counter |
parthchandak02 | 0:b723ff6df537 | 18 | int f1 = 100; //frequency in Hz |
parthchandak02 | 0:b723ff6df537 | 19 | //int f2 = 250; //frequency in Hz |
parthchandak02 | 0:b723ff6df537 | 20 | //int f3 = 500; //frequency in Hz |
parthchandak02 | 0:b723ff6df537 | 21 | float step = 1/(5*f1); |
parthchandak02 | 0:b723ff6df537 | 22 | float time[MAX] = 0; |
parthchandak02 | 0:b723ff6df537 | 23 | |
parthchandak02 | 0:b723ff6df537 | 24 | float* generateTimeVector (float* timeVec, int vecSize, float stepSize) |
parthchandak02 | 0:b723ff6df537 | 25 | { |
parthchandak02 | 0:b723ff6df537 | 26 | for(int i=1; i<=vecSize; i++) |
parthchandak02 | 0:b723ff6df537 | 27 | { |
parthchandak02 | 0:b723ff6df537 | 28 | timeVec[i] = i*stepSize; |
parthchandak02 | 0:b723ff6df537 | 29 | } |
parthchandak02 | 0:b723ff6df537 | 30 | return timeVec; |
parthchandak02 | 0:b723ff6df537 | 31 | } |
parthchandak02 | 0:b723ff6df537 | 32 | |
parthchandak02 | 0:b723ff6df537 | 33 | bool set1 = false; |
parthchandak02 | 0:b723ff6df537 | 34 | bool set2 = false; |
parthchandak02 | 0:b723ff6df537 | 35 | bool set3 = false; |
parthchandak02 | 0:b723ff6df537 | 36 | |
parthchandak02 | 0:b723ff6df537 | 37 | void sineVector (complex<double>* sineVector, int i, int freqHz, int amplitude, int offset); |
parthchandak02 | 0:b723ff6df537 | 38 | |
parthchandak02 | 0:b723ff6df537 | 39 | int log2(int N) /*function to calculate the log2(.) of int numbers*/ |
parthchandak02 | 0:b723ff6df537 | 40 | { |
parthchandak02 | 0:b723ff6df537 | 41 | int k = N, i = 0; |
parthchandak02 | 0:b723ff6df537 | 42 | while(k) { |
parthchandak02 | 0:b723ff6df537 | 43 | k >>= 1; |
parthchandak02 | 0:b723ff6df537 | 44 | i++; |
parthchandak02 | 0:b723ff6df537 | 45 | } |
parthchandak02 | 0:b723ff6df537 | 46 | return i - 1; |
parthchandak02 | 0:b723ff6df537 | 47 | } |
parthchandak02 | 0:b723ff6df537 | 48 | |
parthchandak02 | 0:b723ff6df537 | 49 | int check(int n) //checking if the number of element is a power of 2 |
parthchandak02 | 0:b723ff6df537 | 50 | { |
parthchandak02 | 0:b723ff6df537 | 51 | return n > 0 && (n & (n - 1)) == 0; |
parthchandak02 | 0:b723ff6df537 | 52 | } |
parthchandak02 | 0:b723ff6df537 | 53 | |
parthchandak02 | 0:b723ff6df537 | 54 | int reverse(int N, int n) //calculating revers number |
parthchandak02 | 0:b723ff6df537 | 55 | { |
parthchandak02 | 0:b723ff6df537 | 56 | int j, p = 0; |
parthchandak02 | 0:b723ff6df537 | 57 | for(j = 1; j <= log2(N); j++) { |
parthchandak02 | 0:b723ff6df537 | 58 | if(n & (1 << (log2(N) - j))) |
parthchandak02 | 0:b723ff6df537 | 59 | p |= 1 << (j - 1); |
parthchandak02 | 0:b723ff6df537 | 60 | } |
parthchandak02 | 0:b723ff6df537 | 61 | return p; |
parthchandak02 | 0:b723ff6df537 | 62 | } |
parthchandak02 | 0:b723ff6df537 | 63 | |
parthchandak02 | 0:b723ff6df537 | 64 | void ordina(complex<double>* f1, int N) //using the reverse order in the array |
parthchandak02 | 0:b723ff6df537 | 65 | { |
parthchandak02 | 0:b723ff6df537 | 66 | complex<double> f2[MAX]; |
parthchandak02 | 0:b723ff6df537 | 67 | for(int i = 0; i < N; i++) |
parthchandak02 | 0:b723ff6df537 | 68 | f2[i] = f1[reverse(N, i)]; |
parthchandak02 | 0:b723ff6df537 | 69 | for(int j = 0; j < N; j++) |
parthchandak02 | 0:b723ff6df537 | 70 | f1[j] = f2[j]; |
parthchandak02 | 0:b723ff6df537 | 71 | } |
parthchandak02 | 0:b723ff6df537 | 72 | |
parthchandak02 | 0:b723ff6df537 | 73 | void transform(complex<double>* f, int N) // |
parthchandak02 | 0:b723ff6df537 | 74 | { |
parthchandak02 | 0:b723ff6df537 | 75 | ordina(f, N); //first: reverse order |
parthchandak02 | 0:b723ff6df537 | 76 | complex<double> *W; |
parthchandak02 | 0:b723ff6df537 | 77 | W = (complex<double> *)malloc(N / 2 * sizeof(complex<double>)); |
parthchandak02 | 0:b723ff6df537 | 78 | W[1] = polar(1., -2. * M_PI / N); |
parthchandak02 | 0:b723ff6df537 | 79 | W[0] = 1; |
parthchandak02 | 0:b723ff6df537 | 80 | for(int i = 2; i < N / 2; i++) |
parthchandak02 | 0:b723ff6df537 | 81 | W[i] = pow(W[1], i); |
parthchandak02 | 0:b723ff6df537 | 82 | int n = 1; |
parthchandak02 | 0:b723ff6df537 | 83 | int a = N / 2; |
parthchandak02 | 0:b723ff6df537 | 84 | for(int j = 0; j < log2(N); j++) { |
parthchandak02 | 0:b723ff6df537 | 85 | for(int i = 0; i < N; i++) { |
parthchandak02 | 0:b723ff6df537 | 86 | if(!(i & n)) { |
parthchandak02 | 0:b723ff6df537 | 87 | complex<double> temp = f[i]; |
parthchandak02 | 0:b723ff6df537 | 88 | complex<double> Temp = W[(i * a) % (n * a)] * f[i + n]; |
parthchandak02 | 0:b723ff6df537 | 89 | f[i] = temp + Temp; |
parthchandak02 | 0:b723ff6df537 | 90 | f[i + n] = temp - Temp; |
parthchandak02 | 0:b723ff6df537 | 91 | } |
parthchandak02 | 0:b723ff6df537 | 92 | } |
parthchandak02 | 0:b723ff6df537 | 93 | n *= 2; |
parthchandak02 | 0:b723ff6df537 | 94 | a = a / 2; |
parthchandak02 | 0:b723ff6df537 | 95 | } |
parthchandak02 | 0:b723ff6df537 | 96 | } |
parthchandak02 | 0:b723ff6df537 | 97 | |
parthchandak02 | 0:b723ff6df537 | 98 | void FFT(complex<double>* f, int N, double d) |
parthchandak02 | 0:b723ff6df537 | 99 | { |
parthchandak02 | 0:b723ff6df537 | 100 | transform(f, N); |
parthchandak02 | 0:b723ff6df537 | 101 | for(int i = 0; i < N; i++) |
parthchandak02 | 0:b723ff6df537 | 102 | f[i] *= d; //multiplying by step |
parthchandak02 | 0:b723ff6df537 | 103 | } |
parthchandak02 | 0:b723ff6df537 | 104 | |
parthchandak02 | 0:b723ff6df537 | 105 | int main() |
parthchandak02 | 0:b723ff6df537 | 106 | { |
parthchandak02 | 0:b723ff6df537 | 107 | complex<double> vec[MAX]; |
parthchandak02 | 0:b723ff6df537 | 108 | for (i = 0; i < MAX; i++) |
parthchandak02 | 0:b723ff6df537 | 109 | { |
parthchandak02 | 0:b723ff6df537 | 110 | sineVector(vec, i, f1, a, 0); |
parthchandak02 | 0:b723ff6df537 | 111 | } |
parthchandak02 | 0:b723ff6df537 | 112 | |
parthchandak02 | 0:b723ff6df537 | 113 | FFT(vec, MAX, 1); //'d' should be 1 in order to have the same results of matlab fft(.) |
parthchandak02 | 0:b723ff6df537 | 114 | cout << "...printing the FFT of the array specified" << endl; |
parthchandak02 | 0:b723ff6df537 | 115 | for(int j = 0; j < MAX; j++) |
parthchandak02 | 0:b723ff6df537 | 116 | { |
parthchandak02 | 0:b723ff6df537 | 117 | cout << vec[j] << endl; |
parthchandak02 | 0:b723ff6df537 | 118 | } |
parthchandak02 | 0:b723ff6df537 | 119 | |
parthchandak02 | 0:b723ff6df537 | 120 | return 0; |
parthchandak02 | 0:b723ff6df537 | 121 | } |
parthchandak02 | 0:b723ff6df537 | 122 | |
parthchandak02 | 0:b723ff6df537 | 123 | |
parthchandak02 | 0:b723ff6df537 | 124 | void sineVector (complex<double>* sineVector, int i, int freqHz, int amplitude, int offset) |
parthchandak02 | 0:b723ff6df537 | 125 | { |
parthchandak02 | 0:b723ff6df537 | 126 | sineVector[i] = offset + amplitude*sin((pi/180) * freqHz * i); |
parthchandak02 | 0:b723ff6df537 | 127 | } |