Jaspreet Singh / Mbed 2 deprecated fft_merge_main_codev1_FTPGTET

Dependencies:   SDFileSystem dspmodified mbed

Fork of fft_merge_main_codev1 by Testbed

Revision:
7:57454a466b98
Parent:
3:ae644cac8e0b
Child:
8:61686460d0e7
--- a/main.cpp	Sat Jan 24 12:43:17 2015 +0000
+++ b/main.cpp	Sun Jan 25 09:09:45 2015 +0000
@@ -1,3 +1,7 @@
+/*
+Designed to work from year 2015 to 2020
+*/
+
 #include "mbed.h"
 #include "arm_math.h"
 #include "SDFileSystem.h"
@@ -16,7 +20,7 @@
 AnalogIn TC2(A5);
 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");
 
-char sitename[100] ="MODZ";
+char sitename[100] ="NTF1";
 char filename[100] = "";
 char filepath[100] = "";
 char timestamp[100]="";
@@ -29,6 +33,7 @@
 char pathfft[100]="";
 char filenameAC[100]   ="";
 char filenameDC[100]   ="";
+char filenamefft[100]   ="";
 char clk_response[100] ="";
 int m=0,mfinal;
 int date, month, year, hour, minute, second, temp = 0;
@@ -52,124 +57,29 @@
 //--------------------------------------------------------------------------------------------------//
 void reset_mod()
 {
-    NVIC_SystemReset();     
-}
-
-//--------------------------------------------------------------------------------------------------//
-//                  Send AT Command (1 response with clk variable)                                                    //
-//--------------------------------------------------------------------------------------------------//
-int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout, bool clk_flag)
-{
-    uint8_t x=0,answer=0;                                                                           //x=0 and answer=0, if not put, AT+CCLK?\r is not executed
-    char response[100];
-    memset(response, '\0', 100);                                                                    // Initialize the string
-    wait_ms(100);
-    while(gsm.readable() > 0) gsm.getc();                                                          // Clean the input buffer
-    gsm.printf("%s \r", ATcommand);                                                                 // Send the AT command 
-    x = 0;
-    previous = time(NULL);
-    do                                                                                              // this loop waits for the answer
-    { 
-        if(gsm.readable() != 0)
-        {    
-            response[x] = gsm.getc();                                                               // if there are data in the UART input buffer, reads it and checks for the asnwer 
-            pc.putc(response[x]);
-            x++;
-            if (strstr(response, expected_answer) != NULL)                                          // check if the desired answer  is in the response of the module
-            {
-                answer = 1;
-            } 
-        }
-    }
-    while((answer == 0) && ((time(NULL) - previous) < timeout));                                    // Waits for the asnwer with time out
-    if(clk_flag == 1)
-    {
-        sprintf(timestamp,response);                                                                //copies response to timestamp for further processing    }
-    }
-    return answer;
-}
-
-//--------------------------------------------------------------------------------------------------//
-//                  Getting File Directories and names                                              //
-//--------------------------------------------------------------------------------------------------//   
-void GetFileDir()
-{
-    int attempt_cnt=0;
-    char month_dir[100]="";
-    char date_dir[100] ="";
-    sendATcommand("AT+CCLK?\r", "OK", 5,1);  
-    //sprintf(response,timestamp);
-    x=20;
-    year = 10*(timestamp[x+1]-48) + (timestamp[x+2]-48);
-    while(year<15||year>20)
-    {
-        sendATcommand("AT+CCLK?\r", "OK", 5,0);  
-        year = 10*(timestamp[x+1]-48) + (timestamp[x+2]-48);
-        attempt_cnt++;
-        if(attempt_cnt>4) break; 
-    }
-    if(year>14&&year<21)
-    {
-        if(timestamp!=timestampold)
-        {
-            sprintf(timestampold,timestamp);
-            //pc.printf("\r\nold time stamp is%s\r\n",timestampold);
-            month = (timestamp[x+4]-48)*10+(timestamp[x+5]-48);
-            date = (timestamp[x+7]-48)*10+(timestamp[x+8]-48);
-            hour = (timestamp[x+10]-48)*10+(timestamp[x+11]-48);
-            minute = (timestamp[x+13]-48)*10+(timestamp[x+14]-48);
-            second = (timestamp[x+16]-48)*10+(timestamp[x+17]-48); 
-            
-            sprintf(main_dir,"/sd/%s",sitename);
-            sprintf(month_dir,"%s/%s%02d%02d",main_dir,sitename,year,month);
-            sprintf(date_dir,"%s/%s%02d%02d%02d",month_dir,sitename,year,month,date);
-            sprintf(hour_dir,"%s/%s%02d%02d%02d%02d",date_dir,sitename,year,month,date,hour);
-            sprintf(filepath,"%s/%s%02d%02d%02d%02d%02d%02d",hour_dir,sitename,year,month,date,hour,minute,second);
-            sprintf(filename,"%s%02d%02d%02d%02d%02d%02d",sitename,year,month,date,hour,minute,second);
-            gsmerr=0;
-            mkdir(main_dir,0777);
-            mkdir(month_dir,0777);
-            mkdir(date_dir,0777);
-            mkdir(hour_dir,0777);
-        }
-        else
-        {
-            sprintf(filepath,"%s/%s%02d%02d%02d%02d%02d%02dErr%03d",hour_dir,sitename,year,month,date,hour,minute,second,gsmerr);
-            sprintf(filename,"%s%02d%02d%02d%02d%02d%02dErr%03d",sitename,year,month,date,hour,minute,second,gsmerr);
-            gsmerr++;
-        }
-    }
-    else
-    {
-        sprintf(timestamp,timestampold);
-        sprintf(filepath,"%s/%s%02d%02d%02d%02d%02d%02dErr%03d",hour_dir,sitename,year,month,date,hour,minute,second,gsmerr);
-        sprintf(filename,"%s%02d%02d%02d%02d%02d%02dErr%03d",sitename,year,month,date,hour,minute,second,gsmerr);
-        gsmerr++;
-    }
+    NVIC_SystemReset();
 }
 
 //--------------------------------------------------------------------------------------------------//
 //                  DC Detect                                                                       //
 //--------------------------------------------------------------------------------------------------//
 void DC_Detection()
-{   
+{
     Timer t;
-    unsigned long j=0;
-    
+    int j=0;
+
     t.start();
-    while(j<10)
-    {        
-        if((int)(t.read()*50*freq*1000)%100 == 0)
-        {
+    while(j<10) {
+        if((int)(t.read()*50*freq*1000)%100 == 0) {
             DC_Detect_data[j] = DC_Detect.read_u16();
             ++j;
         }
     }
     t.stop();
-   // pc.printf("\r\nTemp Time Taken %f \n\r", t.read());
+    // pc.printf("\r\nTemp Time Taken %f \n\r", t.read());
     t.reset();
-   // pc.printf("Total Samples %d\r\n",j);
-   //osSignalSet(tid3, 0x1); 
+    // pc.printf("Total Samples %d\r\n",j);
+    //osSignalSet(tid3, 0x1);
 }
 
 //--------------------------------------------------------------------------------------------------//
@@ -178,13 +88,11 @@
 void calculate_ADC()
 {
     Timer t;
-    unsigned long j=0;
+    int j=0;
     //pc.printf("\n\rStarting Reading \n\r");
     t.start();
-    while(j<dataLength)
-    {        
-        if((int)(t.read()*100*freq*1000)%100 == 0)
-        {
+    while(j<dataLength) {
+        if((int)(t.read()*100*freq*1000)%100 == 0) {
             ACdata[j] = AC_input.read_u16();
             DCdata[j] = DC_input.read_u16();
             ++j;
@@ -206,110 +114,191 @@
     //tid1 = osThreadGetId();
     //while(true)
     //{
-            pc.printf("AC sampling attempt");
-            calculate_ADC();
-            DC_Detection();
-            /*for(int i=0;i<30; i++)
-             { 
-               Thread::wait(60000);
-             }  
+    pc.printf("\r\nSampling attempt");
+    calculate_ADC();
+    DC_Detection();
+    /*for(int i=0;i<30; i++)
+     {
+       Thread::wait(60000);
+     }
     }*/
 }
-    
-void fft(short N, short start_index, short step, int loop){
-   if(N==2048){
+
+//--------------------------------------------------------------------------------------------------//
+//                  Send AT Command (1 response with clk variable)                                                    //
+//--------------------------------------------------------------------------------------------------//
+int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout, bool clk_flag)
+{
+    uint8_t x=0,answer=0;                                                                           //x=0 and answer=0, if not put, AT+CCLK?\r is not executed
+    char response[100];
+    memset(response, '\0', 100);                                                                    // Initialize the string
+    wait_ms(100);
+    while(gsm.readable() > 0) gsm.getc();                                                          // Clean the input buffer
+    gsm.printf("%s \r", ATcommand);                                                                 // Send the AT command
+    x = 0;
+    previous = time(NULL);
+    do {                                                                                            // this loop waits for the answer
+        if(gsm.readable() != 0) {
+            response[x] = gsm.getc();                                                               // if there are data in the UART input buffer, reads it and checks for the asnwer
+            pc.putc(response[x]);
+            x++;
+            if (strstr(response, expected_answer) != NULL) {                                        // check if the desired answer  is in the response of the module
+                answer = 1;
+            }
+        }
+    } while((answer == 0) && ((time(NULL) - previous) < timeout));                                  // Waits for the asnwer with time out
+    if(clk_flag == 1) {
+        sprintf(timestamp,response);                                                                //copies response to timestamp for further processing    }
+    }
+    return answer;
+}
+
+//--------------------------------------------------------------------------------------------------//
+//                  Getting File Directories and names                                              //
+//--------------------------------------------------------------------------------------------------//
+void GetFileDir()
+{
+    int attempt_cnt=0;
+    char month_dir[100]="";
+    char date_dir[100] ="";
+    sendATcommand("AT+CCLK?\r", "OK", 5,1);
+    //sprintf(response,timestamp);
+    x=20;
+    year = 10*(timestamp[x+1]-48) + (timestamp[x+2]-48);
+    while(year<15||year>20) {
+        sendATcommand("AT+CCLK?\r", "OK", 5,0);
+        year = 10*(timestamp[x+1]-48) + (timestamp[x+2]-48);
+        attempt_cnt++;
+        if(attempt_cnt>4) break;
+    }
+    if(year>14&&year<21) {
+        if(timestamp!=timestampold) {
+            sprintf(timestampold,timestamp);
+            //pc.printf("\r\nold time stamp is%s\r\n",timestampold);
+            month = (timestamp[x+4]-48)*10+(timestamp[x+5]-48);
+            date = (timestamp[x+7]-48)*10+(timestamp[x+8]-48);
+            hour = (timestamp[x+10]-48)*10+(timestamp[x+11]-48);
+            minute = (timestamp[x+13]-48)*10+(timestamp[x+14]-48);
+            second = (timestamp[x+16]-48)*10+(timestamp[x+17]-48);
+
+            sprintf(main_dir,"/sd/%s",sitename);
+            sprintf(month_dir,"%s/%s%02d%02d",main_dir,sitename,year,month);
+            sprintf(date_dir,"%s/%s%02d%02d%02d",month_dir,sitename,year,month,date);
+            sprintf(hour_dir,"%s/%s%02d%02d%02d%02d",date_dir,sitename,year,month,date,hour);
+            sprintf(filepath,"%s/%s%02d%02d%02d%02d%02d%02d",hour_dir,sitename,year,month,date,hour,minute,second);
+            sprintf(filename,"%s%02d%02d%02d%02d%02d%02d",sitename,year,month,date,hour,minute,second);
+            gsmerr=0;
+            mkdir(main_dir,0777);
+            mkdir(month_dir,0777);
+            mkdir(date_dir,0777);
+            mkdir(hour_dir,0777);
+        } else {
+            sprintf(filepath,"%s/%s%02d%02d%02d%02d%02d%02dErr%03d",hour_dir,sitename,year,month,date,hour,minute,second,gsmerr);
+            sprintf(filename,"%s%02d%02d%02d%02d%02d%02dErr%03d",sitename,year,month,date,hour,minute,second,gsmerr);
+            gsmerr++;
+        }
+    } else {
+        sprintf(timestamp,timestampold);
+        sprintf(filepath,"%s/%s%02d%02d%02d%02d%02d%02dErr%03d",hour_dir,sitename,year,month,date,hour,minute,second,gsmerr);
+        sprintf(filename,"%s%02d%02d%02d%02d%02d%02dErr%03d",sitename,year,month,date,hour,minute,second,gsmerr);
+        gsmerr++;
+    }
+}
+
+
+void fft(short N, short start_index, short step, int loop)
+{
+    if(N==2048) {
         float input_2048[2048];
-        for (short i=0; i<2048; i++){
+        for (short i=0; i<2048; i++) {
             input_2048[i]=float(fft_array[(start_index+i*step)]);
-           }
+        }
         float output_4096[4096];
         arm_rfft_instance_f32 SR;
-        arm_cfft_radix4_instance_f32 SR_CFFT; 
+        arm_cfft_radix4_instance_f32 SR_CFFT;
         arm_status status=arm_rfft_init_f32(&SR, &SR_CFFT, 2048, 0, 1);
         arm_rfft_f32(&SR, input_2048, output_4096);
-        for (short i=0; i<4096; i++){
+        for (short i=0; i<4096; i++) {
             fft_array[start_index+i*step]=int(output_4096[i]);
-            }
+        }
         return;
-        }
-    else{
-        fft(N/2, start_index, step*2,0);      
+    } else {
+        fft(N/2, start_index, step*2,0);
         fft(N/2, start_index+step, step*2,0);
         float output_data1[2];
         float output_data2[2];
         float output_data3[2];
-        float output_data4[2];       
+        float output_data4[2];
         output_data3[0]=fft_array[start_index];
         output_data3[1]=fft_array[start_index+2*step];
         output_data4[0]=fft_array[start_index+step];
         output_data4[1]=fft_array[start_index+3*step];
         fft_array[start_index]=output_data3[0]+output_data4[0];
         fft_array[start_index+step]=output_data3[1]+output_data4[1];
-        for(int i=1;i<N/2;i++){         
+        for(int i=1; i<N/2; i++) {
             output_data1[0]=float(fft_array[start_index+4*(i)*step]);
             output_data1[1]=float(fft_array[start_index+(4*(i)+2)*step]);
             output_data2[0]=float(fft_array[start_index+(4*(i)+1)*step]);
             output_data2[1]=float(fft_array[start_index+(4*(i)+3)*step]);
             fft_array[start_index+2*i*step]=int(output_data1[0]+(output_data2[0]*cos(2*i*3.142/N))-(output_data2[1]*sin(-2*i*3.142/N)));
             fft_array[start_index+(2*i+1)*step]=int(output_data1[1]+(output_data2[0]*sin(-2*i*3.142/N))+(output_data2[1]*cos(2*i*3.142/N)));
-            }
+        }
         fft_array[start_index+N*step]=output_data3[0]-output_data4[0];
-        fft_array[start_index+(N+1)*step]=output_data3[1]-output_data4[1];    
-        if(loop==0){
-            for(int i=N/2+1;i<N;i++){
+        fft_array[start_index+(N+1)*step]=output_data3[1]-output_data4[1];
+        if(loop==0) {
+            for(int i=N/2+1; i<N; i++) {
                 fft_array[start_index+2*i*step]=fft_array[start_index+2*(N-i)*step];
                 fft_array[start_index+(2*i+1)*step]=-1*fft_array[start_index+(2*(N-i)+1)*step];
-                }
             }
-        if(loop==1){
-            for(short i=0;i<8193;i++){
+        }
+        if(loop==1) {
+            for(short i=0; i<8193; i++) {
                 fft_array[i]=int(sqrt((double(fft_array[2*i])*double(fft_array[2*i]))+(double(fft_array[2*i+1])*double(fft_array[2*i+1]))));
-                }
             }
-        return; 
         }
+        return;
     }
-void fft_max(int start_index, int number_count, int maximal_count, uint32_t* maximum_index, float* maximum_value){
+}
+void fft_max(int start_index, int number_count, int maximal_count, uint32_t* maximum_index, float* maximum_value)
+{
     float maximal_array[number_count];
-    for (int i=0;i<number_count;i++){
+    for (int i=0; i<number_count; i++) {
         maximal_array[i]=float(fft_array[start_index+i]);
-        }
-    for(int i=0;i<maximal_count;i++){
-        arm_max_f32(maximal_array,number_count,&maximum_value[i],&maximum_index[i]);    
+    }
+    for(int i=0; i<maximal_count; i++) {
+        arm_max_f32(maximal_array,number_count,&maximum_value[i],&maximum_index[i]);
         maximal_array[maximum_index[i]]=0;
         maximum_index[i]=start_index+maximum_index[i];
-        }    
     }
+}
 
-void prepare_fft_array(int* start_index, int* points_count, int* maximas_required, int segments){
+void prepare_fft_array(int* start_index, int* points_count, int* maximas_required, int segments, int fft_type)
+{
     int max_points_count=0;
-    for (int i=0;i<segments;i++){
+    for (int i=0; i<segments; i++) {
         max_points_count=max_points_count+maximas_required[i];
-        }
+    }
     uint32_t index[max_points_count];
     float maxima[max_points_count];
     int j=0;
-    for (int i=0;i<segments;i++){
-        fft_max(start_index[i],points_count[i],maximas_required[i],&index[j],&maxima[j]);    
+    for (int i=0; i<segments; i++) {
+        fft_max(start_index[i],points_count[i],maximas_required[i],&index[j],&maxima[j]);
         j=j+maximas_required[i];
-        }
+    }
     FILE *fp = fopen(pathfft, "a");
-        if(fp == NULL)
-            {
-                pc.printf("Could not open file for write\n\r");
-                //reset_mod();
-            }
-        else
-            {
-                //fprintf(fp, "\r\n%s",timestamp);
-                for(int i=0;i<max_points_count;i++)
-                    {
-                        fprintf(fp,"\r\n %d %d",index[i], int(maxima[i]));
-                        }             
-                fprintf(fp,"\r\n\r\n");
-                fclose(fp);
-            }
+    if(fp == NULL) {
+        pc.printf("Could not open file for write\n\r");
+        //reset_mod();
+    } else {
+        //fprintf(fp, "\r\n%s",timestamp);
+        for(int i=0; i<max_points_count; i++) {
+            fprintf(fp,"\r\n %d %d",index[i], int(maxima[i]));
+        }
+        if(fft_type==0) fprintf(fp,"\r\n\r\n");
+        else fprintf(fp,"\r\nEOF");
+        fclose(fp);
     }
+}
 
 //--------------------------------------------------------------------------------------------------//
 //                  Storage                                                                         //
@@ -320,132 +309,271 @@
     while(true)
     {
         osSignalWait(0x1, osWaitForever);*/
-        GetFileDir();
-        sprintf(filenameAC,"%sAC.txt",filename);
-        sprintf(pathAC,"%sAC.txt",filepath);
-        pc.printf("\r\nFilename-AC\t%s", pathAC);
-        FILE *fp = fopen(pathAC, "w");
-        if(fp == NULL)
-            {
-            pc.printf("Could not open file for write\n\r");
-               //reset_mod();
-            }
-        else
-            {
-             for(unsigned long k=0; k<dataLength; ++k)
-                 {
-                  fprintf(fp, "%d.", ACdata[k]);
-                 }
-                    fprintf(fp, "EOF");
-              fclose(fp);                      
-            }
-        sprintf(filenameDC,"%sDC.txt",filename);
-        sprintf(pathDC,"%sDC.txt",filepath);
-        pc.printf("\r\nFilename-DC\t%s", pathDC);
-        fp = fopen(pathDC, "w");
-        if(fp == NULL)
-            {
-                pc.printf("Could not open file for write\n\r");
-                //reset_mod();
-            }
-        else
-            {
-                for(unsigned long k=0; k<dataLength; ++k)
-                    {
-                        fprintf(fp, "%d.", DCdata[k]);
-                    }
-                fprintf(fp, "EOF");
-                fclose(fp);
-            }
-        sprintf(pathDCdetect,"%sData.txt",hour_dir);
-        pc.printf("\r\nFilename-TC\t%s", pathDCdetect);    
-        fp = fopen(pathDCdetect, "w");
-        if(fp == NULL)
-            {
-                pc.printf("Could not open file for write\n\r");
-                //reset_mod();
-            }
-        else
-            {
-                fprintf(fp, "\r\n%s",timestamp);
-                for(unsigned long k=0; k<10; ++k)
-                    {
-                        fprintf(fp, "\r\nDC-%d",DC_Detect_data[k]);
-                    }             
-                fclose(fp);
-            }
-        
-        int avg_dc_detect=0;
-        for(int i=0;i<10;i++){
-            avg_dc_detect=avg_dc_detect+int(DC_Detect_data[i]);
-            }
-        avg_dc_detect=avg_dc_detect/10;
-        pc.printf("\r\nfftstart");
-        
-        //pc.printf("\r\nfftstage1");
-        sprintf(pathfft,"%sfftAC.txt",filepath);
-        fp = fopen(pathfft, "w");
-        if(fp == NULL)
-            {
-                pc.printf("Could not open file for write\n\r");
-                //reset_mod();
-            }
-        else
-            {
-                fprintf(fp, "\r\n%s",timestamp);           
-                fprintf(fp, "\r\n DCDetect %d",avg_dc_detect);
-                fclose(fp);
-            }
-        
-        int fft_segments=4;
-        int fft_start_index[4]={0,775,1600,2430};
-        int fft_points_count[4]={4,80,80,100};
-        int fft_maximas_required[4]={1,20,10,10};
-        //AC fft
-        for(int i=0;i<fft_points;i++){
-            fft_array[i]=int(ACdata[i]);
-            //pc.printf("%d %d %d\r\n", i, i+int(fft_points), fft_array[i]);
-            fft_array[(i+int(fft_points))]=0;
-            }
-        fft(fft_points,0,1,1);
-        pc.printf("\r\nfftdone");
-        prepare_fft_array(fft_start_index,fft_points_count,fft_maximas_required,fft_segments);
-        //DC fft
-        for(int i=0;i<fft_points;i++){
-            fft_array[i]=int(DCdata[i]);
-            //pc.printf("%d %d %d\r\n", i, i+int(fft_points), fft_array[i]);
-            fft_array[(i+int(fft_points))]=0;
-            }
-        fft(fft_points,0,1,1);
-        pc.printf("\r\nfftdone");
-        prepare_fft_array(fft_start_index,fft_points_count,fft_maximas_required,fft_segments);
-        
-        pc.printf("\n storage done");
-        //osSignalSet(tid6, 0x1); 
-              
-   // }
+    GetFileDir();
+    sprintf(filenameAC,"%sAC.txt",filename);
+    sprintf(pathAC,"%sAC.txt",filepath);
+    pc.printf("\r\nFilename-AC\t%s", pathAC);
+    FILE *fp = fopen(pathAC, "w");
+    if(fp == NULL) {
+        pc.printf("Could not open file for write\n\r");
+        //reset_mod();
+    } else {
+        pc.printf("attempting to save file\n\r");
+        for(int k=0; k<dataLength; k++) {
+            fprintf(fp, "%d.", ACdata[k]);
+        }
+        fprintf(fp, "EOF");
+        fclose(fp);
+    }
+    sprintf(filenameDC,"%sDC.txt",filename);
+    sprintf(pathDC,"%sDC.txt",filepath);
+    pc.printf("\r\nFilename-DC\t%s", pathDC);
+    fp = fopen(pathDC, "w");
+    if(fp == NULL) {
+        pc.printf("Could not open file for write\n\r");
+        //reset_mod();
+    } else {
+        for(int k=0; k<dataLength; k++) {
+            fprintf(fp, "%d.", DCdata[k]);
+        }
+        fprintf(fp, "EOF");
+        fclose(fp);
+    }
+    sprintf(pathDCdetect,"%sData.txt",hour_dir);
+    pc.printf("\r\nFilename-TC\t%s", pathDCdetect);
+    fp = fopen(pathDCdetect, "w");
+    if(fp == NULL) {
+        pc.printf("Could not open file for write\n\r");
+        //reset_mod();
+    } else {
+        fprintf(fp, "\r\n%s",timestamp);
+        for(int k=0; k<10; k++) {
+            fprintf(fp, "\r\nDC-%d",DC_Detect_data[k]);
+        }
+        fclose(fp);
+    }
+
+    int avg_dc_detect=0;
+    for(int i=0; i<10; i++) {
+        avg_dc_detect=avg_dc_detect+int(DC_Detect_data[i]);
+    }
+    avg_dc_detect=avg_dc_detect/10;
+    pc.printf("\r\nfftstart");
+
+    //pc.printf("\r\nfftstage1");
+    sprintf(filenamefft,"%sft.txt",filename);
+    sprintf(pathfft,"%sft.txt",filepath);
+    pc.printf("\r\nFilename-FFT\t%s", pathfft);
+    fp = fopen(pathfft, "w");
+    if(fp == NULL) {
+        pc.printf("Could not open file for write\n\r");
+        //reset_mod();
+    } else {
+        fprintf(fp, "\r\n%s",timestamp);
+        fprintf(fp, "\r\n DCDetect %d",avg_dc_detect);
+        fclose(fp);
+    }
+
+    int fft_segments=4;
+    int fft_start_index[4]= {0,760,1520,2280};
+    int fft_points_count[4]= {4,120,240,360};
+    int fft_maximas_required[4]= {1,20,8,8};
+    //AC fft
+    for(int i=0; i<fft_points; i++) {
+        fft_array[i]=int(ACdata[i]);
+        //pc.printf("%d %d %d\r\n", i, i+int(fft_points), fft_array[i]);
+        fft_array[(i+int(fft_points))]=0;
+    }
+    fft(fft_points,0,1,1);
+    pc.printf("\r\nfftdone");
+    prepare_fft_array(fft_start_index,fft_points_count,fft_maximas_required,fft_segments,0);
+    //DC fft
+    for(int i=0; i<fft_points; i++) {
+        fft_array[i]=int(DCdata[i]);
+        //pc.printf("%d %d %d\r\n", i, i+int(fft_points), fft_array[i]);
+        fft_array[(i+int(fft_points))]=0;
+    }
+    fft(fft_points,0,1,1);
+    pc.printf("\r\nfftdone");
+    prepare_fft_array(fft_start_index,fft_points_count,fft_maximas_required,fft_segments,1);
+
+    pc.printf("\n storage done");
+    //osSignalSet(tid6, 0x1);
+
+    // }
 }
 
-int main(){
-    pc.printf("\r\nHelloWorld\r\n");   
-    sendATcommand("AT+CLTS=1\r", "OK", 5,0); 
+//--------------------------------------------------------------------------------------------------//
+//                 GSM Initialization                                                               //
+//--------------------------------------------------------------------------------------------------//
+void gsm_init()
+{
+    int cnt = 0;
+    while((sendATcommand("AT+CREG?", "+CREG: 1,1", 5,0) || sendATcommand("AT+CREG?", "+CREG: 1,5", 5,0)) == 0) {
+        if(sendATcommand("AT+CREG=1", "+CREG:1", 5,0)) {
+            pc.printf("creg is 1");
+            return;
+        } else if(sendATcommand("AT+CREG=1", "+CREG:5", 5,0)) {
+            pc.printf("creg is 5");
+            return;
+        }
+        ++cnt;
+        if(cnt == 4) {
+            pc.printf("GSM registration failed");
+            return;
+        }
+    }
+}
+//--------------------------------------------------------------------------------------------------//
+//                 FTP Connect function                                                             //
+//--------------------------------------------------------------------------------------------------//
+bool ftp_connect()
+{
+    int ftp_connect_attempt=0;
+    int ftp_close_attempt=0;
+    if(sendATcommand("AT+SAPBR=2,1", "+SAPBR: 1,3,\"0.0.0.0\"", 5,0)) {
+    } else {
+        while(!sendATcommand("AT+SAPBR=2,1", "+SAPBR: 1,3,\"0.0.0.0\"", 5,0)) {
+            sendATcommand("AT+SAPBR=0,1","OK",5,0);
+            ftp_close_attempt++;
+            if(ftp_close_attempt>3) {
+                return 0;
+            }
+        }
+    }
+    while(sendATcommand("AT+SAPBR=2,1", "+SAPBR: 1,3,\"0.0.0.0\"", 5,0)) {
+        sendATcommand("AT+SAPBR=1,1", "OK", 10,0);
+        ftp_connect_attempt++;
+        if(ftp_connect_attempt>3) {
+            return 0;
+        }
+    }
+    sendATcommand("AT+FTPTYPE=\"I\"", "OK", 5,0);
+    sendATcommand("AT+FTPSERV=\"ftp.panchsheel.biz\"", "OK", 10,0);
+    sendATcommand("AT+FTPUN=\"hardware@panchsheel.biz\"", "OK\r", 10,0);
+    if(sendATcommand("AT+FTPPW=\"DAStest123\"", "OK\r", 10,0)) return 1;
+    else return 0;
+}
+
+//--------------------------------------------------------------------------------------------------//
+//                 FTP Send Function                                                                //
+//--------------------------------------------------------------------------------------------------//
+
+void ftp_put()
+{
+    char ftpfilename[30]="";
+    char ftpfilepath[100]="";
+    sprintf(ftpfilename,"%s",filenamefft);
+    sprintf(ftpfilepath,"%s",pathfft);
+    pc.printf("\r\nftp_put attempt\n\r");
+    char command1[40];
+    char result1[20];
+    sprintf(command1,"%s\"%s\"", "AT+FTPPUTNAME=", ftpfilename);
+    sprintf(result1,"%s", "OK");
+    //sd card
+    int char_cnt=0;
+    char file_data[1294];
+    FILE *fp8 = fopen(ftpfilepath, "r");
+    if(fp8==NULL) {
+        pc.printf("\n\r NULL Condition\r\n");
+        return;
+    } else {
+        for(int i=0; i<1294; i++) {
+            file_data[i] = fgetc(fp8);
+            char_cnt= char_cnt++;
+            if( feof(fp8) ) {
+                break ;
+            }
+        }//size calculation loop ends
+        fclose(fp8);
+    }
+    for(int putname_attempt=0; putname_attempt<3; putname_attempt++) {
+        if(sendATcommand(command1,result1, 10,0)) {
+            //  pc.printf("\n %p",ftpfilename);
+            sendATcommand("AT+FTPPUTPATH=\"/Trial//\"", "OK", 5,0);
+            if(sendATcommand("AT+FTPPUT=1", "OK", 15,0)) {
+                // pc.printf("in loop");
+                // pc.printf("\n\r %s",ftpfilepath);
+                /*FILE *fp9 = fopen(ftpfilepath, "r");
+                if(fp9==NULL)return;
+                else
+                {*/
+                int trialcount=0;
+                int allowedtrials=4;
+                char command[40];
+                char result[30];
+                sprintf(command,"%s%d", "AT+FTPPUT=2,",char_cnt);
+                sprintf(result,"%s%d", "+FTPPUT:2,",char_cnt);
+                while(trialcount<allowedtrials) {
+                    if(sendATcommand(command,result, 10,0)) {
+                        trialcount=allowedtrials+4;
+                        for(int i=0; i<char_cnt; i++) {
+                            gsm.printf("%c", file_data[i]);
+
+                        }
+                        //break;
+                    } else trialcount++;
+                }
+
+                //  fclose(fp9);
+                //}
+
+                for(int ftp_close_attempt=0; ftp_close_attempt<3; ftp_close_attempt++) {
+                    if(sendATcommand("AT+FTPPUT=2,0", "OK", 5,0)) {
+                        ftp_close_attempt=6;
+                    }
+                }
+                return;
+                //}
+
+            }
+            return;
+        }
+    }
+}
+void FTP_Fun()
+{
+    /*tid6 = osThreadGetId();
+
+    while(true)
+    {
+        osSignalWait(0x1, osWaitForever);*/
+    if(ftp_connect()) {
+
+        ftp_put();
+        // downloadFTP();
+    }
+    return;
+    //}
+}
+
+int main()
+{
+    int loop_count=0;
+    pc.printf("\r\nHelloWorld\r\n");
+    sendATcommand("AT+CLTS=1\r", "OK", 5,0);
     wait(2);
     sendATcommand("AT+CCLK?\r", "OK", 5,1);
     FILE *fp = fopen("/sd/SiteInfo.txt","a");
-    if(fp == NULL)
-        {
-            FILE *fp = fopen("/sd/SiteInfo.txt","w");
-            fprintf(fp, "\r\n%s",timestamp);   
+    if(fp == NULL) {
+        FILE *fp = fopen("/sd/SiteInfo.txt","w");
+        fprintf(fp, "\r\n%s",timestamp);
+    } else {
+        fprintf(fp, "\r\n%s",timestamp);
+        //pc.printf("\r\n%s",timestamp);
+        fclose(fp);
+    }
+    while(1) {
+        sampling();
+        store();
+        FTP_Fun();
+        loop_count++;
+        if(loop_count%120==0) {
+            loop_count=0;
+            reset_mod();
         }
-    else
-        {
-            fprintf(fp, "\r\n%s",timestamp);
-            //pc.printf("\r\n%s",timestamp);
-            fclose(fp);
-         }
-    
-    sampling();
-    store();
+        wait(10);
+    }
     //fft(16384,0,1,1);
     //prepare_fft_array(fft_start_index,fft_points_count,fft_maximas_required,fft_segments);
 }
\ No newline at end of file