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/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 78d17e1..126d240 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..d6543a1 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,14 +36,27 @@ 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"); } + } } - +/** + * @brief verify if all the element of an array are not NULL, return + * + * @param p + * @param N + * @param M + * @return int + */ int checkArrayFullyFill(long **p, int N , int M){ for(int i = 0 ; i < N ; i++){ if(p[i][0] == '\0'){ return 1; } diff --git a/Code-C/initialParameters.h b/Code-C/initialParameters.h index 3007f21..5a635fa 100644 --- a/Code-C/initialParameters.h +++ b/Code-C/initialParameters.h @@ -1,6 +1,10 @@ -const int nRow = 100000; -const int nCol = 9; -const double freqEch = 250; +#include -const double period = 1.0/freqEch; -const double timeBandwidth = 1.0 /(nRow * period); +extern int nRow; +extern int nCol; +extern double freqEch; + +extern int selectionCaptors[]; +extern int sizeSelectionArray; + +extern bool flag; \ No newline at end of file diff --git a/Code-C/main b/Code-C/main deleted file mode 100755 index 9d2cfd1..0000000 Binary files a/Code-C/main and /dev/null differ diff --git a/Code-C/main.c b/Code-C/main.c index 3e55a33..5de3cde 100644 --- a/Code-C/main.c +++ b/Code-C/main.c @@ -1,9 +1,25 @@ -//#include "simulateFlux.h" +#include +#include +#include "simulateFlux.h" #include "getArray.h" #include "fileGestion.h" #include "power.h" #include "initialParameters.h" +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)==0){} } \ 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..e672b4f 100644 --- a/Code-C/power.c +++ b/Code-C/power.c @@ -2,26 +2,26 @@ #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){ +int 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; diff --git a/Code-C/simulateFlux b/Code-C/simulateFlux deleted file mode 100755 index 646e120..0000000 Binary files a/Code-C/simulateFlux and /dev/null differ diff --git a/Code-C/simulateFlux.c b/Code-C/simulateFlux.c index 1cbb294..381471f 100644 --- a/Code-C/simulateFlux.c +++ b/Code-C/simulateFlux.c @@ -1,18 +1,99 @@ #include "simulateFlux.h" +#include "initialParameters.h" + +typedef struct { + char *fileName; +} nameFill; -int writeOneRawData(){ +typedef struct { + nameFill curentFile; + nameFill *nextFile; +}rawDataFile; +nameFill getNameFill(int N){ + nameFill file; + file.fileName = (char *) malloc(N*sizeof(char *)); + return file; +} +int cptData = 0; +int cptFile = 1; +nameFill FirstFile; + +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,47 +103,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++; - sleep(0.004); //simul la freq ech - return 0; + cptData++; + //sleep(0.004); //simul la freq ech + return true; } else { - return 1; + return false; } } +void *simulateFlux(void *vargp){ -int simulateFlux(){ - while(writeOneRawData()==0){ - if(cpt==nRows){ - tFlag = 1; - } - while(tFlag == 1){ - /*printf("max nRows"); - cpt = 0; - tFlag = 0; - break;*/ + 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(); } \ No newline at end of file diff --git a/Code-C/simulateFlux.h b/Code-C/simulateFlux.h index 82d8a35..77fdc3e 100644 --- a/Code-C/simulateFlux.h +++ b/Code-C/simulateFlux.h @@ -3,12 +3,9 @@ #include #include #include +#include #include -int cpt = 0; -int tFlag = 0; -int nRows = 1000; - typedef struct { uint8_t octet1; uint8_t octet2; @@ -16,9 +13,12 @@ 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 +extern struct nameFill; + + +extern struct rawDataFile; + +nameFill getNameFill(int N); + +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