Skip to content
Snippets Groups Projects
Commit d8ec7bdc authored by corentin.cotture's avatar corentin.cotture
Browse files

cartouche

parent 1e63519f
Branches
No related tags found
No related merge requests found
/// @brief Simulation d'un circuit RC
/// @file circuit_rc
/// @date 20233-10-15
/// @author Edi - Froideveaux - Esteves - Cotture
#include "circuit_rc.h"
#include "serialize.h"
/// @brief Calcule le nombre de volt "exact" à l'instant t
/// @param t
/// @return double
double vc_t_exact(double t) {
return Vo * exp(-t / (R * C));
}
/// @brief Calcule le nombre de volt à l'instant t
/// @param t
/// @return double
double v_t (double t) {
double i_t = (C * vc_t_exact(t + delta_t) - C * vc_t_exact(t)) / delta_t;
return R * i_t;
}
// double circuit_rc(double t) {//calcul de l'approximation
// return vc_t(t) + delta_t * (v_t(t) - vc_t(t)) / (R * C);
// }
/// @brief Calcule le nombre de volt à l'instant t+delta_t
/// @param t
/// @param vct
/// @return double
double circuit_rc_approximation(double t, double vct) {//calcul de l'approximation à la première itération
return vct + delta_t * (t - vct) / (R * C);//t=Vo pour la charge= constante
}
//créer un tableau sin pour simuler une alimentation alternative de -12,12
/// @brief Créer un tableau sin pour simuler une alimentation alternative de -12,12
/// @param size
/// @return double*
double* sinfonction (int size){
double* tabSIN =malloc(size*sizeof(double));
for(int i =0; i<size;i++){ //i++ est fait après avoir créer le i
......@@ -27,6 +39,12 @@ double* sinfonction (int size){
}
return tabSIN;
}
/// @brief Applique un filtre passe bas sur un tableau de double
/// @param tabSIN
/// @param size
/// @param filtre_bas
/// @return double*
double* filtre_passe_bas(double* tabSIN,int size,double filtre_bas){
double* tabOUT=calloc(size,sizeof(double));
for(int i=0;i<size;i++){
......@@ -34,6 +52,13 @@ double* filtre_passe_bas(double* tabSIN,int size,double filtre_bas){
}
return tabOUT;
}
/// @brief Applique un filtre passe haut sur un tableau de double
/// @param tabSIN
/// @param size
/// @param filtre_haut
/// @return double*
double* filtre_passe_haut(double* tabSIN,int size,double filtre_haut){
double* tabOUT=calloc(size,sizeof(double));
for(int i=0;i<size;i++){
......@@ -41,6 +66,10 @@ double* filtre_passe_haut(double* tabSIN,int size,double filtre_haut){
}
return tabOUT;
}
/// @brief Affiche un tableau de double
/// @param tab
/// @param size
void printTab(double* tab,int size){
for(int i=0;i<size;i++){
printf("%lf ",tab[i]);
......
/// @brief Simulation d'un circuit RC
/// @file circuit_rc
/// @date 20233-10-15
/// @author Edi - Froideveaux - Esteves - Cotture
//je mets toutes les signatures de mon programme
#include <stdint.h>
#include <stdio.h>
......
# @brief Simulation d'un circuit RC
# @file graph_generator
# @date 20233-10-15
# @author Edi - Froideveaux - Esteves - Cotture
import matplotlib.pyplot as plt
import os
......
/// @brief Simulation d'un circuit RC
/// @file serialize
/// @date 20233-10-15
/// @author Edi - Froideveaux - Esteves - Cotture
#ifndef SERIALIZE
#define SERIALIZE
#include "serialize.h"
#include <string.h>
// Fonction pour allouer dynamiquement un tableau 2D
/// @brief Alloue dynamiquement un tableau 2D
/// @param rows
/// @param cols
/// @return double**
double** allocateMatrix(int rows, int cols) {
double** matrix = (double**)malloc(rows * sizeof(double*));
for (int i = 0; i < rows; i++) {
......@@ -12,7 +21,10 @@ double** allocateMatrix(int rows, int cols) {
return matrix;
}
// Fonction pour libérer la mémoire du tableau 2D
/// @brief Libère la mémoire d'un tableau 2D
/// @param matrix
/// @param rows
void freeMatrix(double** matrix, int rows) {
for (int i = 0; i < rows; i++) {
free(matrix[i]);
......@@ -20,7 +32,9 @@ void freeMatrix(double** matrix, int rows) {
free(matrix);
}
// Fonction pour sérialiser le tableau 2D
/// @brief Sérialise un tableau 2D
/// @param matrix
/// @param filename
void serializeMatrix(Matrix2D* matrix, const char* filename) {
FILE* file = fopen(filename, "wb");
if (file == NULL) {
......@@ -35,7 +49,9 @@ void serializeMatrix(Matrix2D* matrix, const char* filename) {
fclose(file);
}
// Fonction pour désérialiser le tableau 2D
/// @brief Désérialise un tableau 2D
/// @param filename
/// @return Matrix2D
Matrix2D deserializeMatrix(const char* filename) {
FILE* file = fopen(filename, "rb");
if (file == NULL) {
......@@ -54,134 +70,12 @@ Matrix2D deserializeMatrix(const char* filename) {
return matrix;
}
// // Exemple d'utilisation
// int main() {
// // Création d'un tableau 2D
// int rows = 3;
// int cols = 4;
// Matrix2D matrix;
// matrix.rows = rows;
// matrix.cols = cols;
// matrix.data = allocateMatrix(rows, cols);
// // Remplissage du tableau avec des valeurs
// for (int i = 0; i < rows; i++) {
// for (int j = 0; j < cols; j++) {
// matrix.data[i][j] = i * cols + j + 0.5;
// }
// }
// // Sérialisation du tableau
// serializeMatrix(&matrix, "matrix.bin");
// // Libération de la mémoire du tableau
// freeMatrix(matrix.data, rows);
// // Désérialisation du tableau
// Matrix2D deserializedMatrix = deserializeMatrix("matrix.bin");
// // Affichage des valeurs désérialisées
// for (int i = 0; i < deserializedMatrix.rows; i++) {
// for (int j = 0; j < deserializedMatrix.cols; j++) {
// printf("%.2f ", deserializedMatrix.data[i][j]);
// }
// printf("\n");
// }
// // Libération de la mémoire de la matrice désérialisée
// freeMatrix(deserializedMatrix.data, deserializedMatrix.rows);
// return 0;
// }
// Convertir les données en une chaîne JSON
char* matrixToJson(Matrix2D* matrix) {
// Allouer une chaîne de caractères suffisamment grande
int bufferSize = matrix->rows * matrix->cols * 100; // Taille approximative
char* jsonStr = malloc(bufferSize * sizeof(char));
char tempBuffer[50]; // Tampon temporaire pour convertir les entiers en chaînes de caractères
// jsonStr[0] = '\0';
// printf("jsonStr : %s\n ", jsonStr);
// // Commencer la construction de la chaîne JSON
// strcpy(jsonStr, "{\"rows\": ");
// sprintf(tempBuffer, "%d", matrix->rows);
// strcat(jsonStr, tempBuffer);
// strcat(jsonStr, ", \"cols\": ");
// sprintf(tempBuffer, "%d", matrix->cols);
// strcat(jsonStr, tempBuffer);
// strcat(jsonStr, ", \"data\": [");
// // Ajouter les éléments de données à la chaîne JSON
// for (int i = 0; i < matrix->rows; i++) {
// for (int j = 0; j < matrix->cols; j++) {
// sprintf(tempBuffer, "%f", matrix->data[i][j]);
// strcat(jsonStr, tempBuffer);
// if (i != matrix->rows - 1 || j != matrix->cols - 1) {
// strcat(jsonStr, ", ");
// }
// }
// }
// // Terminer la chaîne JSON
// strcat(jsonStr, "]}");
// Commencer la construction de la chaîne JSON
int offset = snprintf(jsonStr, bufferSize, "{\"rows\": %d, \"cols\": %d, \"data\": [", matrix->rows, matrix->cols);
// Ajouter les éléments de données à la chaîne JSON
for (int i = 0; i < matrix->rows; i++) {
// printf("i : %d\n ", i);
for (int j = 0; j < matrix->cols; j++) {
// printf("j : %d\n ", j);
snprintf(tempBuffer, sizeof(tempBuffer), "%f", matrix->data[i][j]);
int tempLen = strlen(tempBuffer);
if (offset + tempLen < bufferSize) {
strcat(jsonStr + offset, tempBuffer);
offset += tempLen;
// printf("jsonStr : %s\n ", jsonStr);
}
if (i != matrix->rows - 1 || j != matrix->cols - 1) {
strcat(jsonStr + offset, ", ");
offset += 2;
}
}
}
// Terminer la chaîne JSON
if (offset + 2 <= bufferSize) {
strcat(jsonStr + offset, "]}");
// printf("jsonStr : %s\n ", jsonStr);
}
return jsonStr;
}
// Enregistrer la chaîne JSON dans un fichier
void saveJsonToFile(const char* jsonStr, const char* fileName) {
FILE* file = fopen(fileName, "w");
if (file != NULL) {
// printf("%s\n", jsonStr);
printf("rien est cassé ?\n");
// printf("json - len : %s",jsonStr);
fputs(jsonStr, file); // TOUT CASSE ICI
fclose(file);
printf("Les données ont été enregistrées au format JSON.\n");
} else {
printf("Impossible d'ouvrir le fichier pour l'enregistrement.\n");
}
}
/// @brief Sauvegarde une matrice dans un fichier
/// @param matrix
/// @param filename
void saveMatrixToFile(Matrix2D* matrix, const char* filename) {
FILE* file = fopen(filename, "w");
if (file == NULL) {
......@@ -200,6 +94,10 @@ void saveMatrixToFile(Matrix2D* matrix, const char* filename) {
printf("Les données ont été enregistrées dans le fichier %s\n", filename);
}
/// @brief Sauvegarde un tableau dans un fichier
/// @param array
/// @param size
/// @param filename
void saveArrayToFile(double* array,int size, const char* filename) {
FILE* file = fopen(filename, "w");
if (file == NULL) {
......
/// @brief Simulation d'un circuit RC
/// @file serialize
/// @date 20233-10-15
/// @author Edi - Froideveaux - Esteves - Cotture
#include <stdio.h>
#include <stdlib.h>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment