fft_implementation_draft1

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
parthchandak02
Date:
Sun Mar 25 18:50:02 2018 +0000
Parent:
0:b723ff6df537
Commit message:
mbed LPC1768 C FFT Function Revision 1

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Mar 07 11:06:31 2018 +0000
+++ b/main.cpp	Sun Mar 25 18:50:02 2018 +0000
@@ -2,40 +2,61 @@
 
 #include <iostream>
 #include <complex>
-#define MAX 2048
+#include <cmath>
+
+#define MAX 256
 #define M_PI 3.1415926535897932384
 
+
+Serial pc(USBTX, USBRX);
 using namespace std;
 
 
 float s;
-float a = 1; //amplitude
+float a = 1.0; //amplitude
 float pi = 3.142; //pi
-float o = 1/2; //offset
-float time_count = 0;
+float o = 1; //offset
+float time_count = 0.0;
 
 int i = 0; //iteration counter
 int f1 = 100; //frequency in Hz
-//int f2 = 250; //frequency in Hz
-//int f3 = 500; //frequency in Hz
-float step = 1/(5*f1);
-float time[MAX] = 0;
+
+float step = 1.0/(10.0*f1);
 
-float* generateTimeVector (float* timeVec, int vecSize, float stepSize)
+float* generateTimeVector (int vecSize, float stepSize)
 {
+//    pc.printf("\nGenerating Time Vector...\n");
+    float *timeVec = new float[vecSize];
     for(int i=1; i<=vecSize; i++)
     {
         timeVec[i] = i*stepSize;
+//        pc.printf("%f\n",timeVec[i]);
     }
     return timeVec;  
 }
 
+float* generateFreqVector(int vecSize, float stepSize)
+{
+//    pc.printf("\nGenerating Frequency Vector...\n");
+    float *freqVec = new float[vecSize];
+    float v = vecSize;
+    for(int i=1; i<=(vecSize); i++)
+    {
+        freqVec[i] = (1.0/stepSize)*(i/v);
+//        pc.printf("%f\n",freqVec[i]);
+    }
+    return freqVec;  
+}
+
 bool set1 = false;
 bool set2 = false;
 bool set3 = false;
 
-void sineVector (complex<double>* sineVector, int i, int freqHz, int amplitude, int offset);
-
+void sineGenerate(complex<double>* sineVector, int index, float time, int freqHz, int amplitude, int offset)
+{
+       sineVector[index] = offset + amplitude*sin((2*pi) * freqHz * time);
+//       pc.printf("%d, %f\n", index, sineVector[index]);
+}
 int log2(int N)    /*function to calculate the log2(.) of int numbers*/
 {
   int k = N, i = 0;
@@ -104,24 +125,24 @@
 
 int main()
 {
+  float* timeVector = generateTimeVector (MAX, step);
+  float* freqVector = generateFreqVector (MAX, step);
+  
   complex<double> vec[MAX];
   for (i = 0; i < MAX; i++)
   {
-      sineVector(vec, i, f1, a, 0);
+      float t = timeVector[i];
+      sineGenerate(vec, i, t, f1, a, 0);
   }
   
   FFT(vec, MAX, 1); //'d' should be 1 in order to have the same results of matlab fft(.)
   cout << "...printing the FFT of the array specified" << endl;
+  float absFFT = 0.0;
   for(int j = 0; j < MAX; j++)
   {
-     cout << vec[j] << endl; 
+     absFFT = 2*(std::abs(vec[j]))/MAX;
+     pc.printf("%f, %f\n", freqVector[j], absFFT); //Y: absFFT, X: freqVector
   }
     
   return 0;
 }
-
-
-void sineVector (complex<double>* sineVector, int i, int freqHz, int amplitude, int offset)
-{
-       sineVector[i] = offset + amplitude*sin((pi/180) * freqHz * i);
-}