Start of QAM

This commit is contained in:
2025-09-29 15:47:14 +02:00
parent 8555648077
commit f614b794aa
2 changed files with 85 additions and 0 deletions

BIN
QAM/out Executable file

Binary file not shown.

85
QAM/qam.c Normal file
View File

@ -0,0 +1,85 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
struct qam_system_s {
int M; // Nombre de symboles M-QAM
int k; // Nombre de bits/symboles
double Fs; // Fréquence d'échantillionage
double Ts; // Temps d'échantillionage
int N; // Nombre d'échantillions
double Fc; // Fréquence de la porteuse
double complex** constellation; // Tableau de symboles I + j Q
};
typedef struct qam_system_s qam_system;
void init_constellation (qam_system *qam) {
int sm = sqrt(qam->M);
qam->constellation = (double complex**)malloc(sizeof(double complex*) * sm);
for (int i = 0; i < sm; i++) {
qam->constellation[i] = (double complex*)malloc(sizeof(double complex) * sm);
}
for (int i = 0; i < sm; i++) {
double complex ip = -(sm - 1) + 2 * i;
for (int j = 0; j < sm; j++) {
double complex qp = -(sm - 1) + 2 * j;
qam->constellation[i][j] = ip + I * qp;
qam->constellation[i][j] /= sqrt((qam->M-1)/3.0); // Pour puissance unitaire
}
}
}
void bits_to_symbols (qam_system *qam, int *bits, int nb_bits, double complex *symbols) {
int nb_symbols = nb_bits / qam->k;
int sm = sqrt(qam->M);
for (int k = 0; k < nb_symbols; k++) {
int id = 0;
for (int b = 0 ; b < qam->k; b++) {
id = id * 2 + bits[k * qam->k + b];
}
int i = id / sm;
int j = id % sm;
symbols[k] = qam->constellation[i][j];
}
}
void modulate (qam_system *qam,double complex *symbols, int nb_symbols, double complex *s) {
for (int k = 0; k < nb_symbols; k++) {
double complex iq = symbols[k];
for (int n = 0; n < qam->N; n++) {
s[k * qam->N + n] = iq * cexp(2 * M_PI * qam->Fc * ((double)n / qam->Fs));
}
}
}
int main () {
qam_system qam;
qam.M = 16;
qam.k = (int)log2((double)(qam.M));
qam.Fs = 44100;
qam.Ts = 0.01;
qam.N = (int)qam.Fs * qam.Ts;
qam.Fc = 2000;
init_constellation(&qam);
// Nombre de bit multiple de k sinon remplir de zero jusqu'a ce que ce le soit
int bits[16] = {1,0,1,1, 0,1,1,0, 1,1,0,0, 0,0,0,1};
int nb_symbols = 16 / qam.k;
double complex symbols[nb_symbols];
bits_to_symbols(&qam, bits, 16, symbols);
int total_samples = qam.N * nb_symbols;
double complex* s = (double complex*)malloc(sizeof(double complex) * total_samples);
modulate(&qam, symbols, nb_symbols, s);
for(int i=0; i<sqrt(qam.M); i++)
free(qam.constellation[i]);
free(qam.constellation);
free(s);
return 0;
}