diff --git a/.vscode/settings.json b/.vscode/settings.json index 08ba79e..5c64e94 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -14,6 +14,7 @@ "limits": "c", "*.tcc": "c", "type_traits": "c", - "simulateflux.h": "c" + "simulateflux.h": "c", + "pthread.h": "c" } } \ No newline at end of file diff --git a/Code-C/.vscode/settings.json b/Code-C/.vscode/settings.json new file mode 100644 index 0000000..c70a9f1 --- /dev/null +++ b/Code-C/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "power.h": "c" + } +} \ No newline at end of file diff --git a/Code-C/Makefile b/Code-C/Makefile index cdc4abd..7711ce9 100644 --- a/Code-C/Makefile +++ b/Code-C/Makefile @@ -2,8 +2,6 @@ CC = gcc all: # rm powerData.csv - $(CC) simulateFlux.c -o simulateFlux - $(CC) fileGestion.c getArray.c power.c main.c -lm -o main - - ./simulateFlux < ../02400001.TXT - ./main \ No newline at end of file +# $(CC) simulateFlux.c fileGestion.c getArray.c power.c main.c -lm -o main + $(CC) simulateFlux.c main.c -lpthread -o main + ./main < ../02400031.TXT \ No newline at end of file diff --git a/Code-C/b2hd b/Code-C/b2hd deleted file mode 100755 index bdb7e90..0000000 Binary files a/Code-C/b2hd and /dev/null differ diff --git a/Code-C/b2hd.c b/Code-C/b2hd.c index 761273e..c6e5147 100644 --- a/Code-C/b2hd.c +++ b/Code-C/b2hd.c @@ -1,5 +1,8 @@ #include "b2hd.h" - +/** + * @brief allow to transform all binary hex data send by the Vegetal Signal captor in a .TXT file into decimal values in a .csv + * + */ void b2hd() { int i; diff --git a/Code-C/b2hd.h b/Code-C/b2hd.h index 033a535..5479d31 100644 --- a/Code-C/b2hd.h +++ b/Code-C/b2hd.h @@ -5,6 +5,10 @@ #include #include +/** + * @brief struct used to stock binary 32 bits data from the captor + * + */ typedef struct { uint8_t octet1; uint8_t octet2; diff --git a/Code-C/fileGestion.c b/Code-C/fileGestion.c index 5b9eeee..c081480 100644 --- a/Code-C/fileGestion.c +++ b/Code-C/fileGestion.c @@ -1,28 +1,38 @@ #include "fileGestion.h" -void clearRawData(int N){ + +/** + * @brief function that delete nRow lign in the beginning of the file rawData.csv . This function is necessary to not deal with the same ligns over and over + * + * @param nRow number of lign in the beginning of the file rawData.csv that has to be delete + */ +void clearRawData(int nRow){ char buffer[256]; FILE *f = fopen("newFile.csv","w+"); FILE *g = fopen("rawData.csv","r"); - for(int i = 0; i < N; i++){ - fgets(buffer , sizeof buffer , g); - //printf("Line contaigns: %s" , buffer); + for(int i = 0; i < nRow; i++){ //first the program read the first nRow ligns of the csv file but do nothing + fgets(buffer , sizeof buffer , g); } - while(1){ + while(1){ //then, till the end of the csv file it copy the lign to a new csv : newFile.csv if(!fgets(buffer,sizeof buffer , g)) break; fprintf(f,"%s",buffer); } - remove("rawData.csv"); rename("newFile.csv", "rawData.csv"); + remove("rawData.csv"); rename("newFile.csv", "rawData.csv"); //finally we remove the original file and rename the new one to replace rawData.csv fclose(f); fclose(g); } - -void writePowerData(double a[], int N){ +/** + * @brief use to write the file powerData.csv that contaign all power calculous results + * + * @param powerArray + * @param nCol size of the power array, correspond to the number of captor used + */ +void writePowerData(double powerArray[], int nCol){ FILE *f = fopen("powerData.csv","a+"); - for(int i = 0 ; i < N ; i++){ - if( i < N-1){ - fprintf(f, "%f , ", a[i]); + for(int i = 0 ; i < nCol ; i++){ + if( i < nCol-1){ + fprintf(f, "%f , ", powerArray[i]); } else { - fprintf(f, "%f\n", a[i]); + fprintf(f, "%f\n", powerArray[i]); } } fclose(f); diff --git a/Code-C/getArray.c b/Code-C/getArray.c index 07ce2ed..73e6893 100644 --- a/Code-C/getArray.c +++ b/Code-C/getArray.c @@ -13,7 +13,7 @@ long **get(int N, int M) /* Allocate the array */ return array; } -void fillRawData(long** p, int N, int M) { +void fillArrayWithRawData(long** p, int N, int M) { int i, j; char *buffer; @@ -36,19 +36,32 @@ void fillRawData(long** p, int N, int M) { //printf("\n\n"); } } - +/** + * @brief print all the element of a bidimensionnal array p of shape : N x M + */ void printArrayData(long** p, int N, int M) { int i, j; for(i = 0 ; i < N ; i++){ - printf("line n°%d : %d , %d , %d , %d , %d , %d , %d , %d , %d\n" ,i,p[i][0],p[i][1],p[i][2],p[i][3],p[i][4],p[i][5],p[i][6],p[i][7],p[i][8]); + printf("line n°%d : " , i); + for(int j = 0 ; j < M ; j++){ + printf("%d , " , p[i][j]); + if(j==(M-1)) printf("\n"); } -} - -int checkArrayFullyFill(long **p, int N , int M){ - for(int i = 0 ; i < N ; i++){ - if(p[i][0] == '\0'){ return 1; } } - return 0; +} +/** + * @brief verify if all the element of an array are not NULL, return + * + * @param p + * @param N + * @param M + * @return int + */ +bool checkArrayFullyFill(long **p, int N , int M){ + for(int i = 0 ; i < N ; i++){ + if(p[i][0] == '\0'){ return false; } + } + return true; } void freeArray(long **p, int N) { diff --git a/Code-C/getArray.h b/Code-C/getArray.h index 3d6e16a..c975391 100644 --- a/Code-C/getArray.h +++ b/Code-C/getArray.h @@ -2,9 +2,10 @@ #include #include #include +#include #include long **getRawDataArray(); void printArrayData(long** p, int N, int M); void freeArray(long **p, int N); -int checkArrayFullyFill(long **p, int N , int M); \ No newline at end of file +bool checkArrayFullyFill(long **p, int N , int M); \ No newline at end of file diff --git a/Code-C/initialParameters.h b/Code-C/initialParameters.h index 708bc2b..e4bb21e 100644 --- a/Code-C/initialParameters.h +++ b/Code-C/initialParameters.h @@ -1,5 +1,11 @@ -const int nRow = 100000; -const int nCol = 9; -const double freqEch = 250; +#include -extern bool rawDataWriteFlag , stopFlag; \ No newline at end of file +extern bool rawDataWriteFlag , stopFlag; +extern int nRow; +extern int nCol; +extern double freqEch; + +extern int selectionCaptors[]; +extern int sizeSelectionArray; + +extern bool flag; diff --git a/Code-C/main b/Code-C/main index 9d2cfd1..b1ea901 100755 Binary files a/Code-C/main and b/Code-C/main differ diff --git a/Code-C/main.c b/Code-C/main.c index 70402cf..7f0e0f2 100644 --- a/Code-C/main.c +++ b/Code-C/main.c @@ -1,11 +1,26 @@ -//#include "simulateFlux.h" +#include +#include +#include "simulateFlux.h" #include "getArray.h" #include "fileGestion.h" #include "power.h" #include "initialParameters.h" bool rawDataWriteFlag = 0, stopFlag = 0; +int nRow = 100000; +int nCol = 9; +double freqEch = 250; + +int selectionCaptors[] = {1,2,3,4,5,6,7,8}; +int sizeSelectionArray = 8; int main(int argc , char** argv){ - while(power(nRow,nCol,period,timeBandwidth)==0){} + + double period = 1 / freqEch; + double invTimeBandWidth = 1 /(nRow * period); + + pthread_t rawData; + pthread_create(&rawData , NULL, simulateFlux, (void *)&rawData); + pthread_exit(NULL); + while(power(nRow,nCol,period,invTimeBandWidth)){} } \ No newline at end of file diff --git a/Code-C/power b/Code-C/power deleted file mode 100755 index 8d6ec53..0000000 Binary files a/Code-C/power and /dev/null differ diff --git a/Code-C/power.c b/Code-C/power.c index 5f2f58c..b99ad45 100644 --- a/Code-C/power.c +++ b/Code-C/power.c @@ -2,31 +2,30 @@ #include "getArray.h" #include "fileGestion.h" -void powerCalculation(long **p, double a[] , int N, int M , double period , double timeBandwidth){ +void powerCalculation(long **p, double powerArray[] , int N, int M , double period , double invTimeBandwidth){ for(int i = 0; i < M-1; i++){ int j = 0; - a[i] = 0; + powerArray[i] = 0; while(j < N-1){ double aire = ( pow(p[j][i+1],2) + pow(p[j+1][i+1],2) ) / 2 * period; //printf("aire [%d,%d] : %f\n",j,i,aire); - a[i] += aire; + powerArray[i] += aire; j++; } - a[i] *= timeBandwidth; - //printf("%f\n", a[i]); + powerArray[i] *= invTimeBandwidth; + //printf("%f\n", powerArray[i]); } } -int power(int N , int M, double periode , double timeBandwidth){ +bool power(int N , int M, double periode , double invTimeBandwidth){ long **p = getRawDataArray(N, M); - //printArrayData(p,N,M); double pw[8]; if(p !=NULL){ - powerCalculation(p,pw,N,M,periode,timeBandwidth); + powerCalculation(p,pw,N,M,periode,invTimeBandwidth); writePowerData(pw,8); freeArray(p,N); - return 0; + return true; } else{ - return 1; + return false; } } \ No newline at end of file diff --git a/Code-C/power.h b/Code-C/power.h index 0da1b44..f83ba8c 100644 --- a/Code-C/power.h +++ b/Code-C/power.h @@ -1,3 +1,4 @@ #include +#include int power(int N , int M, double periode , double timeBandwidth); \ No newline at end of file diff --git a/Code-C/simulateFlux b/Code-C/simulateFlux deleted file mode 100755 index 5f39085..0000000 Binary files a/Code-C/simulateFlux and /dev/null differ diff --git a/Code-C/simulateFlux.c b/Code-C/simulateFlux.c index 5f23070..b3e20c2 100644 --- a/Code-C/simulateFlux.c +++ b/Code-C/simulateFlux.c @@ -1,18 +1,83 @@ #include "simulateFlux.h" +#include "initialParameters.h" +int cptData = 0; +int cptFile = 1; -int writeOneRawData(){ +char* convertIntegerToChar(int N) +{ + // Count digits in number N + int m = N; + int digit = 0; + while (m) { + digit++; + m /= 10; + } + char* arr; + char arr1[digit]; + arr = (char*)malloc(digit); + int index = 0; + while (N) { + arr1[++index] = N % 10 + '0'; + N /= 10; + } + int i; + for (i = 0; i < index; i++) { + arr[i] = arr1[index - i]; + } + arr[i] = '\0'; + return (char*)arr; +} +FILE *createThenOpenNewRawDataFile(){ + char *fileName = "RawData"; + char *extension = ".csv"; + char *fileNumber = convertIntegerToChar(cptFile); + + char fileNameNumber[strlen(fileName)+strlen(fileNumber)]; + char fullFillName[strlen(fileNameNumber)+strlen(extension)]; + + strcpy( fileNameNumber, fileName ); + strcat( fileNameNumber, fileNumber ); + + strcpy( fullFillName, fileNameNumber ); + strcat( fullFillName, extension ); + + FILE *file = fopen(fullFillName,"w+"); + return file; +} + +int64_t millis() +{ + struct timespec now; + timespec_get(&now, TIME_UTC); + return ((int64_t) now.tv_sec) * 1000 + ((int64_t) now.tv_nsec) / 1000000; +} + +int intInArray(int number , int *array , int N){ + for(int i = 0 ; i < N ; i++){ + if(array[i] == number) return 0; + } + return -1; +} +int maxInArray(int *array , int N){ + int max = 0; + for(int i = 0 ; i < N ; i++){ + if(array[i]>max) max = array[i]; + } + return max; +} + +bool writeOneRawData(FILE *rawDataFile){ char buff[26]; char buff2[18]; int32_t values[8]; uint32_t valbin[8]; quartet value; - FILE *f = fopen("rawData.csv" , "a"); - if(fread(&buff, 26, 1, stdin)) { - fprintf(f , "%d,", millis()); + if(fread(&buff, 26, 1, stdin)) { + fprintf(rawDataFile , "%d,", millis()); if (strncmp(buff, "#################\n", (size_t)18) == 0) { if (!(fread(&buff2, 18, 1, stdin))) { fprintf(stderr, "Erreur lecture après ###...#"); @@ -22,39 +87,43 @@ int writeOneRawData(){ strncpy(&buff[8], buff2, 18); } } - + int maxCapteurNb = maxInArray(selectionCaptors,sizeSelectionArray); for (int i = 1; i < 9; i++){ - value.octet1 = buff[3*i+1]; - value.octet2 = buff[3*i+2]; - value.octet3 = buff[3*i+3]; - value.octet4 = 0; - - valbin[i] = buff[3*i+1]*256*256*256 + buff[3*i+2]*256*256 + buff[3*i+3]*256; - memcpy(&values[i], &valbin[i], sizeof(uint32_t)); + if(intInArray(i,selectionCaptors,sizeSelectionArray)==0){ + value.octet1 = buff[3*i+1]; + value.octet2 = buff[3*i+2]; + value.octet3 = buff[3*i+3]; + value.octet4 = 0; + + valbin[i] = buff[3*i+1]*256*256*256 + buff[3*i+2]*256*256 + buff[3*i+3]*256; + memcpy(&values[i], &valbin[i], sizeof(uint32_t)); - if(i<8){ - fprintf(f, "%d,", values[i]/256); - } - else{ - fprintf(f, "%d\n", values[i]/256); + if(i==maxCapteurNb){ + fprintf(rawDataFile, "%d\n", values[i]/256); + } + else{ + fprintf(rawDataFile, "%d,", values[i]/256); + } } } - fclose(f); - cpt++; - return 0; + cptData++; + //sleep(0.004); //simul la freq ech + return true; } else { - return 1; + return false; } } +void *simulateFlux(void *vargp){ -void simulateFlux(){ - while(writeOneRawData()==0){ - sleep(0.004); + FILE *f = createThenOpenNewRawDataFile(); + while(writeOneRawData(f)){ + if(cptData == nRow){ + cptData = 0; + cptFile++; + fclose(f); + // initialiser et/ou modifier la structure ici + f = createThenOpenNewRawDataFile(); + } } -} - -int main(int argc , char argv[]){ - simulateFlux(); - printf("%d",cpt); } \ No newline at end of file diff --git a/Code-C/simulateFlux.h b/Code-C/simulateFlux.h index 4bce58c..b9edec8 100644 --- a/Code-C/simulateFlux.h +++ b/Code-C/simulateFlux.h @@ -3,10 +3,9 @@ #include #include #include +#include #include -int cpt = 0; - typedef struct { uint8_t octet1; uint8_t octet2; @@ -14,9 +13,5 @@ typedef struct { uint8_t octet4; } quartet; -int64_t millis() -{ - struct timespec now; - timespec_get(&now, TIME_UTC); - return ((int64_t) now.tv_sec) * 1000 + ((int64_t) now.tv_nsec) / 1000000; -} \ No newline at end of file +bool writeOneRawData(FILE *f); +void *simulateFlux(void *vargp); \ No newline at end of file diff --git a/Py-Script/Traitement-FFT.py b/Py-Script/Traitement-FFT.py index 5347b63..6907068 100644 --- a/Py-Script/Traitement-FFT.py +++ b/Py-Script/Traitement-FFT.py @@ -1,5 +1,5 @@ import numpy as np -#import matplotlib.pyplot as plt +import matplotlib.pyplot as plt import struct @@ -9,7 +9,7 @@ import struct AllRes = [[],[],[],[],[],[],[],[]] -link = "02400007.TXT" +link = "02400031.TXT" f = open(link , "rb")#ouvertuture du fichier txt traiter @@ -47,7 +47,7 @@ while True: f.close()#fermeturedu fichier txt -"""fig, (ax1, ax2) = plt.subplots(2, 1) #Création de la figure à 2 plots +fig, (ax1, ax2) = plt.subplots(2, 1) #Création de la figure à 2 plots Fe = 250000 #Fréquence d'échantillonage tstep = 1 / Fe #Time spacing @@ -70,8 +70,8 @@ sp = np.fft.fft(y,Fe) f = np.fft.fftfreq(Fe,tstep) #axe des abscisses: fréquence #Définition des courbes des plots -ax1.plot(t,y) +ax1.plot(t,y) # print y(t) ax2.plot(f,abs(sp)) #Lancement de l'affichage du plot -plt.show()""" \ No newline at end of file +plt.show() \ No newline at end of file