Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem dspmodified mbed
Fork of fft_merge_main_codev1 by
Diff: main.cpp
- Revision:
- 6:19f5d8f63010
- Parent:
- 5:87587eaeb702
- Child:
- 8:61686460d0e7
diff -r 87587eaeb702 -r 19f5d8f63010 main.cpp --- a/main.cpp Sun Jan 25 09:02:18 2015 +0000 +++ b/main.cpp Sun Jan 25 09:06:50 2015 +0000 @@ -53,7 +53,7 @@ //--------------------------------------------------------------------------------------------------// void reset_mod() { - NVIC_SystemReset(); + NVIC_SystemReset(); } //--------------------------------------------------------------------------------------------------// @@ -66,25 +66,20 @@ 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 + 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 + 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 - { + 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) - { + } 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; @@ -92,13 +87,13 @@ //--------------------------------------------------------------------------------------------------// // 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); + sendATcommand("AT+CCLK?\r", "OK", 5,1); //sprintf(response,timestamp); x=20; year = 10*(timestamp[x+1]-48) + (timestamp[x+2]-48); @@ -106,24 +101,21 @@ 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); - + second = (timestamp[x+16]-48)*10+(timestamp[x+17]-48); + sprintf(time_stamp,"%02d%02d%02d%02d%02d%02d",year,month,date,hour,minute,second); - while(year<15||year>20) - { - sendATcommand("AT+CCLK?\r", "OK", 5,0); + 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(attempt_cnt>4) break; } - if(year>14&&year<21) - { - if(timestamp!=timestampold) - { + if(year>14&&year<21) { + if(timestamp!=timestampold) { sprintf(timestampold,timestamp); //pc.printf("\r\nold time stamp is%s\r\n",timestampold); - - + + 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); @@ -135,16 +127,12 @@ mkdir(month_dir,0777); mkdir(date_dir,0777); mkdir(hour_dir,0777); - } - else - { + } 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 - { + } 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); @@ -156,24 +144,22 @@ // DC Detect // //--------------------------------------------------------------------------------------------------// void DC_Detection() -{ +{ Timer t; unsigned long 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); } //--------------------------------------------------------------------------------------------------// @@ -185,10 +171,8 @@ unsigned long 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; @@ -210,110 +194,108 @@ //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("AC sampling 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){ + +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 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])); + } + fprintf(fp,"\r\n\r\n"); + fclose(fp); } +} //--------------------------------------------------------------------------------------------------// // Storage // @@ -324,137 +306,119 @@ 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 { + 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",time_stamp); + 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",time_stamp); + 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); + + // } } -int main(){ - pc.printf("\r\nHelloWorld\r\n"); - sendATcommand("AT+CLTS=1\r", "OK", 5,0); +int main() +{ + pc.printf("\r\nHelloWorld\r\n"); + sendATcommand("AT+CLTS=1\r", "OK", 5,0); wait(2); - sendATcommand("AT+CCLK?\r", "OK", 5,1); + GetFileDir(); FILE *fp = fopen("/sd/SiteInfo.txt","a"); - 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); - } + if(fp == NULL) { + FILE *fp = fopen("/sd/SiteInfo.txt","w"); + fprintf(fp, "\r\n%s",time_stamp); + } else { + fprintf(fp, "\r\n%s",time_stamp); + pc.printf("\r\n%s",time_stamp); + fclose(fp); + } pc.printf("\r\n Timestamp saved"); - while(1) - { - - sampling(); - store(); - wait(34); - } + while(1) { + + sampling(); + store(); + wait(34); + } //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